Update eternalegypt (#15180)

* Update eternalegypt to 0.0.2

* Share websession

* Renames
This commit is contained in:
Anders Melchiorsen 2018-06-29 23:25:49 +02:00 committed by Paulus Schoutsen
parent bbbec5a056
commit 66479dc2e5
4 changed files with 48 additions and 32 deletions

View File

@ -9,12 +9,15 @@ from datetime import timedelta
import voluptuous as vol
import attr
import aiohttp
from homeassistant.const import CONF_HOST, CONF_PASSWORD
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.util import Throttle
REQUIREMENTS = ['eternalegypt==0.0.1']
REQUIREMENTS = ['eternalegypt==0.0.2']
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
@ -30,33 +33,34 @@ CONFIG_SCHEMA = vol.Schema({
@attr.s
class LTEData:
"""Class for LTE state."""
class ModemData:
"""Class for modem state."""
eternalegypt = attr.ib()
modem = attr.ib()
unread_count = attr.ib(init=False)
usage = attr.ib(init=False)
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Call the API to update the data."""
information = await self.eternalegypt.information()
information = await self.modem.information()
self.unread_count = sum(1 for x in information.sms if x.unread)
self.usage = information.usage
@attr.s
class LTEHostData:
"""Container for LTE states."""
class LTEData:
"""Shared state."""
hostdata = attr.ib(init=False, factory=dict)
websession = attr.ib()
modem_data = attr.ib(init=False, factory=dict)
def get(self, config):
"""Get the requested or the only hostdata value."""
def get_modem_data(self, config):
"""Get the requested or the only modem_data value."""
if CONF_HOST in config:
return self.hostdata.get(config[CONF_HOST])
elif len(self.hostdata) == 1:
return next(iter(self.hostdata.values()))
return self.modem_data.get(config[CONF_HOST])
elif len(self.modem_data) == 1:
return next(iter(self.modem_data.values()))
return None
@ -64,7 +68,9 @@ class LTEHostData:
async def async_setup(hass, config):
"""Set up Netgear LTE component."""
if DATA_KEY not in hass.data:
hass.data[DATA_KEY] = LTEHostData()
websession = async_create_clientsession(
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:
@ -80,7 +86,17 @@ async def _setup_lte(hass, lte_config):
host = lte_config[CONF_HOST]
password = lte_config[CONF_PASSWORD]
eternalegypt = eternalegypt.LB2120(host, password)
lte_data = LTEData(eternalegypt)
await lte_data.async_update()
hass.data[DATA_KEY].hostdata[host] = lte_data
websession = hass.data[DATA_KEY].websession
modem = eternalegypt.Modem(hostname=host, websession=websession)
await modem.login(password=password)
modem_data = ModemData(modem)
await modem_data.async_update()
hass.data[DATA_KEY].modem_data[host] = modem_data
async def cleanup(event):
"""Clean up resources."""
await modem.logout()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup)

View File

@ -25,16 +25,16 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
async def async_get_service(hass, config, discovery_info=None):
"""Get the notification service."""
lte_data = hass.data[DATA_KEY].get(config)
modem_data = hass.data[DATA_KEY].get_modem_data(config)
phone = config.get(ATTR_TARGET)
return NetgearNotifyService(lte_data, phone)
return NetgearNotifyService(modem_data, phone)
@attr.s
class NetgearNotifyService(BaseNotificationService):
"""Implementation of a notification service."""
lte_data = attr.ib()
modem_data = attr.ib()
phone = attr.ib()
async def async_send_message(self, message="", **kwargs):
@ -42,4 +42,4 @@ class NetgearNotifyService(BaseNotificationService):
targets = kwargs.get(ATTR_TARGET, self.phone)
if targets and message:
for target in targets:
await self.lte_data.eternalegypt.sms(target, message)
await self.modem_data.modem.sms(target, message)

View File

@ -29,14 +29,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
async def async_setup_platform(
hass, config, async_add_devices, discovery_info):
"""Set up Netgear LTE sensor devices."""
lte_data = hass.data[DATA_KEY].get(config)
modem_data = hass.data[DATA_KEY].get_modem_data(config)
sensors = []
for sensortype in config[CONF_SENSORS]:
if sensortype == SENSOR_SMS:
sensors.append(SMSSensor(lte_data))
sensors.append(SMSSensor(modem_data))
elif sensortype == SENSOR_USAGE:
sensors.append(UsageSensor(lte_data))
sensors.append(UsageSensor(modem_data))
async_add_devices(sensors, True)
@ -45,11 +45,11 @@ async def async_setup_platform(
class LTESensor(Entity):
"""Data usage sensor entity."""
lte_data = attr.ib()
modem_data = attr.ib()
async def async_update(self):
"""Update state."""
await self.lte_data.async_update()
await self.modem_data.async_update()
class SMSSensor(LTESensor):
@ -63,7 +63,7 @@ class SMSSensor(LTESensor):
@property
def state(self):
"""Return the state of the sensor."""
return self.lte_data.unread_count
return self.modem_data.unread_count
class UsageSensor(LTESensor):
@ -82,4 +82,4 @@ class UsageSensor(LTESensor):
@property
def state(self):
"""Return the state of the sensor."""
return round(self.lte_data.usage / 1024**2, 1)
return round(self.modem_data.usage / 1024**2, 1)

View File

@ -309,7 +309,7 @@ ephem==3.7.6.0
epson-projector==0.1.3
# homeassistant.components.netgear_lte
eternalegypt==0.0.1
eternalegypt==0.0.2
# homeassistant.components.keyboard_remote
# evdev==0.6.1