2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Netgear LTE sensors."""
|
2022-01-12 11:53:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-13 09:18:35 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
2023-12-26 04:19:28 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-12 11:53:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2023-12-26 04:19:28 +00:00
|
|
|
from .const import DOMAIN
|
2023-05-12 11:29:56 +00:00
|
|
|
from .entity import LTEEntity
|
2023-12-26 04:19:28 +00:00
|
|
|
from .sensor_types import (
|
|
|
|
ALL_SENSORS,
|
|
|
|
SENSOR_SMS,
|
|
|
|
SENSOR_SMS_TOTAL,
|
|
|
|
SENSOR_UNITS,
|
|
|
|
SENSOR_USAGE,
|
|
|
|
)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
2023-12-26 04:19:28 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2022-01-12 11:53:50 +00:00
|
|
|
) -> None:
|
2023-12-26 04:19:28 +00:00
|
|
|
"""Set up the Netgear LTE sensor."""
|
|
|
|
modem_data = hass.data[DOMAIN].get_modem_data(entry.data)
|
2019-03-22 13:43:39 +00:00
|
|
|
|
2022-01-12 11:53:50 +00:00
|
|
|
sensors: list[SensorEntity] = []
|
2023-12-26 04:19:28 +00:00
|
|
|
for sensor in ALL_SENSORS:
|
|
|
|
if sensor == SENSOR_SMS:
|
|
|
|
sensors.append(SMSUnreadSensor(modem_data, sensor))
|
|
|
|
elif sensor == SENSOR_SMS_TOTAL:
|
|
|
|
sensors.append(SMSTotalSensor(modem_data, sensor))
|
|
|
|
elif sensor == SENSOR_USAGE:
|
|
|
|
sensors.append(UsageSensor(modem_data, sensor))
|
2019-04-01 06:47:29 +00:00
|
|
|
else:
|
2023-12-26 04:19:28 +00:00
|
|
|
sensors.append(GenericSensor(modem_data, sensor))
|
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
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class LTESensor(LTEEntity, SensorEntity):
|
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
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2019-03-30 09:09:36 +00:00
|
|
|
"""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
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self):
|
2018-06-08 05:46:34 +00:00
|
|
|
"""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
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self):
|
2019-04-10 07:41:57 +00:00
|
|
|
"""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."""
|
|
|
|
|
2022-12-13 09:18:35 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.DATA_SIZE
|
|
|
|
|
2018-06-08 05:46:34 +00:00
|
|
|
@property
|
2022-12-13 09:18:35 +00:00
|
|
|
def native_value(self) -> float:
|
2018-06-08 05:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2022-02-05 13:19:37 +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
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self):
|
2019-04-01 06:47:29 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return getattr(self.modem_data.data, self.sensor_type)
|