From 3f997aefc1579664b76f8395d3682a579ab7adf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Fri, 1 Feb 2019 19:42:45 +0200 Subject: [PATCH] Add huawei_lte notify component (#19544) * Add huawei_lte notify component * Use CONF_RECIPIENT instead of ATTR_TARGET in config --- homeassistant/components/notify/huawei_lte.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 homeassistant/components/notify/huawei_lte.py diff --git a/homeassistant/components/notify/huawei_lte.py b/homeassistant/components/notify/huawei_lte.py new file mode 100644 index 00000000000..a406a7ec2d8 --- /dev/null +++ b/homeassistant/components/notify/huawei_lte.py @@ -0,0 +1,60 @@ +"""Huawei LTE router platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.huawei_lte/ +""" + +import logging + +import voluptuous as vol +import attr + +from homeassistant.components.notify import ( + BaseNotificationService, ATTR_TARGET, PLATFORM_SCHEMA) +from homeassistant.const import CONF_RECIPIENT, CONF_URL +import homeassistant.helpers.config_validation as cv + +from ..huawei_lte import DATA_KEY + + +DEPENDENCIES = ['huawei_lte'] + +_LOGGER = logging.getLogger(__name__) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_URL): cv.url, + vol.Required(CONF_RECIPIENT): vol.All(cv.ensure_list, [cv.string]), +}) + + +async def async_get_service(hass, config, discovery_info=None): + """Get the notification service.""" + return HuaweiLteSmsNotificationService(hass, config) + + +@attr.s +class HuaweiLteSmsNotificationService(BaseNotificationService): + """Huawei LTE router SMS notification service.""" + + hass = attr.ib() + config = attr.ib() + + def send_message(self, message="", **kwargs): + """Send message to target numbers.""" + from huawei_lte_api.exceptions import ResponseErrorException + + targets = kwargs.get(ATTR_TARGET, self.config.get(CONF_RECIPIENT)) + if not targets or not message: + return + + data = self.hass.data[DATA_KEY].get_data(self.config) + if not data: + _LOGGER.error("Router not available") + return + + try: + resp = data.client.sms.send_sms( + phone_numbers=targets, message=message) + _LOGGER.debug("Sent to %s: %s", targets, resp) + except ResponseErrorException as ex: + _LOGGER.error("Could not send to %s: %s", targets, ex)