Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for SMS dongle sensor."""
2 
4  SensorDeviceClass,
5  SensorEntity,
6  SensorEntityDescription,
7  SensorStateClass,
8 )
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import PERCENTAGE, SIGNAL_STRENGTH_DECIBELS, EntityCategory
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.update_coordinator import CoordinatorEntity
15 
16 from .const import DOMAIN, GATEWAY, NETWORK_COORDINATOR, SIGNAL_COORDINATOR, SMS_GATEWAY
17 
18 SIGNAL_SENSORS = (
20  key="SignalStrength",
21  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
22  entity_category=EntityCategory.DIAGNOSTIC,
23  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
24  entity_registry_enabled_default=False,
25  state_class=SensorStateClass.MEASUREMENT,
26  ),
28  key="SignalPercent",
29  translation_key="signal_percent",
30  native_unit_of_measurement=PERCENTAGE,
31  entity_registry_enabled_default=True,
32  state_class=SensorStateClass.MEASUREMENT,
33  ),
35  key="BitErrorRate",
36  translation_key="bit_error_rate",
37  entity_category=EntityCategory.DIAGNOSTIC,
38  native_unit_of_measurement=PERCENTAGE,
39  entity_registry_enabled_default=False,
40  state_class=SensorStateClass.MEASUREMENT,
41  ),
42 )
43 
44 NETWORK_SENSORS = (
46  key="NetworkName",
47  translation_key="network_name",
48  entity_category=EntityCategory.DIAGNOSTIC,
49  entity_registry_enabled_default=False,
50  ),
52  key="State",
53  translation_key="state",
54  entity_registry_enabled_default=True,
55  ),
57  key="NetworkCode",
58  translation_key="network_code",
59  entity_category=EntityCategory.DIAGNOSTIC,
60  entity_registry_enabled_default=False,
61  ),
63  key="CID",
64  translation_key="cid",
65  entity_category=EntityCategory.DIAGNOSTIC,
66  entity_registry_enabled_default=False,
67  ),
69  key="LAC",
70  translation_key="lac",
71  entity_category=EntityCategory.DIAGNOSTIC,
72  entity_registry_enabled_default=False,
73  ),
74 )
75 
76 
78  hass: HomeAssistant,
79  config_entry: ConfigEntry,
80  async_add_entities: AddEntitiesCallback,
81 ) -> None:
82  """Set up all device sensors."""
83  sms_data = hass.data[DOMAIN][SMS_GATEWAY]
84  signal_coordinator = sms_data[SIGNAL_COORDINATOR]
85  network_coordinator = sms_data[NETWORK_COORDINATOR]
86  gateway = sms_data[GATEWAY]
87  unique_id = str(await gateway.get_imei_async())
88  entities = [
89  DeviceSensor(signal_coordinator, description, unique_id, gateway)
90  for description in SIGNAL_SENSORS
91  ]
92  entities.extend(
93  DeviceSensor(network_coordinator, description, unique_id, gateway)
94  for description in NETWORK_SENSORS
95  )
96  async_add_entities(entities, True)
97 
98 
100  """Implementation of a device sensor."""
101 
102  _attr_has_entity_name = True
103 
104  def __init__(self, coordinator, description, unique_id, gateway):
105  """Initialize the device sensor."""
106  super().__init__(coordinator)
107  self._attr_device_info_attr_device_info = DeviceInfo(
108  identifiers={(DOMAIN, unique_id)},
109  name="SMS Gateway",
110  manufacturer=gateway.manufacturer,
111  model=gateway.model,
112  sw_version=gateway.firmware,
113  )
114  self._attr_unique_id_attr_unique_id = f"{unique_id}_{description.key}"
115  self.entity_descriptionentity_description = description
116 
117  @property
118  def native_value(self):
119  """Return the state of the device."""
120  return self.coordinator.data.get(self.entity_descriptionentity_description.key)
def __init__(self, coordinator, description, unique_id, gateway)
Definition: sensor.py:104
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:81