Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for the Hive sensors."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 from datetime import timedelta
6 from typing import Any
7 
8 from apyhiveapi import Hive
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  PERCENTAGE,
19  EntityCategory,
20  UnitOfPower,
21  UnitOfTemperature,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import StateType
26 
27 from .const import DOMAIN
28 from .entity import HiveEntity
29 
30 PARALLEL_UPDATES = 0
31 SCAN_INTERVAL = timedelta(seconds=15)
32 
33 
34 @dataclass(frozen=True)
36  """Describes Hive sensor entity."""
37 
38  fn: Callable[[StateType], StateType] = lambda x: x
39 
40 
41 SENSOR_TYPES: tuple[HiveSensorEntityDescription, ...] = (
43  key="Battery",
44  native_unit_of_measurement=PERCENTAGE,
45  device_class=SensorDeviceClass.BATTERY,
46  entity_category=EntityCategory.DIAGNOSTIC,
47  ),
49  key="Power",
50  native_unit_of_measurement=UnitOfPower.WATT,
51  state_class=SensorStateClass.MEASUREMENT,
52  device_class=SensorDeviceClass.POWER,
53  entity_category=EntityCategory.DIAGNOSTIC,
54  ),
56  key="Current_Temperature",
57  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
58  state_class=SensorStateClass.MEASUREMENT,
59  device_class=SensorDeviceClass.TEMPERATURE,
60  entity_category=EntityCategory.DIAGNOSTIC,
61  ),
63  key="Heating_Current_Temperature",
64  device_class=SensorDeviceClass.TEMPERATURE,
65  state_class=SensorStateClass.MEASUREMENT,
66  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
67  ),
69  key="Heating_Target_Temperature",
70  device_class=SensorDeviceClass.TEMPERATURE,
71  state_class=SensorStateClass.MEASUREMENT,
72  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
73  ),
75  key="Heating_Mode",
76  device_class=SensorDeviceClass.ENUM,
77  options=["schedule", "manual", "off"],
78  translation_key="heating",
79  fn=lambda x: x.lower() if isinstance(x, str) else None,
80  ),
82  key="Hotwater_Mode",
83  device_class=SensorDeviceClass.ENUM,
84  options=["schedule", "on", "off"],
85  translation_key="hot_water",
86  fn=lambda x: x.lower() if isinstance(x, str) else None,
87  ),
88 )
89 
90 
92  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
93 ) -> None:
94  """Set up Hive thermostat based on a config entry."""
95  hive = hass.data[DOMAIN][entry.entry_id]
96  devices = hive.session.deviceList.get("sensor")
97  if not devices:
98  return
100  (
101  HiveSensorEntity(hive, dev, description)
102  for dev in devices
103  for description in SENSOR_TYPES
104  if dev["hiveType"] == description.key
105  ),
106  True,
107  )
108 
109 
111  """Hive Sensor Entity."""
112 
113  entity_description: HiveSensorEntityDescription
114 
115  def __init__(
116  self,
117  hive: Hive,
118  hive_device: dict[str, Any],
119  entity_description: HiveSensorEntityDescription,
120  ) -> None:
121  """Initialise hive sensor."""
122  super().__init__(hive, hive_device)
123  self.entity_descriptionentity_description = entity_description
124 
125  async def async_update(self) -> None:
126  """Update all Node data from Hive."""
127  await self.hivehive.session.updateData(self.devicedevicedevice)
128  self.devicedevicedevice = await self.hivehive.sensor.getSensor(self.devicedevicedevice)
129  self._attr_native_value_attr_native_value = self.entity_descriptionentity_description.fn(
130  self.devicedevicedevice.get("status", {}).get("state")
131  )
None __init__(self, Hive hive, dict[str, Any] hive_device, HiveSensorEntityDescription entity_description)
Definition: sensor.py:120
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:93