Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Python Control of Nobø Hub - Nobø Energy Control."""
2 
3 from __future__ import annotations
4 
5 from pynobo import nobo
6 
8  SensorDeviceClass,
9  SensorEntity,
10  SensorStateClass,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import ATTR_MODEL, ATTR_NAME, UnitOfTemperature
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers.device_registry import DeviceInfo
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import StateType
18 
19 from .const import ATTR_SERIAL, ATTR_ZONE_ID, DOMAIN, NOBO_MANUFACTURER
20 
21 
23  hass: HomeAssistant,
24  config_entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up any temperature sensors connected to the Nobø Ecohub."""
28 
29  # Setup connection with hub
30  hub: nobo = hass.data[DOMAIN][config_entry.entry_id]
31 
33  NoboTemperatureSensor(component["serial"], hub)
34  for component in hub.components.values()
35  if component[ATTR_MODEL].has_temp_sensor
36  )
37 
38 
40  """A Nobø device with a temperature sensor."""
41 
42  _attr_device_class = SensorDeviceClass.TEMPERATURE
43  _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
44  _attr_state_class = SensorStateClass.MEASUREMENT
45  _attr_should_poll = False
46  _attr_has_entity_name = True
47 
48  def __init__(self, serial: str, hub: nobo) -> None:
49  """Initialize the temperature sensor."""
50  self._temperature: StateType = None
51  self._id_id = serial
52  self._nobo_nobo = hub
53  component = hub.components[self._id_id]
54  self._attr_unique_id_attr_unique_id = component[ATTR_SERIAL]
55  zone_id = component[ATTR_ZONE_ID]
56  suggested_area = None
57  if zone_id != "-1":
58  suggested_area = hub.zones[zone_id][ATTR_NAME]
59  self._attr_device_info_attr_device_info = DeviceInfo(
60  identifiers={(DOMAIN, component[ATTR_SERIAL])},
61  name=component[ATTR_NAME],
62  manufacturer=NOBO_MANUFACTURER,
63  model=component[ATTR_MODEL].name,
64  via_device=(DOMAIN, hub.hub_info[ATTR_SERIAL]),
65  suggested_area=suggested_area,
66  )
67  self._read_state_read_state()
68 
69  async def async_added_to_hass(self) -> None:
70  """Register callback from hub."""
71  self._nobo_nobo.register_callback(self._after_update_after_update)
72 
73  async def async_will_remove_from_hass(self) -> None:
74  """Deregister callback from hub."""
75  self._nobo_nobo.deregister_callback(self._after_update_after_update)
76 
77  @callback
78  def _read_state(self) -> None:
79  """Read the current state from the hub. This is a local call."""
80  value = self._nobo_nobo.get_current_component_temperature(self._id_id)
81  if value is None:
82  self._attr_native_value_attr_native_value = None
83  else:
84  self._attr_native_value_attr_native_value = round(float(value), 1)
85 
86  @callback
87  def _after_update(self, hub) -> None:
88  self._read_state_read_state()
89  self.async_write_ha_stateasync_write_ha_state()
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:26