Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for sensors from the Dovado router."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from datetime import timedelta
7 import re
8 
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16 )
17 from homeassistant.const import CONF_SENSORS, PERCENTAGE, UnitOfInformation
18 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 from . import DOMAIN as DOVADO_DOMAIN
24 
25 MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
26 
27 SENSOR_UPLOAD = "upload"
28 SENSOR_DOWNLOAD = "download"
29 SENSOR_SIGNAL = "signal"
30 SENSOR_NETWORK = "network"
31 SENSOR_SMS_UNREAD = "sms"
32 
33 
34 @dataclass(frozen=True, kw_only=True)
36  """Describes Dovado sensor entity."""
37 
38  identifier: str
39 
40 
41 SENSOR_TYPES: tuple[DovadoSensorEntityDescription, ...] = (
43  identifier=SENSOR_NETWORK,
44  key="signal strength",
45  name="Network",
46  icon="mdi:access-point-network",
47  ),
49  identifier=SENSOR_SIGNAL,
50  key="signal strength",
51  name="Signal Strength",
52  native_unit_of_measurement=PERCENTAGE,
53  icon="mdi:signal",
54  ),
56  identifier=SENSOR_SMS_UNREAD,
57  key="sms unread",
58  name="SMS unread",
59  icon="mdi:message-text-outline",
60  ),
62  identifier=SENSOR_UPLOAD,
63  key="traffic modem tx",
64  name="Sent",
65  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
66  device_class=SensorDeviceClass.DATA_SIZE,
67  icon="mdi:cloud-upload",
68  ),
70  identifier=SENSOR_DOWNLOAD,
71  key="traffic modem rx",
72  name="Received",
73  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
74  device_class=SensorDeviceClass.DATA_SIZE,
75  icon="mdi:cloud-download",
76  ),
77 )
78 
79 SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
80 
81 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
82  {vol.Required(CONF_SENSORS): vol.All(cv.ensure_list, [vol.In(SENSOR_KEYS)])}
83 )
84 
85 
87  hass: HomeAssistant,
88  config: ConfigType,
89  add_entities: AddEntitiesCallback,
90  discovery_info: DiscoveryInfoType | None = None,
91 ) -> None:
92  """Set up the Dovado sensor platform."""
93  dovado = hass.data[DOVADO_DOMAIN]
94 
95  sensors = config[CONF_SENSORS]
96  entities = [
97  DovadoSensor(dovado, description)
98  for description in SENSOR_TYPES
99  if description.key in sensors
100  ]
101  add_entities(entities)
102 
103 
105  """Representation of a Dovado sensor."""
106 
107  entity_description: DovadoSensorEntityDescription
108 
109  def __init__(self, data, description: DovadoSensorEntityDescription) -> None:
110  """Initialize the sensor."""
111  self.entity_descriptionentity_description = description
112  self._data_data = data
113 
114  self._attr_name_attr_name = f"{data.name} {description.name}"
115  self._attr_native_value_attr_native_value = self._compute_state_compute_state()
116 
117  def _compute_state(self):
118  """Compute the state of the sensor."""
119  state = self._data_data.state.get(self.entity_descriptionentity_description.key)
120  sensor_identifier = self.entity_descriptionentity_description.identifier
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:
125  try:
126  return int(state.split()[0])
127  except ValueError:
128  return None
129  if sensor_identifier == SENSOR_SMS_UNREAD:
130  return int(state)
131  if sensor_identifier in [SENSOR_UPLOAD, SENSOR_DOWNLOAD]:
132  return round(float(state) / 1e6, 1)
133  return state
134 
135  def update(self) -> None:
136  """Update sensor values."""
137  self._data_data.update()
138  self._attr_native_value_attr_native_value = self._compute_state_compute_state()
139 
140  @property
142  """Return the state attributes."""
143  return {k: v for k, v in self._data_data.state.items() if k not in ["date", "time"]}
None __init__(self, data, DovadoSensorEntityDescription description)
Definition: sensor.py:109
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:91
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40