Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate the netgear_lte configuration #22105

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 48 additions & 12 deletions homeassistant/components/netgear_lte/__init__.py
Expand Up @@ -8,9 +8,12 @@
import voluptuous as vol

from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP)
CONF_HOST, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_PASSWORD,
CONF_RECIPIENT, EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.util import Throttle

Expand All @@ -23,10 +26,25 @@
DOMAIN = 'netgear_lte'
DATA_KEY = 'netgear_lte'


NOTIFY_SCHEMA = vol.Schema({
vol.Optional(CONF_NAME, default=DOMAIN): cv.string,
vol.Optional(CONF_RECIPIENT, default=[]):
vol.All(cv.ensure_list, [cv.string]),
})

SENSOR_SCHEMA = vol.Schema({
vol.Required(CONF_MONITORED_CONDITIONS):
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
vol.All(cv.ensure_list, [cv.string]),
amelchio marked this conversation as resolved.
Show resolved Hide resolved
})

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.All(cv.ensure_list, [vol.Schema({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(NOTIFY_DOMAIN, default=NOTIFY_SCHEMA({})):
amelchio marked this conversation as resolved.
Show resolved Hide resolved
vol.All(cv.ensure_list, [NOTIFY_SCHEMA]),
vol.Optional(SENSOR_DOMAIN): SENSOR_SCHEMA,
})])
}, extra=vol.ALLOW_EXTRA)

Expand Down Expand Up @@ -71,13 +89,8 @@ class LTEData:
modem_data = attr.ib(init=False, factory=dict)

def get_modem_data(self, config):
"""Get the requested or the only modem_data value."""
if CONF_HOST in config:
return self.modem_data.get(config[CONF_HOST])
if len(self.modem_data) == 1:
return next(iter(self.modem_data.values()))

return None
"""Get modem_data for the host in config."""
return self.modem_data.get(config[CONF_HOST])


async def async_setup(hass, config):
Expand All @@ -87,9 +100,32 @@ async def async_setup(hass, config):
hass, cookie_jar=aiohttp.CookieJar(unsafe=True))
hass.data[DATA_KEY] = LTEData(websession)

tasks = [_setup_lte(hass, conf) for conf in config.get(DOMAIN, [])]
if tasks:
await asyncio.wait(tasks)
netgear_lte_config = config[DOMAIN]

# Set up each modem
tasks = [_setup_lte(hass, lte_conf) for lte_conf in netgear_lte_config]
amelchio marked this conversation as resolved.
Show resolved Hide resolved
await asyncio.wait(tasks)

# Load platforms for each modem
for lte_conf in netgear_lte_config:
# Notify
for notify_conf in lte_conf[NOTIFY_DOMAIN]:
discovery_info = {
CONF_HOST: lte_conf[CONF_HOST],
CONF_NAME: notify_conf.get(CONF_NAME),
NOTIFY_DOMAIN: notify_conf,
}
hass.async_create_task(discovery.async_load_platform(
hass, NOTIFY_DOMAIN, DOMAIN, discovery_info, config))

# Sensor
sensor_conf = lte_conf.get(SENSOR_DOMAIN)
discovery_info = {
CONF_HOST: lte_conf[CONF_HOST],
SENSOR_DOMAIN: sensor_conf,
}
hass.async_create_task(discovery.async_load_platform(
hass, SENSOR_DOMAIN, DOMAIN, discovery_info, config))

return True

Expand Down
43 changes: 22 additions & 21 deletions homeassistant/components/netgear_lte/notify.py
Expand Up @@ -2,28 +2,23 @@
import logging

import attr
import voluptuous as vol

from homeassistant.components.notify import (
ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import CONF_HOST
import homeassistant.helpers.config_validation as cv
ATTR_TARGET, BaseNotificationService, DOMAIN)

from ..netgear_lte import DATA_KEY
from ..netgear_lte import CONF_RECIPIENT, DATA_KEY
amelchio marked this conversation as resolved.
Show resolved Hide resolved

DEPENDENCIES = ['netgear_lte']

_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HOST): cv.string,
vol.Required(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
})


async def async_get_service(hass, config, discovery_info=None):
"""Get the notification service."""
return NetgearNotifyService(hass, config)
if discovery_info is None:
return

return NetgearNotifyService(hass, discovery_info)


@attr.s
Expand All @@ -35,17 +30,23 @@ class NetgearNotifyService(BaseNotificationService):

async def async_send_message(self, message="", **kwargs):
"""Send a message to a user."""
import eternalegypt

modem_data = self.hass.data[DATA_KEY].get_modem_data(self.config)
if not modem_data:
_LOGGER.error("No modem available")
_LOGGER.error("Modem not ready")
return

targets = kwargs.get(ATTR_TARGET, self.config[DOMAIN][CONF_RECIPIENT])
if not targets:
_LOGGER.warning("No recipients")
return

if not message:
return

phone = self.config.get(ATTR_TARGET)
targets = kwargs.get(ATTR_TARGET, phone)
if targets and message:
for target in targets:
import eternalegypt
try:
await modem_data.modem.sms(target, message)
except eternalegypt.Error:
_LOGGER.error("Unable to send to %s", target)
for target in targets:
try:
await modem_data.modem.sms(target, message)
except eternalegypt.Error:
_LOGGER.error("Unable to send to %s", target)
32 changes: 20 additions & 12 deletions homeassistant/components/netgear_lte/sensor.py
@@ -1,41 +1,49 @@
"""Support for Netgear LTE sensors."""
import logging

import attr
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_HOST, CONF_SENSORS
from homeassistant.components.sensor import DOMAIN
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity

from ..netgear_lte import DATA_KEY
from ..netgear_lte import CONF_MONITORED_CONDITIONS, DATA_KEY

DEPENDENCIES = ['netgear_lte']

_LOGGER = logging.getLogger(__name__)

SENSOR_SMS = 'sms'
SENSOR_USAGE = 'usage'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HOST): cv.string,
vol.Required(CONF_SENSORS): vol.All(
cv.ensure_list, [vol.In([SENSOR_SMS, SENSOR_USAGE])]),
})
DEFAULT_SENSORS = [SENSOR_SMS, SENSOR_USAGE]


async def async_setup_platform(
hass, config, async_add_entities, discovery_info):
"""Set up Netgear LTE sensor devices."""
modem_data = hass.data[DATA_KEY].get_modem_data(config)
if discovery_info is None:
return

modem_data = hass.data[DATA_KEY].get_modem_data(discovery_info)

if not modem_data:
raise PlatformNotReady

sensor_conf = discovery_info[DOMAIN]
if sensor_conf:
monitored_conditions = sensor_conf[CONF_MONITORED_CONDITIONS]
else:
monitored_conditions = DEFAULT_SENSORS
amelchio marked this conversation as resolved.
Show resolved Hide resolved

sensors = []
for sensor_type in config[CONF_SENSORS]:
for sensor_type in monitored_conditions:
if sensor_type == SENSOR_SMS:
sensors.append(SMSSensor(modem_data, sensor_type))
elif sensor_type == SENSOR_USAGE:
sensors.append(UsageSensor(modem_data, sensor_type))
else:
_LOGGER.error("Unknown sensor type '%s'", sensor_type)

async_add_entities(sensors, True)

Expand Down