Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for WiZ sensors."""
2 
3 from __future__ import annotations
4 
6  SensorDeviceClass,
7  SensorEntity,
8  SensorEntityDescription,
9  SensorStateClass,
10 )
11 from homeassistant.const import (
12  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
13  EntityCategory,
14  UnitOfPower,
15 )
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import WizConfigEntry
20 from .entity import WizEntity
21 from .models import WizData
22 
23 SENSORS: tuple[SensorEntityDescription, ...] = (
25  key="rssi",
26  entity_registry_enabled_default=False,
27  state_class=SensorStateClass.MEASUREMENT,
28  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
29  entity_category=EntityCategory.DIAGNOSTIC,
30  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
31  ),
32 )
33 
34 
35 POWER_SENSORS: tuple[SensorEntityDescription, ...] = (
37  key="power",
38  state_class=SensorStateClass.MEASUREMENT,
39  device_class=SensorDeviceClass.POWER,
40  native_unit_of_measurement=UnitOfPower.WATT,
41  ),
42 )
43 
44 
46  hass: HomeAssistant,
47  entry: WizConfigEntry,
48  async_add_entities: AddEntitiesCallback,
49 ) -> None:
50  """Set up the wiz sensor."""
51  entities = [
52  WizSensor(entry.runtime_data, entry.title, description)
53  for description in SENSORS
54  ]
55  if entry.runtime_data.coordinator.data is not None:
56  entities.extend(
57  [
58  WizPowerSensor(entry.runtime_data, entry.title, description)
59  for description in POWER_SENSORS
60  ]
61  )
62  async_add_entities(entities)
63 
64 
66  """Defines a WiZ sensor."""
67 
68  entity_description: SensorEntityDescription
69 
70  def __init__(
71  self, wiz_data: WizData, name: str, description: SensorEntityDescription
72  ) -> None:
73  """Initialize an WiZ sensor."""
74  super().__init__(wiz_data, name)
75  self.entity_descriptionentity_description = description
76  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{self._device.mac}_{description.key}"
77  self._async_update_attrs_async_update_attrs_async_update_attrs()
78 
79  @callback
80  def _async_update_attrs(self) -> None:
81  """Handle updating _attr values."""
82  self._attr_native_value_attr_native_value = self._device_device.state.pilotResult.get(
83  self.entity_descriptionentity_description.key
84  )
85 
86 
88  """Defines a WiZ power sensor."""
89 
90  @callback
91  def _async_update_attrs(self) -> None:
92  """Handle updating _attr values."""
93  # Newer firmwares will have the power in their state
94  watts_push = self._device_device.state.get_power()
95  # Older firmwares will be polled and in the coordinator data
96  watts_poll = self.coordinator.data
97  self._attr_native_value_attr_native_value_attr_native_value = watts_poll if watts_push is None else watts_push
None __init__(self, WizData wiz_data, str name, SensorEntityDescription description)
Definition: sensor.py:72
None async_setup_entry(HomeAssistant hass, WizConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:49