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

Do data extraction in sensors #22444

Merged
merged 2 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions homeassistant/components/netgear_lte/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,22 @@ class ModemData:
host = attr.ib()
modem = attr.ib()

serial_number = attr.ib(init=False, default=None)
unread_count = attr.ib(init=False, default=None)
usage = attr.ib(init=False, default=None)
data = attr.ib(init=False, default=None)
connected = attr.ib(init=False, default=True)

async def async_update(self):
"""Call the API to update the data."""
import eternalegypt
try:
information = 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
self.data = await self.modem.information()
if not self.connected:
_LOGGER.warning("Connected to %s", self.host)
self.connected = True
except eternalegypt.Error:
if self.connected:
_LOGGER.warning("Lost connection to %s", self.host)
self.connected = False
self.unread_count = None
self.usage = None
self.data = None

async_dispatcher_send(self.hass, DISPATCHER_NETGEAR_LTE)

Expand Down
24 changes: 17 additions & 7 deletions homeassistant/components/netgear_lte/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def async_setup_platform(

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

sensor_conf = discovery_info[DOMAIN]
Expand All @@ -47,6 +47,14 @@ class LTESensor(Entity):
modem_data = attr.ib()
sensor_type = attr.ib()

_unique_id = attr.ib(init=False)

@_unique_id.default
amelchio marked this conversation as resolved.
Show resolved Hide resolved
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):
"""Register callback."""
async_dispatcher_connect(
Expand All @@ -61,10 +69,15 @@ def should_poll(self):
"""Return that the sensor should not be polled."""
return False

@property
def available(self):
"""Return the availability of the sensor."""
return self.modem_data.data is not None

@property
def unique_id(self):
"""Return a unique ID like 'usage_5TG365AB0078V'."""
return "{}_{}".format(self.sensor_type, self.modem_data.serial_number)
return self._unique_id


class SMSSensor(LTESensor):
Expand All @@ -78,7 +91,7 @@ def name(self):
@property
def state(self):
"""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):
Expand All @@ -97,7 +110,4 @@ def name(self):
@property
def state(self):
"""Return the state of the sensor."""
if self.modem_data.usage is None:
return None

return round(self.modem_data.usage / 1024**2, 1)
return round(self.modem_data.data.usage / 1024**2, 1)