2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Netgear LTE sensors."""
|
2019-03-22 13:43:39 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import DOMAIN
|
2018-09-23 16:58:09 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-04-09 18:28:20 +00:00
|
|
|
from . import CONF_MONITORED_CONDITIONS, DATA_KEY, LTEEntity
|
2019-07-31 19:25:30 +00:00
|
|
|
from .sensor_types import SENSOR_SMS, SENSOR_SMS_TOTAL, SENSOR_USAGE, SENSOR_UNITS
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-03-22 13:43:39 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(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
|
|
|
|
2019-03-27 23:24:02 +00:00
|
|
|
if not modem_data or not modem_data.data:
|
2018-09-23 16:58:09 +00:00
|
|
|
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:
|
2019-04-10 07:41:57 +00:00
|
|
|
sensors.append(SMSUnreadSensor(modem_data, sensor_type))
|
|
|
|
elif sensor_type == SENSOR_SMS_TOTAL:
|
|
|
|
sensors.append(SMSTotalSensor(modem_data, sensor_type))
|
2018-07-21 08:14:56 +00:00
|
|
|
elif sensor_type == SENSOR_USAGE:
|
|
|
|
sensors.append(UsageSensor(modem_data, sensor_type))
|
2019-04-01 06:47:29 +00:00
|
|
|
else:
|
|
|
|
sensors.append(GenericSensor(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
|
|
|
|
|
|
|
|
2019-04-09 18:28:20 +00:00
|
|
|
class LTESensor(LTEEntity):
|
2018-09-08 21:22:41 +00:00
|
|
|
"""Base LTE sensor entity."""
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2019-03-30 09:09:36 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return SENSOR_UNITS[self.sensor_type]
|
|
|
|
|
|
|
|
|
2019-04-10 07:41:57 +00:00
|
|
|
class SMSUnreadSensor(LTESensor):
|
2019-03-30 09:09:36 +00:00
|
|
|
"""Unread SMS sensor entity."""
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2019-03-27 23:24:02 +00:00
|
|
|
return sum(1 for x in self.modem_data.data.sms if x.unread)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
2019-04-10 07:41:57 +00:00
|
|
|
class SMSTotalSensor(LTESensor):
|
|
|
|
"""Total SMS sensor entity."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return len(self.modem_data.data.sms)
|
|
|
|
|
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
class UsageSensor(LTESensor):
|
|
|
|
"""Data usage sensor entity."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return round(self.modem_data.data.usage / 1024 ** 2, 1)
|
2019-04-01 06:47:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenericSensor(LTESensor):
|
|
|
|
"""Sensor entity with raw state."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return getattr(self.modem_data.data, self.sensor_type)
|