Skip to content

Commit c44935f

Browse files
author
LAB02 Research
committed
V1
Working version 1
1 parent ce9870a commit c44935f

File tree

5 files changed

+213
-1
lines changed

5 files changed

+213
-1
lines changed

README.md

+102-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
1-
HASS.Agent-Notifier
1+
# HASS.Agent Notifier
2+
3+
[![hacs_badge](https://img.shields.io/badge/HACS-Default-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration)
4+
5+
This <a href="https://www.home-assistant.io" target="_blank">Home Assistant</a> integration allows you to send notifications to <a href="https://github.com/LAB02-Research/HASS.Agent" target="_blank">HASS.Agent</a>, a Windows-based Home Assistant client.
6+
7+
Note: it won't be of much use if you don't have HASS.Agent installed & configured on at least one device.
8+
9+
Contents
10+
========
11+
12+
* [Functionality](#functionality)
13+
* [Installation](#installation)
14+
* [Configuration](#configuration)
15+
* [Usage](#usage)
16+
* [Wishlist](#wishlist)
17+
18+
Functionality
19+
---
20+
21+
Currently, it's possible to send normal (text-based) and image notifications.
22+
23+
24+
Installation
25+
---
26+
27+
The easiest way to install is to use <a href="https://hacs.xyz" target="_blank">HACS</a>. Simply search for **HASS.Agent Notifier**, install and restart Home Assistant.
28+
29+
If you want to manually install, copy the `hass.agent notifier` folder into the `config\custom_components` folder of your Home Assistant instance, and restart.
30+
31+
32+
Configuration
33+
---
34+
35+
This integration exposes itself as a <a href="https://www.home-assistant.io/integrations/notify/" target="_blank">notifications integration</a>, and has to be configured as such:
36+
37+
```yaml
38+
notify:
39+
name: "hass agent test device"
40+
platform: hass_agent_notifier
41+
resource: http://{device_ip}:5115/notify
42+
```
43+
44+
Replace `{device_ip}` with the IP of the device that has an HASS.Agent instance running. Optionally replace `5115` if you've configured a different port.
45+
46+
Restart Home Assistant to load your configuration.
47+
48+
49+
Usage
50+
---
51+
52+
### General
53+
54+
Currently, there are four variables you can set:
55+
56+
* `message`: the message you want to show
57+
* `title`: the title of your popup [optional]
58+
* `image`: http(s) url containing the location of an image [optional]
59+
* `duration`: duration (in seconds) for which the popup will be shown [optional]
60+
61+
### Text notification
62+
63+
```yaml
64+
action:
65+
- service: notify.hass_agent_test_device
66+
data:
67+
message: "This is a test message."
68+
```
69+
70+
### Text notification with title and duration
71+
72+
```yaml
73+
action:
74+
- service: notify.hass_agent_test_device
75+
data:
76+
message: "This is a test message with title and 3 sec duration."
77+
title: "HASS.Agent Test"
78+
data:
79+
duration: 3
80+
```
81+
82+
### Image notification
83+
84+
```yaml
85+
action:
86+
- service: notify.hass_agent_test_device
87+
data:
88+
message: "This is a test message with an image."
89+
data:
90+
image: "http://10.0.0.6:1234/jpeg/image.jpg"
91+
```
92+
93+
94+
Wishlist
95+
---
96+
97+
List of things I want to add somewhere down the road:
98+
99+
* ability to add commands
100+
* add 'critical' type to attract more attention
101+
102+
If you have any other wishes, feel free to submit a ticket.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"domain": "hass_agent_notifier",
3+
"name": "HASS.Agent Notifier",
4+
"documentation": "https://github.com/LAB02-Research/HASS.Agent-Notifier",
5+
"issue_tracker": "https://github.com/LAB02-Research/HASS.Agent-Notifier/issues",
6+
"dependencies": [],
7+
"codeowners": ["@LAB02-Admin"],
8+
"version": "2021.8.6",
9+
"requirements": []
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
Custom component for Home Assistant to enable sending messages via HASS Agent.
3+
4+
5+
Example configuration.yaml entry:
6+
7+
notify:
8+
- name: hass notifier
9+
platform: hass_agent_notifier
10+
resource: http://192.168.0.1:5115/notify
11+
12+
With this custom component loaded, you can send messaged to a HASS Agent.
13+
"""
14+
15+
import logging
16+
17+
import requests
18+
import voluptuous as vol
19+
20+
from homeassistant.components.notify import (
21+
ATTR_MESSAGE,
22+
ATTR_TITLE,
23+
ATTR_DATA,
24+
PLATFORM_SCHEMA,
25+
BaseNotificationService,
26+
)
27+
from homeassistant.const import (
28+
CONF_RESOURCE,
29+
HTTP_BAD_REQUEST,
30+
HTTP_INTERNAL_SERVER_ERROR,
31+
HTTP_OK,
32+
)
33+
import homeassistant.helpers.config_validation as cv
34+
35+
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
36+
{
37+
vol.Required(CONF_RESOURCE): cv.url
38+
}
39+
)
40+
41+
_LOGGER = logging.getLogger(__name__)
42+
43+
ATTR_IMAGE = 'image'
44+
ATTR_DURATION = 'duration'
45+
46+
47+
def get_service(hass, config, discovery_info=None):
48+
"""Get the HASS Agent notification service."""
49+
resource = config.get(CONF_RESOURCE)
50+
51+
return HassAgentNotificationService(
52+
hass,
53+
resource
54+
)
55+
56+
57+
class HassAgentNotificationService(BaseNotificationService):
58+
"""Implementation of the HASS Agent notification service"""
59+
60+
def __init__(
61+
self,
62+
hass,
63+
resource
64+
):
65+
"""Initialize the service."""
66+
self._resource = resource
67+
self._hass = hass
68+
69+
def send_message(self, message="", title="", **kwargs):
70+
"""Send the message to the provided resource."""
71+
data = kwargs.get(ATTR_DATA, None)
72+
image = data.get(ATTR_IMAGE) if data is not None and ATTR_IMAGE in data else None
73+
duration = data.get(ATTR_DURATION) if data is not None and ATTR_DURATION in data else 0
74+
75+
payload = ({
76+
'message': message,
77+
'title': title,
78+
'image': image,
79+
'duration': duration
80+
})
81+
82+
response = requests.post(
83+
self._resource,
84+
json=payload,
85+
timeout=10
86+
)
87+
88+
if HTTP_INTERNAL_SERVER_ERROR <= response.status_code < 600:
89+
_LOGGER.exception("Server error. Response %d: %s:", response.status_code, response.reason)
90+
elif HTTP_BAD_REQUEST <= response.status_code < HTTP_INTERNAL_SERVER_ERROR:
91+
_LOGGER.exception("Client error. Response %d: %s:", response.status_code, response.reason)
92+
elif HTTP_OK <= response.status_code < 300:
93+
_LOGGER.debug("Success. Response %d: %s:", response.status_code, response.reason)
94+
else:
95+
_LOGGER.debug("Response %d: %s:", response.status_code, response.reason)

hacs.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "HASS.Agent Notifier",
3+
"domains": ["notify"],
4+
"render_readme": true
5+
}

0 commit comments

Comments
 (0)