2019-02-14 04:35:12 +00:00
|
|
|
"""MySensors notification service."""
|
2021-06-07 14:04:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
from homeassistant.components import mysensors
|
2022-01-08 19:55:24 +00:00
|
|
|
from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
|
|
|
|
from homeassistant.const import Platform
|
2021-06-07 14:04:04 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
|
|
from .const import DevId, DiscoveryInfo
|
2017-01-15 02:53:14 +00:00
|
|
|
|
|
|
|
|
2021-06-07 14:04:04 +00:00
|
|
|
async def async_get_service(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: dict[str, Any],
|
|
|
|
discovery_info: DiscoveryInfo | None = None,
|
|
|
|
) -> BaseNotificationService | None:
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Get the MySensors notification service."""
|
2021-03-06 17:33:55 +00:00
|
|
|
if not discovery_info:
|
|
|
|
return None
|
|
|
|
|
2017-08-25 15:58:05 +00:00
|
|
|
new_devices = mysensors.setup_mysensors_platform(
|
2022-01-08 19:55:24 +00:00
|
|
|
hass, Platform.NOTIFY, discovery_info, MySensorsNotificationDevice
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-08-25 15:58:05 +00:00
|
|
|
if not new_devices:
|
2018-04-01 15:36:26 +00:00
|
|
|
return None
|
2017-08-25 15:58:05 +00:00
|
|
|
return MySensorsNotificationService(hass)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
|
|
|
|
2018-06-25 11:58:16 +00:00
|
|
|
class MySensorsNotificationDevice(mysensors.device.MySensorsDevice):
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Represent a MySensors Notification device."""
|
|
|
|
|
2021-06-07 14:04:04 +00:00
|
|
|
def send_msg(self, msg: str) -> None:
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Send a message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
for sub_msg in [msg[i : i + 25] for i in range(0, len(msg), 25)]:
|
2017-01-15 02:53:14 +00:00
|
|
|
# Max mysensors payload is 25 bytes.
|
|
|
|
self.gateway.set_child_value(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.node_id, self.child_id, self.value_type, sub_msg
|
|
|
|
)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2021-06-07 14:04:04 +00:00
|
|
|
def __repr__(self) -> str:
|
2017-08-25 15:58:05 +00:00
|
|
|
"""Return the representation."""
|
2019-09-03 19:14:00 +00:00
|
|
|
return f"<MySensorsNotificationDevice {self.name}>"
|
2017-08-25 15:58:05 +00:00
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
|
|
|
|
class MySensorsNotificationService(BaseNotificationService):
|
2017-08-25 15:58:05 +00:00
|
|
|
"""Implement a MySensors notification service."""
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2021-06-07 14:04:04 +00:00
|
|
|
def __init__(self, hass: HomeAssistant) -> None:
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Initialize the service."""
|
2021-06-07 14:04:04 +00:00
|
|
|
self.devices: dict[
|
|
|
|
DevId, MySensorsNotificationDevice
|
|
|
|
] = mysensors.get_mysensors_devices(
|
2022-01-08 19:55:24 +00:00
|
|
|
hass, Platform.NOTIFY
|
2021-06-07 14:04:04 +00:00
|
|
|
) # type: ignore[assignment]
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2021-06-07 14:04:04 +00:00
|
|
|
async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Send a message to a user."""
|
|
|
|
target_devices = kwargs.get(ATTR_TARGET)
|
2019-07-31 19:25:30 +00:00
|
|
|
devices = [
|
|
|
|
device
|
|
|
|
for device in self.devices.values()
|
|
|
|
if target_devices is None or device.name in target_devices
|
|
|
|
]
|
2017-01-15 02:53:14 +00:00
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
device.send_msg(message)
|