2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Netgear LTE sensors."""
|
2019-03-22 13:43:39 +00:00
|
|
|
import logging
|
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
import attr
|
|
|
|
|
2019-03-22 13:43:39 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN
|
2018-09-23 16:58:09 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-02-11 19:46:21 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-03-26 15:06:11 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-03-26 15:06:11 +00:00
|
|
|
from . import CONF_MONITORED_CONDITIONS, DATA_KEY, DISPATCHER_NETGEAR_LTE
|
2019-03-22 13:43:39 +00:00
|
|
|
from .sensor_types import SENSOR_SMS, SENSOR_USAGE
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['netgear_lte']
|
|
|
|
|
2019-03-22 13:43:39 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
2018-08-24 14:37:30 +00:00
|
|
|
hass, config, async_add_entities, discovery_info):
|
2018-06-08 05:46:34 +00:00
|
|
|
"""Set up Netgear LTE sensor devices."""
|
2019-03-22 13:43:39 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
modem_data = hass.data[DATA_KEY].get_modem_data(discovery_info)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2018-09-23 16:58:09 +00:00
|
|
|
if not modem_data:
|
|
|
|
raise PlatformNotReady
|
|
|
|
|
2019-03-22 13:43:39 +00:00
|
|
|
sensor_conf = discovery_info[DOMAIN]
|
|
|
|
monitored_conditions = sensor_conf[CONF_MONITORED_CONDITIONS]
|
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
sensors = []
|
2019-03-22 13:43:39 +00:00
|
|
|
for sensor_type in monitored_conditions:
|
2018-07-21 08:14:56 +00:00
|
|
|
if sensor_type == SENSOR_SMS:
|
|
|
|
sensors.append(SMSSensor(modem_data, sensor_type))
|
|
|
|
elif sensor_type == SENSOR_USAGE:
|
|
|
|
sensors.append(UsageSensor(modem_data, sensor_type))
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-03-26 15:06:11 +00:00
|
|
|
async_add_entities(sensors)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class LTESensor(Entity):
|
2018-09-08 21:22:41 +00:00
|
|
|
"""Base LTE sensor entity."""
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2018-06-29 21:25:49 +00:00
|
|
|
modem_data = attr.ib()
|
2018-07-21 08:14:56 +00:00
|
|
|
sensor_type = attr.ib()
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-03-26 15:06:11 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callback."""
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, DISPATCHER_NETGEAR_LTE, self.async_write_ha_state)
|
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
async def async_update(self):
|
2019-03-26 15:06:11 +00:00
|
|
|
"""Force update of state."""
|
2018-06-29 21:25:49 +00:00
|
|
|
await self.modem_data.async_update()
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-03-26 15:06:11 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return that the sensor should not be polled."""
|
|
|
|
return False
|
|
|
|
|
2018-07-21 08:14:56 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID like 'usage_5TG365AB0078V'."""
|
|
|
|
return "{}_{}".format(self.sensor_type, self.modem_data.serial_number)
|
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
class SMSSensor(LTESensor):
|
|
|
|
"""Unread SMS sensor entity."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return "Netgear LTE SMS"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2018-06-29 21:25:49 +00:00
|
|
|
return self.modem_data.unread_count
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UsageSensor(LTESensor):
|
|
|
|
"""Data usage sensor entity."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return "MiB"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return "Netgear LTE usage"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2018-09-23 16:58:09 +00:00
|
|
|
if self.modem_data.usage is None:
|
|
|
|
return None
|
|
|
|
|
2018-06-29 21:25:49 +00:00
|
|
|
return round(self.modem_data.usage / 1024**2, 1)
|