mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use dispatcher for netgear_lte state updates (#22328)
* Use dispatcher for netgear_lte state updates * Also dispatch unavailable state
This commit is contained in:
parent
7519e8d417
commit
afa99c9189
@ -15,7 +15,8 @@ from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
|
|||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
from homeassistant.helpers import config_validation as cv, discovery
|
from homeassistant.helpers import config_validation as cv, discovery
|
||||||
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
|
|
||||||
from . import sensor_types
|
from . import sensor_types
|
||||||
|
|
||||||
@ -23,7 +24,8 @@ REQUIREMENTS = ['eternalegypt==0.0.5']
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
|
SCAN_INTERVAL = timedelta(seconds=10)
|
||||||
|
DISPATCHER_NETGEAR_LTE = 'netgear_lte_update'
|
||||||
|
|
||||||
DOMAIN = 'netgear_lte'
|
DOMAIN = 'netgear_lte'
|
||||||
DATA_KEY = 'netgear_lte'
|
DATA_KEY = 'netgear_lte'
|
||||||
@ -56,6 +58,7 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
class ModemData:
|
class ModemData:
|
||||||
"""Class for modem state."""
|
"""Class for modem state."""
|
||||||
|
|
||||||
|
hass = attr.ib()
|
||||||
host = attr.ib()
|
host = attr.ib()
|
||||||
modem = attr.ib()
|
modem = attr.ib()
|
||||||
|
|
||||||
@ -64,7 +67,6 @@ class ModemData:
|
|||||||
usage = attr.ib(init=False, default=None)
|
usage = attr.ib(init=False, default=None)
|
||||||
connected = attr.ib(init=False, default=True)
|
connected = attr.ib(init=False, default=True)
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Call the API to update the data."""
|
"""Call the API to update the data."""
|
||||||
import eternalegypt
|
import eternalegypt
|
||||||
@ -83,6 +85,8 @@ class ModemData:
|
|||||||
self.unread_count = None
|
self.unread_count = None
|
||||||
self.usage = None
|
self.usage = None
|
||||||
|
|
||||||
|
async_dispatcher_send(self.hass, DISPATCHER_NETGEAR_LTE)
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class LTEData:
|
class LTEData:
|
||||||
@ -143,7 +147,7 @@ async def _setup_lte(hass, lte_config):
|
|||||||
websession = hass.data[DATA_KEY].websession
|
websession = hass.data[DATA_KEY].websession
|
||||||
modem = eternalegypt.Modem(hostname=host, websession=websession)
|
modem = eternalegypt.Modem(hostname=host, websession=websession)
|
||||||
|
|
||||||
modem_data = ModemData(host, modem)
|
modem_data = ModemData(hass, host, modem)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await _login(hass, modem_data, password)
|
await _login(hass, modem_data, password)
|
||||||
@ -172,6 +176,12 @@ async def _login(hass, modem_data, password):
|
|||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup)
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cleanup)
|
||||||
|
|
||||||
|
async def _update(now):
|
||||||
|
"""Periodic update."""
|
||||||
|
await modem_data.async_update()
|
||||||
|
|
||||||
|
async_track_time_interval(hass, _update, SCAN_INTERVAL)
|
||||||
|
|
||||||
|
|
||||||
async def _retry_login(hass, modem_data, password):
|
async def _retry_login(hass, modem_data, password):
|
||||||
"""Sleep and retry setup."""
|
"""Sleep and retry setup."""
|
||||||
|
@ -6,8 +6,9 @@ import attr
|
|||||||
from homeassistant.components.sensor import DOMAIN
|
from homeassistant.components.sensor import DOMAIN
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
|
||||||
from . import CONF_MONITORED_CONDITIONS, DATA_KEY
|
from . import CONF_MONITORED_CONDITIONS, DATA_KEY, DISPATCHER_NETGEAR_LTE
|
||||||
from .sensor_types import SENSOR_SMS, SENSOR_USAGE
|
from .sensor_types import SENSOR_SMS, SENSOR_USAGE
|
||||||
|
|
||||||
DEPENDENCIES = ['netgear_lte']
|
DEPENDENCIES = ['netgear_lte']
|
||||||
@ -36,7 +37,7 @@ async def async_setup_platform(
|
|||||||
elif sensor_type == SENSOR_USAGE:
|
elif sensor_type == SENSOR_USAGE:
|
||||||
sensors.append(UsageSensor(modem_data, sensor_type))
|
sensors.append(UsageSensor(modem_data, sensor_type))
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
@ -46,10 +47,20 @@ class LTESensor(Entity):
|
|||||||
modem_data = attr.ib()
|
modem_data = attr.ib()
|
||||||
sensor_type = attr.ib()
|
sensor_type = attr.ib()
|
||||||
|
|
||||||
|
async def async_added_to_hass(self):
|
||||||
|
"""Register callback."""
|
||||||
|
async_dispatcher_connect(
|
||||||
|
self.hass, DISPATCHER_NETGEAR_LTE, self.async_write_ha_state)
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update state."""
|
"""Force update of state."""
|
||||||
await self.modem_data.async_update()
|
await self.modem_data.async_update()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def should_poll(self):
|
||||||
|
"""Return that the sensor should not be polled."""
|
||||||
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique ID like 'usage_5TG365AB0078V'."""
|
"""Return a unique ID like 'usage_5TG365AB0078V'."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user