Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for SwitchBot sensors."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.bluetooth import async_last_service_info
7  SensorDeviceClass,
8  SensorEntity,
9  SensorEntityDescription,
10  SensorStateClass,
11 )
12 from homeassistant.const import (
13  CONCENTRATION_PARTS_PER_MILLION,
14  PERCENTAGE,
15  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
16  EntityCategory,
17  UnitOfPower,
18  UnitOfTemperature,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
24 from .entity import SwitchbotEntity
25 
26 PARALLEL_UPDATES = 0
27 
28 SENSOR_TYPES: dict[str, SensorEntityDescription] = {
30  key="rssi",
31  translation_key="bluetooth_signal",
32  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
33  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
34  state_class=SensorStateClass.MEASUREMENT,
35  entity_registry_enabled_default=False,
36  entity_category=EntityCategory.DIAGNOSTIC,
37  ),
38  "wifi_rssi": SensorEntityDescription(
39  key="wifi_rssi",
40  translation_key="wifi_signal",
41  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
42  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
43  state_class=SensorStateClass.MEASUREMENT,
44  entity_registry_enabled_default=False,
45  entity_category=EntityCategory.DIAGNOSTIC,
46  ),
47  "battery": SensorEntityDescription(
48  key="battery",
49  native_unit_of_measurement=PERCENTAGE,
50  device_class=SensorDeviceClass.BATTERY,
51  state_class=SensorStateClass.MEASUREMENT,
52  entity_category=EntityCategory.DIAGNOSTIC,
53  ),
55  key="co2",
56  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
57  state_class=SensorStateClass.MEASUREMENT,
58  device_class=SensorDeviceClass.CO2,
59  ),
60  "lightLevel": SensorEntityDescription(
61  key="lightLevel",
62  translation_key="light_level",
63  native_unit_of_measurement="Level",
64  state_class=SensorStateClass.MEASUREMENT,
65  ),
66  "humidity": SensorEntityDescription(
67  key="humidity",
68  native_unit_of_measurement=PERCENTAGE,
69  state_class=SensorStateClass.MEASUREMENT,
70  device_class=SensorDeviceClass.HUMIDITY,
71  ),
72  "temperature": SensorEntityDescription(
73  key="temperature",
74  name=None,
75  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
76  state_class=SensorStateClass.MEASUREMENT,
77  device_class=SensorDeviceClass.TEMPERATURE,
78  ),
79  "power": SensorEntityDescription(
80  key="power",
81  native_unit_of_measurement=UnitOfPower.WATT,
82  state_class=SensorStateClass.MEASUREMENT,
83  device_class=SensorDeviceClass.POWER,
84  ),
85 }
86 
87 
89  hass: HomeAssistant,
90  entry: SwitchbotConfigEntry,
91  async_add_entities: AddEntitiesCallback,
92 ) -> None:
93  """Set up Switchbot sensor based on a config entry."""
94  coordinator = entry.runtime_data
95  entities = [
96  SwitchBotSensor(coordinator, sensor)
97  for sensor in coordinator.device.parsed_data
98  if sensor in SENSOR_TYPES
99  ]
100  entities.append(SwitchbotRSSISensor(coordinator, "rssi"))
101  async_add_entities(entities)
102 
103 
105  """Representation of a Switchbot sensor."""
106 
107  def __init__(
108  self,
109  coordinator: SwitchbotDataUpdateCoordinator,
110  sensor: str,
111  ) -> None:
112  """Initialize the Switchbot sensor."""
113  super().__init__(coordinator)
114  self._sensor_sensor = sensor
115  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{coordinator.base_unique_id}-{sensor}"
116  self.entity_descriptionentity_description = SENSOR_TYPES[sensor]
117 
118  @property
119  def native_value(self) -> str | int | None:
120  """Return the state of the sensor."""
121  return self.parsed_dataparsed_data[self._sensor_sensor]
122 
123 
125  """Representation of a Switchbot RSSI sensor."""
126 
127  @property
128  def native_value(self) -> str | int | None:
129  """Return the state of the sensor."""
130  # Switchbot supports both connectable and non-connectable devices
131  # so we need to request the rssi value based on the connectable instead
132  # of the nearest scanner since that is the RSSI that matters for controlling
133  # the device.
134  if service_info := async_last_service_info(
135  self.hasshasshass, self._address_address, self.coordinator.connectable
136  ):
137  return service_info.rssi
138  return None
None __init__(self, SwitchbotDataUpdateCoordinator coordinator, str sensor)
Definition: sensor.py:111
BluetoothServiceInfoBleak|None async_last_service_info(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:80
None async_setup_entry(HomeAssistant hass, SwitchbotConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:92