Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for TechnoVE binary sensor."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import TYPE_CHECKING
8 
9 from technove import Station as TechnoVEStation
10 
12  DOMAIN as BINARY_SENSOR_DOMAIN,
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers import entity_registry as er
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22  IssueSeverity,
23  async_create_issue,
24  async_delete_issue,
25 )
26 
27 from . import TechnoVEConfigEntry
28 from .const import DOMAIN
29 from .coordinator import TechnoVEDataUpdateCoordinator
30 from .entity import TechnoVEEntity
31 
32 
33 @dataclass(frozen=True, kw_only=True)
35  """Describes TechnoVE binary sensor entity."""
36 
37  deprecated_version: str | None = None
38  value_fn: Callable[[TechnoVEStation], bool | None]
39 
40 
41 BINARY_SENSORS = [
43  key="conflict_in_sharing_config",
44  translation_key="conflict_in_sharing_config",
45  entity_category=EntityCategory.DIAGNOSTIC,
46  value_fn=lambda station: station.info.conflict_in_sharing_config,
47  ),
49  key="in_sharing_mode",
50  translation_key="in_sharing_mode",
51  entity_category=EntityCategory.DIAGNOSTIC,
52  value_fn=lambda station: station.info.in_sharing_mode,
53  ),
55  key="is_battery_protected",
56  translation_key="is_battery_protected",
57  entity_category=EntityCategory.DIAGNOSTIC,
58  value_fn=lambda station: station.info.is_battery_protected,
59  ),
61  key="is_session_active",
62  entity_category=EntityCategory.DIAGNOSTIC,
63  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
64  value_fn=lambda station: station.info.is_session_active,
65  deprecated_version="2025.2.0",
66  # Disabled by default, as this entity is deprecated
67  entity_registry_enabled_default=False,
68  ),
70  key="is_static_ip",
71  translation_key="is_static_ip",
72  entity_category=EntityCategory.DIAGNOSTIC,
73  entity_registry_enabled_default=False,
74  value_fn=lambda station: station.info.is_static_ip,
75  ),
77  key="update_available",
78  entity_category=EntityCategory.DIAGNOSTIC,
79  device_class=BinarySensorDeviceClass.UPDATE,
80  value_fn=lambda station: not station.info.is_up_to_date,
81  ),
82 ]
83 
84 
86  hass: HomeAssistant,
87  entry: TechnoVEConfigEntry,
88  async_add_entities: AddEntitiesCallback,
89 ) -> None:
90  """Set up the binary sensor platform."""
92  TechnoVEBinarySensorEntity(entry.runtime_data, description)
93  for description in BINARY_SENSORS
94  )
95 
96 
98  """Defines a TechnoVE binary sensor entity."""
99 
100  entity_description: TechnoVEBinarySensorDescription
101 
102  def __init__(
103  self,
104  coordinator: TechnoVEDataUpdateCoordinator,
105  description: TechnoVEBinarySensorDescription,
106  ) -> None:
107  """Initialize a TechnoVE binary sensor entity."""
108  self.entity_descriptionentity_description = description
109  super().__init__(coordinator, description.key)
110 
111  @property
112  def is_on(self) -> bool | None:
113  """Return the state of the sensor."""
114 
115  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
116 
117  async def async_added_to_hass(self) -> None:
118  """Raise issue when entity is registered and was not disabled."""
119  if TYPE_CHECKING:
120  assert self.unique_idunique_id
121  if entity_id := er.async_get(self.hasshasshass).async_get_entity_id(
122  BINARY_SENSOR_DOMAIN, DOMAIN, self.unique_idunique_id
123  ):
124  if self.enabledenabled and self.entity_descriptionentity_description.deprecated_version:
126  self.hasshasshass,
127  DOMAIN,
128  f"deprecated_entity_{self.entity_description.key}",
129  breaks_in_ha_version=self.entity_descriptionentity_description.deprecated_version,
130  is_fixable=False,
131  severity=IssueSeverity.WARNING,
132  translation_key=f"deprecated_entity_{self.entity_description.key}",
133  translation_placeholders={
134  "sensor_name": self.namenamename
135  if isinstance(self.namenamename, str)
136  else entity_id,
137  "entity": entity_id,
138  },
139  )
140  else:
142  self.hasshasshass,
143  DOMAIN,
144  f"deprecated_entity_{self.entity_description.key}",
145  )
146  await super().async_added_to_hass()
None __init__(self, TechnoVEDataUpdateCoordinator coordinator, TechnoVEBinarySensorDescription description)
str|UndefinedType|None name(self)
Definition: entity.py:738
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
None async_delete_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:85
None async_setup_entry(HomeAssistant hass, TechnoVEConfigEntry entry, AddEntitiesCallback async_add_entities)