|
8 | 8 | - name: hass notifier |
9 | 9 | platform: hass_agent_notifier |
10 | 10 | resource: http://192.168.0.1:5115/notify |
11 | | - |
| 11 | +
|
12 | 12 | With this custom component loaded, you can send messaged to a HASS Agent. |
13 | 13 | """ |
14 | 14 |
|
15 | 15 | import logging |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +from functools import partial |
16 | 19 |
|
17 | 20 | import requests |
18 | 21 | import voluptuous as vol |
19 | 22 |
|
20 | 23 | from homeassistant.components.notify import ( |
21 | | - ATTR_MESSAGE, |
| 24 | + ATTR_TITLE_DEFAULT, |
22 | 25 | ATTR_TITLE, |
23 | 26 | ATTR_DATA, |
24 | 27 | PLATFORM_SCHEMA, |
25 | 28 | BaseNotificationService, |
26 | 29 | ) |
27 | 30 |
|
| 31 | +from homeassistant.components import media_source |
| 32 | + |
28 | 33 | from http import HTTPStatus |
29 | 34 |
|
30 | 35 | from homeassistant.const import ( |
31 | 36 | CONF_RESOURCE, |
32 | 37 | ) |
33 | 38 | import homeassistant.helpers.config_validation as cv |
34 | 39 |
|
35 | | -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( |
36 | | - { |
37 | | - vol.Required(CONF_RESOURCE): cv.url |
38 | | - } |
39 | | -) |
| 40 | +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_RESOURCE): cv.url}) |
40 | 41 |
|
41 | 42 | _LOGGER = logging.getLogger(__name__) |
42 | 43 |
|
| 44 | + |
43 | 45 | def get_service(hass, config, discovery_info=None): |
44 | 46 | """Get the HASS Agent notification service.""" |
45 | 47 | resource = config.get(CONF_RESOURCE) |
46 | 48 |
|
47 | 49 | _LOGGER.info("Service created") |
48 | 50 |
|
49 | | - return HassAgentNotificationService( |
50 | | - hass, |
51 | | - resource |
52 | | - ) |
| 51 | + return HassAgentNotificationService(hass, resource) |
53 | 52 |
|
54 | 53 |
|
55 | 54 | class HassAgentNotificationService(BaseNotificationService): |
56 | 55 | """Implementation of the HASS Agent notification service""" |
57 | 56 |
|
58 | | - def __init__( |
59 | | - self, |
60 | | - hass, |
61 | | - resource |
62 | | - ): |
| 57 | + def __init__(self, hass, resource): |
63 | 58 | """Initialize the service.""" |
64 | 59 | self._resource = resource |
65 | 60 | self._hass = hass |
66 | 61 |
|
67 | | - def send_message(self, message="", title="", **kwargs): |
| 62 | + def send(self, url, data): |
| 63 | + return requests.post(url, json=data, timeout=10) |
| 64 | + |
| 65 | + async def async_send_message(self, message: str, **kwargs: Any): |
68 | 66 | """Send the message to the provided resource.""" |
69 | 67 | _LOGGER.debug("Preparing notification ..") |
70 | 68 |
|
| 69 | + title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) |
71 | 70 | data = kwargs.get(ATTR_DATA, None) |
72 | 71 |
|
73 | | - payload = ({ |
74 | | - 'message': message, |
75 | | - 'title': title, |
76 | | - 'data': data |
77 | | - }) |
| 72 | + image = data.get("image", None) |
| 73 | + if image is not None: |
| 74 | + if media_source.is_media_source_id(image): |
| 75 | + sourced_media = await media_source.async_resolve_media(self.hass, image) |
| 76 | + sourced_media = media_source.async_process_play_media_url( |
| 77 | + self.hass, sourced_media.url |
| 78 | + ) |
| 79 | + |
| 80 | + data.update({"image": sourced_media}) |
| 81 | + |
| 82 | + payload = {"message": message, "title": title, "data": data} |
78 | 83 |
|
79 | 84 | _LOGGER.debug("Sending notification ..") |
80 | 85 |
|
81 | 86 | try: |
82 | 87 |
|
83 | | - response = requests.post( |
84 | | - self._resource, |
85 | | - json=payload, |
86 | | - timeout=10 |
| 88 | + response = await self.hass.async_add_executor_job( |
| 89 | + self.send, self._resource, payload |
87 | 90 | ) |
88 | 91 |
|
89 | 92 | _LOGGER.debug("Checking result ..") |
90 | 93 |
|
91 | 94 | if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR: |
92 | | - _LOGGER.error("Server error. Response %d: %s", response.status_code, response.reason) |
| 95 | + _LOGGER.error( |
| 96 | + "Server error. Response %d: %s", |
| 97 | + response.status_code, |
| 98 | + response.reason, |
| 99 | + ) |
93 | 100 | elif response.status_code == HTTPStatus.BAD_REQUEST: |
94 | | - _LOGGER.error("Client error (bad request). Response %d: %s", response.status_code, response.reason) |
| 101 | + _LOGGER.error( |
| 102 | + "Client error (bad request). Response %d: %s", |
| 103 | + response.status_code, |
| 104 | + response.reason, |
| 105 | + ) |
95 | 106 | elif response.status_code == HTTPStatus.NOT_FOUND: |
96 | | - _LOGGER.debug("Server error (not found). Response %d: %s", response.status_code, response.reason) |
| 107 | + _LOGGER.debug( |
| 108 | + "Server error (not found). Response %d: %s", |
| 109 | + response.status_code, |
| 110 | + response.reason, |
| 111 | + ) |
97 | 112 | elif response.status_code == HTTPStatus.METHOD_NOT_ALLOWED: |
98 | | - _LOGGER.error("Server error (method not allowed). Response %d", response.status_code) |
| 113 | + _LOGGER.error( |
| 114 | + "Server error (method not allowed). Response %d", |
| 115 | + response.status_code, |
| 116 | + ) |
99 | 117 | elif response.status_code == HTTPStatus.REQUEST_TIMEOUT: |
100 | | - _LOGGER.debug("Server error (request timeout). Response %d: %s", response.status_code, response.reason) |
| 118 | + _LOGGER.debug( |
| 119 | + "Server error (request timeout). Response %d: %s", |
| 120 | + response.status_code, |
| 121 | + response.reason, |
| 122 | + ) |
101 | 123 | elif response.status_code == HTTPStatus.NOT_IMPLEMENTED: |
102 | | - _LOGGER.error("Server error (not implemented). Response %d: %s", response.status_code, response.reason) |
| 124 | + _LOGGER.error( |
| 125 | + "Server error (not implemented). Response %d: %s", |
| 126 | + response.status_code, |
| 127 | + response.reason, |
| 128 | + ) |
103 | 129 | elif response.status_code == HTTPStatus.SERVICE_UNAVAILABLE: |
104 | | - _LOGGER.error("Server error (service unavailable). Response %d", response.status_code) |
| 130 | + _LOGGER.error( |
| 131 | + "Server error (service unavailable). Response %d", |
| 132 | + response.status_code, |
| 133 | + ) |
105 | 134 | elif response.status_code == HTTPStatus.GATEWAY_TIMEOUT: |
106 | | - _LOGGER.error("Network error (gateway timeout). Response %d: %s", response.status_code, response.reason) |
| 135 | + _LOGGER.error( |
| 136 | + "Network error (gateway timeout). Response %d: %s", |
| 137 | + response.status_code, |
| 138 | + response.reason, |
| 139 | + ) |
107 | 140 | elif response.status_code == HTTPStatus.OK: |
108 | | - _LOGGER.debug("Success. Response %d: %s", response.status_code, response.reason) |
| 141 | + _LOGGER.debug( |
| 142 | + "Success. Response %d: %s", response.status_code, response.reason |
| 143 | + ) |
109 | 144 | else: |
110 | | - _LOGGER.debug("Unknown response %d: %s", response.status_code, response.reason) |
| 145 | + _LOGGER.debug( |
| 146 | + "Unknown response %d: %s", response.status_code, response.reason |
| 147 | + ) |
111 | 148 |
|
112 | 149 | except Exception as e: |
113 | 150 | _LOGGER.debug("Error sending message: %s", e) |
114 | | - |
|
0 commit comments