Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor platform for Version."""
2 
3 from __future__ import annotations
4 
5 from awesomeversion import AwesomeVersion
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.const import CONF_NAME, EntityCategory, __version__ as HA_VERSION
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import VersionConfigEntry
17 from .const import CONF_SOURCE, DEFAULT_NAME
18 from .entity import VersionEntity
19 
20 HA_VERSION_OBJECT = AwesomeVersion(HA_VERSION)
21 
22 
24  hass: HomeAssistant,
25  config_entry: VersionConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up version binary_sensors."""
29  coordinator = config_entry.runtime_data
30  if (source := config_entry.data[CONF_SOURCE]) == "local":
31  return
32 
33  if (entity_name := config_entry.data[CONF_NAME]) == DEFAULT_NAME:
34  entity_name = config_entry.title
35 
36  entities: list[VersionBinarySensor] = [
38  coordinator=coordinator,
39  entity_description=BinarySensorEntityDescription(
40  key=str(source),
41  name=f"{entity_name} Update Available",
42  device_class=BinarySensorDeviceClass.UPDATE,
43  entity_category=EntityCategory.DIAGNOSTIC,
44  ),
45  )
46  ]
47 
48  async_add_entities(entities)
49 
50 
52  """Binary sensor for version entities."""
53 
54  entity_description: BinarySensorEntityDescription
55 
56  @property
57  def is_on(self) -> bool:
58  """Return true if the binary sensor is on."""
59  version = self.coordinator.version
60  return version is not None and (version > HA_VERSION_OBJECT)
None async_setup_entry(HomeAssistant hass, VersionConfigEntry config_entry, AddEntitiesCallback async_add_entities)