From c44935ffd85856aff9dd8ee6bf9d227702be806a Mon Sep 17 00:00:00 2001 From: LAB02 Research Date: Wed, 24 Nov 2021 11:33:50 +0100 Subject: [PATCH] V1 Working version 1 --- README.md | 103 +++++++++++++++++- .../hass.agent notifier/__init__.py | 1 + .../hass.agent notifier/manifest.json | 10 ++ .../hass.agent notifier/notify.py | 95 ++++++++++++++++ hacs.json | 5 + 5 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 custom_components/hass.agent notifier/__init__.py create mode 100644 custom_components/hass.agent notifier/manifest.json create mode 100644 custom_components/hass.agent notifier/notify.py create mode 100644 hacs.json diff --git a/README.md b/README.md index fa52b00..96aa820 100644 --- a/README.md +++ b/README.md @@ -1 +1,102 @@ -HASS.Agent-Notifier +# HASS.Agent Notifier + +[![hacs_badge](https://img.shields.io/badge/HACS-Default-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration) + +This Home Assistant integration allows you to send notifications to HASS.Agent, a Windows-based Home Assistant client. + +Note: it won't be of much use if you don't have HASS.Agent installed & configured on at least one device. + +Contents +======== + + * [Functionality](#functionality) + * [Installation](#installation) + * [Configuration](#configuration) + * [Usage](#usage) + * [Wishlist](#wishlist) + +Functionality +--- + +Currently, it's possible to send normal (text-based) and image notifications. + + +Installation +--- + +The easiest way to install is to use HACS. Simply search for **HASS.Agent Notifier**, install and restart Home Assistant. + +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. + + +Configuration +--- + +This integration exposes itself as a notifications integration, and has to be configured as such: + +```yaml +notify: + name: "hass agent test device" + platform: hass_agent_notifier + resource: http://{device_ip}:5115/notify +``` + +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. + +Restart Home Assistant to load your configuration. + + +Usage +--- + +### General + +Currently, there are four variables you can set: + + * `message`: the message you want to show + * `title`: the title of your popup [optional] + * `image`: http(s) url containing the location of an image [optional] + * `duration`: duration (in seconds) for which the popup will be shown [optional] + +### Text notification + +```yaml + action: + - service: notify.hass_agent_test_device + data: + message: "This is a test message." +``` + +### Text notification with title and duration + +```yaml + action: + - service: notify.hass_agent_test_device + data: + message: "This is a test message with title and 3 sec duration." + title: "HASS.Agent Test" + data: + duration: 3 +``` + +### Image notification + +```yaml + action: + - service: notify.hass_agent_test_device + data: + message: "This is a test message with an image." + data: + image: "http://10.0.0.6:1234/jpeg/image.jpg" +``` + + +Wishlist +--- + +List of things I want to add somewhere down the road: + + * ability to add commands + * add 'critical' type to attract more attention + +If you have any other wishes, feel free to submit a ticket. diff --git a/custom_components/hass.agent notifier/__init__.py b/custom_components/hass.agent notifier/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/custom_components/hass.agent notifier/__init__.py @@ -0,0 +1 @@ + diff --git a/custom_components/hass.agent notifier/manifest.json b/custom_components/hass.agent notifier/manifest.json new file mode 100644 index 0000000..26624a3 --- /dev/null +++ b/custom_components/hass.agent notifier/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "hass_agent_notifier", + "name": "HASS.Agent Notifier", + "documentation": "https://github.com/LAB02-Research/HASS.Agent-Notifier", + "issue_tracker": "https://github.com/LAB02-Research/HASS.Agent-Notifier/issues", + "dependencies": [], + "codeowners": ["@LAB02-Admin"], + "version": "2021.8.6", + "requirements": [] +} diff --git a/custom_components/hass.agent notifier/notify.py b/custom_components/hass.agent notifier/notify.py new file mode 100644 index 0000000..9aa63fa --- /dev/null +++ b/custom_components/hass.agent notifier/notify.py @@ -0,0 +1,95 @@ +""" +Custom component for Home Assistant to enable sending messages via HASS Agent. + + +Example configuration.yaml entry: + +notify: + - name: hass notifier + platform: hass_agent_notifier + resource: http://192.168.0.1:5115/notify + +With this custom component loaded, you can send messaged to a HASS Agent. +""" + +import logging + +import requests +import voluptuous as vol + +from homeassistant.components.notify import ( + ATTR_MESSAGE, + ATTR_TITLE, + ATTR_DATA, + PLATFORM_SCHEMA, + BaseNotificationService, +) +from homeassistant.const import ( + CONF_RESOURCE, + HTTP_BAD_REQUEST, + HTTP_INTERNAL_SERVER_ERROR, + HTTP_OK, +) +import homeassistant.helpers.config_validation as cv + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( + { + vol.Required(CONF_RESOURCE): cv.url + } +) + +_LOGGER = logging.getLogger(__name__) + +ATTR_IMAGE = 'image' +ATTR_DURATION = 'duration' + + +def get_service(hass, config, discovery_info=None): + """Get the HASS Agent notification service.""" + resource = config.get(CONF_RESOURCE) + + return HassAgentNotificationService( + hass, + resource + ) + + +class HassAgentNotificationService(BaseNotificationService): + """Implementation of the HASS Agent notification service""" + + def __init__( + self, + hass, + resource + ): + """Initialize the service.""" + self._resource = resource + self._hass = hass + + def send_message(self, message="", title="", **kwargs): + """Send the message to the provided resource.""" + data = kwargs.get(ATTR_DATA, None) + image = data.get(ATTR_IMAGE) if data is not None and ATTR_IMAGE in data else None + duration = data.get(ATTR_DURATION) if data is not None and ATTR_DURATION in data else 0 + + payload = ({ + 'message': message, + 'title': title, + 'image': image, + 'duration': duration + }) + + response = requests.post( + self._resource, + json=payload, + timeout=10 + ) + + if HTTP_INTERNAL_SERVER_ERROR <= response.status_code < 600: + _LOGGER.exception("Server error. Response %d: %s:", response.status_code, response.reason) + elif HTTP_BAD_REQUEST <= response.status_code < HTTP_INTERNAL_SERVER_ERROR: + _LOGGER.exception("Client error. Response %d: %s:", response.status_code, response.reason) + elif HTTP_OK <= response.status_code < 300: + _LOGGER.debug("Success. Response %d: %s:", response.status_code, response.reason) + else: + _LOGGER.debug("Response %d: %s:", response.status_code, response.reason) diff --git a/hacs.json b/hacs.json new file mode 100644 index 0000000..9a40c0b --- /dev/null +++ b/hacs.json @@ -0,0 +1,5 @@ +{ + "name": "HASS.Agent Notifier", + "domains": ["notify"], + "render_readme": true +} \ No newline at end of file