Skip to content

Commit

Permalink
V1
Browse files Browse the repository at this point in the history
Working version 1
  • Loading branch information
LAB02 Research committed Nov 24, 2021
1 parent ce9870a commit c44935f
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 1 deletion.
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 <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.

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 <a href="https://hacs.xyz" target="_blank">HACS</a>. 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 <a href="https://www.home-assistant.io/integrations/notify/" target="_blank">notifications integration</a>, 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.
1 change: 1 addition & 0 deletions custom_components/hass.agent notifier/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 10 additions & 0 deletions custom_components/hass.agent notifier/manifest.json
Original file line number Diff line number Diff line change
@@ -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": []
}
95 changes: 95 additions & 0 deletions custom_components/hass.agent notifier/notify.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "HASS.Agent Notifier",
"domains": ["notify"],
"render_readme": true
}

0 comments on commit c44935f

Please sign in to comment.