Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Creates the binary sensor entities for the mower."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 import logging
6 from typing import TYPE_CHECKING
7 
8 from aioautomower.model import MowerActivities, MowerAttributes
9 
10 from homeassistant.components.automation import automations_with_entity
12  DOMAIN as BINARY_SENSOR_DOMAIN,
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.components.script import scripts_with_entity
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 AutomowerConfigEntry
28 from .const import DOMAIN
29 from .coordinator import AutomowerDataUpdateCoordinator
30 from .entity import AutomowerBaseEntity
31 
32 _LOGGER = logging.getLogger(__name__)
33 
34 
35 def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
36  """Get list of related automations and scripts."""
37  used_in = automations_with_entity(hass, entity_id)
38  used_in += scripts_with_entity(hass, entity_id)
39  return used_in
40 
41 
42 @dataclass(frozen=True, kw_only=True)
44  """Describes Automower binary sensor entity."""
45 
46  value_fn: Callable[[MowerAttributes], bool]
47 
48 
49 MOWER_BINARY_SENSOR_TYPES: tuple[AutomowerBinarySensorEntityDescription, ...] = (
51  key="battery_charging",
52  value_fn=lambda data: data.mower.activity == MowerActivities.CHARGING,
53  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
54  ),
56  key="leaving_dock",
57  translation_key="leaving_dock",
58  value_fn=lambda data: data.mower.activity == MowerActivities.LEAVING,
59  ),
61  key="returning_to_dock",
62  translation_key="returning_to_dock",
63  value_fn=lambda data: data.mower.activity == MowerActivities.GOING_HOME,
64  entity_registry_enabled_default=False,
65  ),
66 )
67 
68 
70  hass: HomeAssistant,
71  entry: AutomowerConfigEntry,
72  async_add_entities: AddEntitiesCallback,
73 ) -> None:
74  """Set up binary sensor platform."""
75  coordinator = entry.runtime_data
77  AutomowerBinarySensorEntity(mower_id, coordinator, description)
78  for mower_id in coordinator.data
79  for description in MOWER_BINARY_SENSOR_TYPES
80  )
81 
82 
84  """Defining the Automower Sensors with AutomowerBinarySensorEntityDescription."""
85 
86  entity_description: AutomowerBinarySensorEntityDescription
87 
88  def __init__(
89  self,
90  mower_id: str,
91  coordinator: AutomowerDataUpdateCoordinator,
92  description: AutomowerBinarySensorEntityDescription,
93  ) -> None:
94  """Set up AutomowerSensors."""
95  super().__init__(mower_id, coordinator)
96  self.entity_descriptionentity_description = description
97  self._attr_unique_id_attr_unique_id = f"{mower_id}_{description.key}"
98 
99  @property
100  def is_on(self) -> bool:
101  """Return the state of the binary sensor."""
102  return self.entity_descriptionentity_description.value_fn(self.mower_attributesmower_attributes)
103 
104  async def async_added_to_hass(self) -> None:
105  """Raise issue when entity is registered and was not disabled."""
106  if TYPE_CHECKING:
107  assert self.unique_idunique_id
108  if not (
109  entity_id := er.async_get(self.hasshass).async_get_entity_id(
110  BINARY_SENSOR_DOMAIN, DOMAIN, self.unique_idunique_id
111  )
112  ):
113  return
114  if (
115  self.enabledenabled
116  and self.entity_descriptionentity_description.key == "returning_to_dock"
117  and entity_used_in(self.hasshass, entity_id)
118  ):
120  self.hasshass,
121  DOMAIN,
122  f"deprecated_entity_{self.entity_description.key}",
123  breaks_in_ha_version="2025.6.0",
124  is_fixable=False,
125  severity=IssueSeverity.WARNING,
126  translation_key="deprecated_entity",
127  translation_placeholders={
128  "entity_name": str(self.namename),
129  "entity": entity_id,
130  },
131  )
132  else:
134  self.hasshass,
135  DOMAIN,
136  f"deprecated_task_entity_{self.entity_description.key}",
137  )
138  await super().async_added_to_hass()
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator, AutomowerBinarySensorEntityDescription description)
str|UndefinedType|None name(self)
Definition: entity.py:738
list[str] automations_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:191
None async_setup_entry(HomeAssistant hass, AutomowerConfigEntry entry, AddEntitiesCallback async_add_entities)
list[str] entity_used_in(HomeAssistant hass, str entity_id)
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
list[str] scripts_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:126