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 <marhje52@gmail.com>

* Update homeassistant/components/sms/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix typo

* Apply PR feedback

* Fix bad reference

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Apply PR feedback

* Add back schema

* Update homeassistant/components/sms/notify.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix pylint

* Remove platform schema

* Fix after merge

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Oscar Calvo 2022-08-22 12:03:57 -06:00 committed by GitHub
parent 3938015c93
commit e4b288ef7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 33 deletions

View File

@ -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

View File

@ -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"

View File

@ -167,8 +167,13 @@ async def create_sms_gateway(config, hass):
"""Create the sms gateway."""
try:
gateway = Gateway(config, hass)
try:
await gateway.init_async()
return gateway
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 create async worker, error %s", exc)
return None

View File

@ -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,