Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor that can display the current Home Assistant versions."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
8 from homeassistant.const import CONF_NAME
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import StateType
12 
13 from . import VersionConfigEntry
14 from .const import CONF_SOURCE, DEFAULT_NAME
15 from .entity import VersionEntity
16 
17 
19  hass: HomeAssistant,
20  entry: VersionConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up version sensors."""
24  coordinator = entry.runtime_data
25  if (entity_name := entry.data[CONF_NAME]) == DEFAULT_NAME:
26  entity_name = entry.title
27 
28  version_sensor_entities: list[VersionSensorEntity] = [
30  coordinator=coordinator,
31  entity_description=SensorEntityDescription(
32  key=str(entry.data[CONF_SOURCE]),
33  name=entity_name,
34  translation_key="version",
35  ),
36  )
37  ]
38 
39  async_add_entities(version_sensor_entities)
40 
41 
43  """Version sensor entity class."""
44 
45  @property
46  def native_value(self) -> StateType:
47  """Return the native value of this sensor."""
48  return self.coordinator.version
49 
50  @property
51  def extra_state_attributes(self) -> dict[str, Any] | None:
52  """Return extra state attributes of this sensor."""
53  return self.coordinator.version_data
None async_setup_entry(HomeAssistant hass, VersionConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:22