From e4b288ef7cb49be63fd45ac7714851cfc44bd11e Mon Sep 17 00:00:00 2001 From: Oscar Calvo <2091582+ocalvo@users.noreply.github.com> Date: Mon, 22 Aug 2022 12:03:57 -0600 Subject: [PATCH] Load sms notify via discovery (#76733) * Fix #76283 Fix #76283 * Update notify.py * Support sending SMS as ANSI * Put back load via discovery * Update homeassistant/components/sms/const.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/__init__.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/__init__.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Fix typo * Apply PR feedback * Fix bad reference * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Apply PR feedback * Add back schema * Update homeassistant/components/sms/notify.py Co-authored-by: Martin Hjelmare * Fix pylint * Remove platform schema * Fix after merge Co-authored-by: Martin Hjelmare --- homeassistant/components/sms/__init__.py | 33 +++++++++++++----------- homeassistant/components/sms/const.py | 1 + homeassistant/components/sms/gateway.py | 9 +++++-- homeassistant/components/sms/notify.py | 27 ++++++++----------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/sms/__init__.py b/homeassistant/components/sms/__init__.py index 83d4bbc31f3..27cb7ac034d 100644 --- a/homeassistant/components/sms/__init__.py +++ b/homeassistant/components/sms/__init__.py @@ -6,10 +6,11 @@ import async_timeout import gammu # pylint: disable=import-error import voluptuous as vol -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_DEVICE, Platform +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_DEVICE, CONF_NAME, Platform from homeassistant.core import HomeAssistant -from homeassistant.helpers import config_validation as cv +from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -19,6 +20,7 @@ from .const import ( DEFAULT_SCAN_INTERVAL, DOMAIN, GATEWAY, + HASS_CONFIG, NETWORK_COORDINATOR, SIGNAL_COORDINATOR, SMS_GATEWAY, @@ -49,17 +51,7 @@ _LOGGER = logging.getLogger(__name__) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Configure Gammu state machine.""" hass.data.setdefault(DOMAIN, {}) - if not (sms_config := config.get(DOMAIN, {})): - return True - - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data=sms_config, - ) - ) - + hass.data[HASS_CONFIG] = config return True @@ -75,7 +67,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: _LOGGER.debug("Connecting mode:%s", connection_mode) gateway = await create_sms_gateway(config, hass) if not gateway: - return False + raise ConfigEntryNotReady(f"Cannot find device {device}") signal_coordinator = SignalCoordinator(hass, gateway) network_coordinator = NetworkCoordinator(hass, gateway) @@ -93,6 +85,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + # set up notify platform, no entry support for notify component yet, + # have to use discovery to load platform. + hass.async_create_task( + discovery.async_load_platform( + hass, + Platform.NOTIFY, + DOMAIN, + {CONF_NAME: DOMAIN}, + hass.data[HASS_CONFIG], + ) + ) return True diff --git a/homeassistant/components/sms/const.py b/homeassistant/components/sms/const.py index 841c4bd8f89..d055894f402 100644 --- a/homeassistant/components/sms/const.py +++ b/homeassistant/components/sms/const.py @@ -7,6 +7,7 @@ from homeassistant.helpers.entity import EntityCategory DOMAIN = "sms" SMS_GATEWAY = "SMS_GATEWAY" +HASS_CONFIG = "sms_hass_config" SMS_STATE_UNREAD = "UnRead" SIGNAL_COORDINATOR = "signal_coordinator" NETWORK_COORDINATOR = "network_coordinator" diff --git a/homeassistant/components/sms/gateway.py b/homeassistant/components/sms/gateway.py index c469e688737..7a2f095abd1 100644 --- a/homeassistant/components/sms/gateway.py +++ b/homeassistant/components/sms/gateway.py @@ -167,8 +167,13 @@ async def create_sms_gateway(config, hass): """Create the sms gateway.""" try: gateway = Gateway(config, hass) - await gateway.init_async() + try: + await gateway.init_async() + except gammu.GSMError as exc: + _LOGGER.error("Failed to initialize, error %s", exc) + await gateway.terminate_async() + return None return gateway except gammu.GSMError as exc: - _LOGGER.error("Failed to initialize, error %s", exc) + _LOGGER.error("Failed to create async worker, error %s", exc) return None diff --git a/homeassistant/components/sms/notify.py b/homeassistant/components/sms/notify.py index d82e4951c00..13f9cfc9f72 100644 --- a/homeassistant/components/sms/notify.py +++ b/homeassistant/components/sms/notify.py @@ -2,40 +2,31 @@ import logging import gammu # pylint: disable=import-error -import voluptuous as vol -from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationService -from homeassistant.const import CONF_NAME, CONF_RECIPIENT, CONF_TARGET -import homeassistant.helpers.config_validation as cv +from homeassistant.components.notify import BaseNotificationService +from homeassistant.const import CONF_TARGET from .const import CONF_UNICODE, DOMAIN, GATEWAY, SMS_GATEWAY _LOGGER = logging.getLogger(__name__) -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( - {vol.Required(CONF_RECIPIENT): cv.string, vol.Optional(CONF_NAME): cv.string} -) - -def get_service(hass, config, discovery_info=None): +async def async_get_service(hass, config, discovery_info=None): """Get the SMS notification service.""" if discovery_info is None: - number = config[CONF_RECIPIENT] - else: - number = discovery_info[CONF_RECIPIENT] + return None - return SMSNotificationService(hass, number) + return SMSNotificationService(hass) class SMSNotificationService(BaseNotificationService): """Implement the notification service for SMS.""" - def __init__(self, hass, number): + def __init__(self, hass): """Initialize the service.""" self.hass = hass - self.number = number async def async_send_message(self, message="", **kwargs): """Send SMS message.""" @@ -46,7 +37,11 @@ class SMSNotificationService(BaseNotificationService): gateway = self.hass.data[DOMAIN][SMS_GATEWAY][GATEWAY] - targets = kwargs.get(CONF_TARGET, [self.number]) + targets = kwargs.get(CONF_TARGET) + if targets is None: + _LOGGER.error("No target number specified, cannot send message") + return + is_unicode = kwargs.get(CONF_UNICODE, True) smsinfo = { "Class": -1,