2019-08-02 21:20:07 +00:00
|
|
|
"""Support for Netgear LTE notifications."""
|
2024-03-08 14:04:07 +00:00
|
|
|
|
2023-01-26 15:54:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-06-18 07:12:02 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2019-10-17 13:03:05 +00:00
|
|
|
import eternalegypt
|
2024-06-18 07:12:02 +00:00
|
|
|
from eternalegypt.eternalegypt import Modem
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2022-01-10 14:13:30 +00:00
|
|
|
from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
|
2023-05-11 15:49:11 +00:00
|
|
|
from homeassistant.const import CONF_RECIPIENT
|
2023-01-26 15:54:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2024-06-18 07:12:02 +00:00
|
|
|
from .const import CONF_NOTIFY, LOGGER
|
2018-09-23 16:58:09 +00:00
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2023-01-26 15:54:57 +00:00
|
|
|
async def async_get_service(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> NetgearNotifyService | None:
|
2018-06-08 05:46:34 +00:00
|
|
|
"""Get the notification service."""
|
2019-03-22 13:43:39 +00:00
|
|
|
if discovery_info is None:
|
2023-01-26 15:54:57 +00:00
|
|
|
return None
|
2019-03-22 13:43:39 +00:00
|
|
|
|
2024-06-18 07:12:02 +00:00
|
|
|
return NetgearNotifyService(config, discovery_info)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NetgearNotifyService(BaseNotificationService):
|
|
|
|
"""Implementation of a notification service."""
|
|
|
|
|
2024-06-18 07:12:02 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: ConfigType,
|
|
|
|
discovery_info: dict[str, Any],
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the service."""
|
|
|
|
self.config = config
|
|
|
|
self.modem: Modem = discovery_info["modem"]
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
async def async_send_message(self, message="", **kwargs):
|
|
|
|
"""Send a message to a user."""
|
2019-03-22 13:43:39 +00:00
|
|
|
|
2024-06-18 07:12:02 +00:00
|
|
|
if not self.modem.token:
|
2023-05-11 15:49:11 +00:00
|
|
|
LOGGER.error("Modem not ready")
|
2019-03-22 13:43:39 +00:00
|
|
|
return
|
2023-12-26 04:19:28 +00:00
|
|
|
if not (targets := kwargs.get(ATTR_TARGET)):
|
|
|
|
targets = self.config[CONF_NOTIFY][CONF_RECIPIENT]
|
2019-03-22 13:43:39 +00:00
|
|
|
if not targets:
|
2023-05-11 15:49:11 +00:00
|
|
|
LOGGER.warning("No recipients")
|
2019-03-22 13:43:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not message:
|
2018-09-23 16:58:09 +00:00
|
|
|
return
|
|
|
|
|
2019-03-22 13:43:39 +00:00
|
|
|
for target in targets:
|
|
|
|
try:
|
2024-06-18 07:12:02 +00:00
|
|
|
await self.modem.sms(target, message)
|
2019-03-22 13:43:39 +00:00
|
|
|
except eternalegypt.Error:
|
2023-05-11 15:49:11 +00:00
|
|
|
LOGGER.error("Unable to send to %s", target)
|