1 """Support for sensors from the Dovado router."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
6 from datetime
import timedelta
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
15 SensorEntityDescription,
23 from .
import DOMAIN
as DOVADO_DOMAIN
27 SENSOR_UPLOAD =
"upload"
28 SENSOR_DOWNLOAD =
"download"
29 SENSOR_SIGNAL =
"signal"
30 SENSOR_NETWORK =
"network"
31 SENSOR_SMS_UNREAD =
"sms"
34 @dataclass(frozen=True, kw_only=True)
36 """Describes Dovado sensor entity."""
41 SENSOR_TYPES: tuple[DovadoSensorEntityDescription, ...] = (
43 identifier=SENSOR_NETWORK,
44 key=
"signal strength",
46 icon=
"mdi:access-point-network",
49 identifier=SENSOR_SIGNAL,
50 key=
"signal strength",
51 name=
"Signal Strength",
52 native_unit_of_measurement=PERCENTAGE,
56 identifier=SENSOR_SMS_UNREAD,
59 icon=
"mdi:message-text-outline",
62 identifier=SENSOR_UPLOAD,
63 key=
"traffic modem tx",
65 native_unit_of_measurement=UnitOfInformation.GIGABYTES,
66 device_class=SensorDeviceClass.DATA_SIZE,
67 icon=
"mdi:cloud-upload",
70 identifier=SENSOR_DOWNLOAD,
71 key=
"traffic modem rx",
73 native_unit_of_measurement=UnitOfInformation.GIGABYTES,
74 device_class=SensorDeviceClass.DATA_SIZE,
75 icon=
"mdi:cloud-download",
79 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
81 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
82 {vol.Required(CONF_SENSORS): vol.All(cv.ensure_list, [vol.In(SENSOR_KEYS)])}
89 add_entities: AddEntitiesCallback,
90 discovery_info: DiscoveryInfoType |
None =
None,
92 """Set up the Dovado sensor platform."""
93 dovado = hass.data[DOVADO_DOMAIN]
95 sensors = config[CONF_SENSORS]
98 for description
in SENSOR_TYPES
99 if description.key
in sensors
105 """Representation of a Dovado sensor."""
107 entity_description: DovadoSensorEntityDescription
109 def __init__(self, data, description: DovadoSensorEntityDescription) ->
None:
110 """Initialize the sensor."""
114 self.
_attr_name_attr_name = f
"{data.name} {description.name}"
118 """Compute the state of the sensor."""
121 if sensor_identifier == SENSOR_NETWORK:
122 match = re.search(
r"\((.+)\)", state)
123 return match.group(1)
if match
else None
124 if sensor_identifier == SENSOR_SIGNAL:
126 return int(state.split()[0])
129 if sensor_identifier == SENSOR_SMS_UNREAD:
131 if sensor_identifier
in [SENSOR_UPLOAD, SENSOR_DOWNLOAD]:
132 return round(
float(state) / 1e6, 1)
136 """Update sensor values."""
142 """Return the state attributes."""
143 return {k: v
for k, v
in self.
_data_data.state.items()
if k
not in [
"date",
"time"]}
def extra_state_attributes(self)
None __init__(self, data, DovadoSensorEntityDescription description)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)