|
| 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) |
0 commit comments