mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Do data extraction in sensors (#22444)
* Do data extraction in sensors * Hopefully fix lint
This commit is contained in:
parent
f795d03503
commit
90c4f6f6e5
@ -62,19 +62,14 @@ class ModemData:
|
|||||||
host = attr.ib()
|
host = attr.ib()
|
||||||
modem = attr.ib()
|
modem = attr.ib()
|
||||||
|
|
||||||
serial_number = attr.ib(init=False, default=None)
|
data = attr.ib(init=False, default=None)
|
||||||
unread_count = 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)
|
||||||
|
|
||||||
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
|
||||||
try:
|
try:
|
||||||
information = await self.modem.information()
|
self.data = await self.modem.information()
|
||||||
self.serial_number = information.serial_number
|
|
||||||
self.unread_count = sum(1 for x in information.sms if x.unread)
|
|
||||||
self.usage = information.usage
|
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
_LOGGER.warning("Connected to %s", self.host)
|
_LOGGER.warning("Connected to %s", self.host)
|
||||||
self.connected = True
|
self.connected = True
|
||||||
@ -82,8 +77,7 @@ class ModemData:
|
|||||||
if self.connected:
|
if self.connected:
|
||||||
_LOGGER.warning("Lost connection to %s", self.host)
|
_LOGGER.warning("Lost connection to %s", self.host)
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.unread_count = None
|
self.data = None
|
||||||
self.usage = None
|
|
||||||
|
|
||||||
async_dispatcher_send(self.hass, DISPATCHER_NETGEAR_LTE)
|
async_dispatcher_send(self.hass, DISPATCHER_NETGEAR_LTE)
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ async def async_setup_platform(
|
|||||||
|
|
||||||
modem_data = hass.data[DATA_KEY].get_modem_data(discovery_info)
|
modem_data = hass.data[DATA_KEY].get_modem_data(discovery_info)
|
||||||
|
|
||||||
if not modem_data:
|
if not modem_data or not modem_data.data:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
sensor_conf = discovery_info[DOMAIN]
|
sensor_conf = discovery_info[DOMAIN]
|
||||||
@ -47,6 +47,14 @@ class LTESensor(Entity):
|
|||||||
modem_data = attr.ib()
|
modem_data = attr.ib()
|
||||||
sensor_type = attr.ib()
|
sensor_type = attr.ib()
|
||||||
|
|
||||||
|
_unique_id = attr.ib(init=False)
|
||||||
|
|
||||||
|
@_unique_id.default
|
||||||
|
def _init_unique_id(self):
|
||||||
|
"""Register unique_id while we know data is valid."""
|
||||||
|
return "{}_{}".format(
|
||||||
|
self.sensor_type, self.modem_data.data.serial_number)
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Register callback."""
|
"""Register callback."""
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
@ -61,10 +69,15 @@ class LTESensor(Entity):
|
|||||||
"""Return that the sensor should not be polled."""
|
"""Return that the sensor should not be polled."""
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return the availability of the sensor."""
|
||||||
|
return self.modem_data.data is not None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique ID like 'usage_5TG365AB0078V'."""
|
"""Return a unique ID like 'usage_5TG365AB0078V'."""
|
||||||
return "{}_{}".format(self.sensor_type, self.modem_data.serial_number)
|
return self._unique_id
|
||||||
|
|
||||||
|
|
||||||
class SMSSensor(LTESensor):
|
class SMSSensor(LTESensor):
|
||||||
@ -78,7 +91,7 @@ class SMSSensor(LTESensor):
|
|||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.modem_data.unread_count
|
return sum(1 for x in self.modem_data.data.sms if x.unread)
|
||||||
|
|
||||||
|
|
||||||
class UsageSensor(LTESensor):
|
class UsageSensor(LTESensor):
|
||||||
@ -97,7 +110,4 @@ class UsageSensor(LTESensor):
|
|||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self.modem_data.usage is None:
|
return round(self.modem_data.data.usage / 1024**2, 1)
|
||||||
return None
|
|
||||||
|
|
||||||
return round(self.modem_data.usage / 1024**2, 1)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user