Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for Sun integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime
8 
10  DOMAIN as SENSOR_DOMAIN,
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import DEGREE, EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
19 from homeassistant.helpers.dispatcher import async_dispatcher_connect
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import StateType
22 
23 from .const import DOMAIN, SIGNAL_EVENTS_CHANGED, SIGNAL_POSITION_CHANGED
24 from .entity import Sun, SunConfigEntry
25 
26 ENTITY_ID_SENSOR_FORMAT = SENSOR_DOMAIN + ".sun_{}"
27 
28 
29 @dataclass(kw_only=True, frozen=True)
31  """Describes a Sun sensor entity."""
32 
33  value_fn: Callable[[Sun], StateType | datetime]
34  signal: str
35 
36 
37 SENSOR_TYPES: tuple[SunSensorEntityDescription, ...] = (
39  key="next_dawn",
40  device_class=SensorDeviceClass.TIMESTAMP,
41  translation_key="next_dawn",
42  value_fn=lambda data: data.next_dawn,
43  signal=SIGNAL_EVENTS_CHANGED,
44  ),
46  key="next_dusk",
47  device_class=SensorDeviceClass.TIMESTAMP,
48  translation_key="next_dusk",
49  value_fn=lambda data: data.next_dusk,
50  signal=SIGNAL_EVENTS_CHANGED,
51  ),
53  key="next_midnight",
54  device_class=SensorDeviceClass.TIMESTAMP,
55  translation_key="next_midnight",
56  value_fn=lambda data: data.next_midnight,
57  signal=SIGNAL_EVENTS_CHANGED,
58  ),
60  key="next_noon",
61  device_class=SensorDeviceClass.TIMESTAMP,
62  translation_key="next_noon",
63  value_fn=lambda data: data.next_noon,
64  signal=SIGNAL_EVENTS_CHANGED,
65  ),
67  key="next_rising",
68  device_class=SensorDeviceClass.TIMESTAMP,
69  translation_key="next_rising",
70  value_fn=lambda data: data.next_rising,
71  signal=SIGNAL_EVENTS_CHANGED,
72  ),
74  key="next_setting",
75  device_class=SensorDeviceClass.TIMESTAMP,
76  translation_key="next_setting",
77  value_fn=lambda data: data.next_setting,
78  signal=SIGNAL_EVENTS_CHANGED,
79  ),
81  key="solar_elevation",
82  translation_key="solar_elevation",
83  state_class=SensorStateClass.MEASUREMENT,
84  value_fn=lambda data: data.solar_elevation,
85  entity_registry_enabled_default=False,
86  native_unit_of_measurement=DEGREE,
87  signal=SIGNAL_POSITION_CHANGED,
88  ),
90  key="solar_azimuth",
91  translation_key="solar_azimuth",
92  state_class=SensorStateClass.MEASUREMENT,
93  value_fn=lambda data: data.solar_azimuth,
94  entity_registry_enabled_default=False,
95  native_unit_of_measurement=DEGREE,
96  signal=SIGNAL_POSITION_CHANGED,
97  ),
99  key="solar_rising",
100  translation_key="solar_rising",
101  value_fn=lambda data: data.rising,
102  entity_registry_enabled_default=False,
103  signal=SIGNAL_EVENTS_CHANGED,
104  ),
105 )
106 
107 
109  hass: HomeAssistant, entry: SunConfigEntry, async_add_entities: AddEntitiesCallback
110 ) -> None:
111  """Set up Sun sensor platform."""
112 
113  sun = entry.runtime_data
114 
116  [SunSensor(sun, description, entry.entry_id) for description in SENSOR_TYPES]
117  )
118 
119 
121  """Representation of a Sun Sensor."""
122 
123  _attr_has_entity_name = True
124  _attr_should_poll = False
125  _attr_entity_category = EntityCategory.DIAGNOSTIC
126  entity_description: SunSensorEntityDescription
127 
128  def __init__(
129  self, sun: Sun, entity_description: SunSensorEntityDescription, entry_id: str
130  ) -> None:
131  """Initiate Sun Sensor."""
132  self.entity_descriptionentity_description = entity_description
133  self.entity_identity_identity_id = ENTITY_ID_SENSOR_FORMAT.format(entity_description.key)
134  self._attr_unique_id_attr_unique_id = f"{entry_id}-{entity_description.key}"
135  self.sunsun = sun
136  self._attr_device_info_attr_device_info = DeviceInfo(
137  name="Sun",
138  identifiers={(DOMAIN, entry_id)},
139  entry_type=DeviceEntryType.SERVICE,
140  )
141 
142  @property
143  def native_value(self) -> StateType | datetime:
144  """Return value of sensor."""
145  return self.entity_descriptionentity_description.value_fn(self.sunsun)
146 
147  async def async_added_to_hass(self) -> None:
148  """Register signal listener when added to hass."""
149  await super().async_added_to_hass()
150  self.async_on_removeasync_on_remove(
152  self.hasshass,
153  self.entity_descriptionentity_description.signal,
154  self.async_write_ha_stateasync_write_ha_state,
155  )
156  )
None __init__(self, Sun sun, SunSensorEntityDescription entity_description, str entry_id)
Definition: sensor.py:130
StateType|datetime native_value(self)
Definition: sensor.py:143
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, SunConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:110
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103