Incoming SMS handling for netgear_lte (#22402)
* Fire netgear_lte events for incoming SMS * Add netgear_lte.delete_sms service call * Fix log statement * Add services.yamlpull/22406/head
parent
8bf5e57b7f
commit
a1369c2fee
|
@ -30,6 +30,15 @@ DISPATCHER_NETGEAR_LTE = 'netgear_lte_update'
|
|||
DOMAIN = 'netgear_lte'
|
||||
DATA_KEY = 'netgear_lte'
|
||||
|
||||
EVENT_SMS = 'netgear_lte_sms'
|
||||
|
||||
SERVICE_DELETE_SMS = 'delete_sms'
|
||||
|
||||
ATTR_HOST = 'host'
|
||||
ATTR_SMS_ID = 'sms_id'
|
||||
ATTR_FROM = 'from'
|
||||
ATTR_MESSAGE = 'message'
|
||||
|
||||
|
||||
NOTIFY_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_NAME, default=DOMAIN): cv.string,
|
||||
|
@ -53,6 +62,11 @@ CONFIG_SCHEMA = vol.Schema({
|
|||
})])
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
DELETE_SMS_SCHEMA = vol.Schema({
|
||||
vol.Required(ATTR_HOST): cv.string,
|
||||
vol.Required(ATTR_SMS_ID): vol.All(cv.ensure_list, [cv.positive_int]),
|
||||
})
|
||||
|
||||
|
||||
@attr.s
|
||||
class ModemData:
|
||||
|
@ -101,6 +115,24 @@ async def async_setup(hass, config):
|
|||
hass, cookie_jar=aiohttp.CookieJar(unsafe=True))
|
||||
hass.data[DATA_KEY] = LTEData(websession)
|
||||
|
||||
async def delete_sms_handler(service):
|
||||
"""Apply a service."""
|
||||
host = service.data[ATTR_HOST]
|
||||
conf = {CONF_HOST: host}
|
||||
modem_data = hass.data[DATA_KEY].get_modem_data(conf)
|
||||
|
||||
if not modem_data:
|
||||
_LOGGER.error(
|
||||
"%s: host %s unavailable", SERVICE_DELETE_SMS, host)
|
||||
return
|
||||
|
||||
for sms_id in service.data[ATTR_SMS_ID]:
|
||||
await modem_data.modem.delete_sms(sms_id)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_DELETE_SMS, delete_sms_handler,
|
||||
schema=DELETE_SMS_SCHEMA)
|
||||
|
||||
netgear_lte_config = config[DOMAIN]
|
||||
|
||||
# Set up each modem
|
||||
|
@ -161,6 +193,19 @@ async def _setup_lte(hass, lte_config):
|
|||
async def _login(hass, modem_data, password):
|
||||
"""Log in and complete setup."""
|
||||
await modem_data.modem.login(password=password)
|
||||
|
||||
def fire_sms_event(sms):
|
||||
"""Send an SMS event."""
|
||||
data = {
|
||||
ATTR_HOST: modem_data.host,
|
||||
ATTR_SMS_ID: sms.id,
|
||||
ATTR_FROM: sms.sender,
|
||||
ATTR_MESSAGE: sms.message,
|
||||
}
|
||||
hass.bus.async_fire(EVENT_SMS, data)
|
||||
|
||||
await modem_data.modem.add_sms_listener(fire_sms_event)
|
||||
|
||||
await modem_data.async_update()
|
||||
hass.data[DATA_KEY].modem_data[modem_data.host] = modem_data
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
delete_sms:
|
||||
description: Delete messages from the modem inbox.
|
||||
fields:
|
||||
host:
|
||||
description: The modem that should have a message deleted.
|
||||
example: 192.168.5.1
|
||||
sms_id:
|
||||
description: Integer or list of integers with inbox IDs of messages to delete.
|
||||
example: 7
|
Loading…
Reference in New Issue