Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Medcom BLE radiation monitor sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from medcom_ble import MedcomBleDevice
8 
9 from homeassistant import config_entries
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceInfo
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19  CoordinatorEntity,
20  DataUpdateCoordinator,
21 )
22 
23 from .const import DOMAIN, UNIT_CPM
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 SENSORS_MAPPING_TEMPLATE: dict[str, SensorEntityDescription] = {
29  key="cpm",
30  translation_key="cpm",
31  native_unit_of_measurement=UNIT_CPM,
32  state_class=SensorStateClass.MEASUREMENT,
33  ),
34 }
35 
36 
38  hass: HomeAssistant,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up Medcom BLE radiation monitor sensors."""
43 
44  coordinator: DataUpdateCoordinator[MedcomBleDevice] = hass.data[DOMAIN][
45  entry.entry_id
46  ]
47 
48  entities = []
49  _LOGGER.debug("got sensors: %s", coordinator.data.sensors)
50  for sensor_type, sensor_value in coordinator.data.sensors.items():
51  if sensor_type not in SENSORS_MAPPING_TEMPLATE:
52  _LOGGER.debug(
53  "Unknown sensor type detected: %s, %s",
54  sensor_type,
55  sensor_value,
56  )
57  continue
58  entities.append(
59  MedcomSensor(coordinator, SENSORS_MAPPING_TEMPLATE[sensor_type])
60  )
61 
62  async_add_entities(entities)
63 
64 
66  CoordinatorEntity[DataUpdateCoordinator[MedcomBleDevice]], SensorEntity
67 ):
68  """Medcom BLE radiation monitor sensors for the device."""
69 
70  _attr_has_entity_name = True
71 
72  def __init__(
73  self,
74  coordinator: DataUpdateCoordinator[MedcomBleDevice],
75  entity_description: SensorEntityDescription,
76  ) -> None:
77  """Populate the medcom entity with relevant data."""
78  super().__init__(coordinator)
79  self.entity_descriptionentity_description = entity_description
80  medcom_device = coordinator.data
81 
82  name = medcom_device.name
83  if identifier := medcom_device.identifier:
84  name += f" ({identifier})"
85 
86  self._attr_unique_id_attr_unique_id = f"{medcom_device.address}_{entity_description.key}"
87  self._attr_device_info_attr_device_info = DeviceInfo(
88  connections={
89  (
90  CONNECTION_BLUETOOTH,
91  medcom_device.address,
92  )
93  },
94  name=name,
95  manufacturer=medcom_device.manufacturer,
96  hw_version=medcom_device.hw_version,
97  sw_version=medcom_device.sw_version,
98  model=medcom_device.model,
99  )
100 
101  @property
102  def native_value(self) -> float:
103  """Return the value reported by the sensor."""
104  return self.coordinator.data.sensors[self.entity_descriptionentity_description.key]
None __init__(self, DataUpdateCoordinator[MedcomBleDevice] coordinator, SensorEntityDescription entity_description)
Definition: sensor.py:76
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:41