2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Huawei LTE router notifications."""
|
2019-08-26 08:32:50 +00:00
|
|
|
|
2019-02-01 17:42:45 +00:00
|
|
|
import logging
|
2020-02-12 01:54:19 +00:00
|
|
|
import time
|
2019-10-24 16:31:49 +00:00
|
|
|
from typing import Any, List
|
2019-02-01 17:42:45 +00:00
|
|
|
|
|
|
|
import attr
|
2019-10-24 16:31:49 +00:00
|
|
|
from huawei_lte_api.exceptions import ResponseErrorException
|
2019-02-01 17:42:45 +00:00
|
|
|
|
2019-12-09 18:06:25 +00:00
|
|
|
from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
|
2019-02-01 17:42:45 +00:00
|
|
|
from homeassistant.const import CONF_RECIPIENT, CONF_URL
|
|
|
|
|
2019-10-24 16:31:49 +00:00
|
|
|
from . import Router
|
2019-08-26 08:32:50 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
2019-02-01 17:42:45 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_service(hass, config, discovery_info=None):
|
|
|
|
"""Get the notification service."""
|
2019-10-24 16:31:49 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Loading as a platform is no longer supported, convert to use "
|
|
|
|
"config entries or the huawei_lte component"
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
router = hass.data[DOMAIN].routers[discovery_info[CONF_URL]]
|
|
|
|
default_targets = discovery_info[CONF_RECIPIENT] or []
|
|
|
|
|
|
|
|
return HuaweiLteSmsNotificationService(router, default_targets)
|
2019-02-01 17:42:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class HuaweiLteSmsNotificationService(BaseNotificationService):
|
|
|
|
"""Huawei LTE router SMS notification service."""
|
|
|
|
|
2019-10-24 16:31:49 +00:00
|
|
|
router: Router = attr.ib()
|
|
|
|
default_targets: List[str] = attr.ib()
|
2019-02-01 17:42:45 +00:00
|
|
|
|
2019-10-24 16:31:49 +00:00
|
|
|
def send_message(self, message: str = "", **kwargs: Any) -> None:
|
2019-02-01 17:42:45 +00:00
|
|
|
"""Send message to target numbers."""
|
|
|
|
|
2019-10-24 16:31:49 +00:00
|
|
|
targets = kwargs.get(ATTR_TARGET, self.default_targets)
|
2019-02-01 17:42:45 +00:00
|
|
|
if not targets or not message:
|
|
|
|
return
|
|
|
|
|
2019-12-25 11:43:51 +00:00
|
|
|
if self.router.suspended:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Integration suspended, not sending notification to %s", targets
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2019-02-01 17:42:45 +00:00
|
|
|
try:
|
2019-10-24 16:31:49 +00:00
|
|
|
resp = self.router.client.sms.send_sms(
|
|
|
|
phone_numbers=targets, message=message
|
|
|
|
)
|
2019-02-01 17:42:45 +00:00
|
|
|
_LOGGER.debug("Sent to %s: %s", targets, resp)
|
|
|
|
except ResponseErrorException as ex:
|
|
|
|
_LOGGER.error("Could not send to %s: %s", targets, ex)
|
2020-02-12 01:54:19 +00:00
|
|
|
finally:
|
|
|
|
self.router.notify_last_attempt = time.monotonic()
|