-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
/
Copy pathbinary_sensor.py
83 lines (66 loc) · 2.69 KB
/
binary_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Creates the binary sensor entities for the mower."""
from collections.abc import Callable
from dataclasses import dataclass
import logging
from aioautomower.model import MowerActivities, MowerAttributes
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AutomowerConfigEntry
from .coordinator import AutomowerDataUpdateCoordinator
from .entity import AutomowerBaseEntity
_LOGGER = logging.getLogger(__name__)
@dataclass(frozen=True, kw_only=True)
class AutomowerBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Automower binary sensor entity."""
value_fn: Callable[[MowerAttributes], bool]
BINARY_SENSOR_TYPES: tuple[AutomowerBinarySensorEntityDescription, ...] = (
AutomowerBinarySensorEntityDescription(
key="battery_charging",
value_fn=lambda data: data.mower.activity == MowerActivities.CHARGING,
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
),
AutomowerBinarySensorEntityDescription(
key="leaving_dock",
translation_key="leaving_dock",
value_fn=lambda data: data.mower.activity == MowerActivities.LEAVING,
),
AutomowerBinarySensorEntityDescription(
key="returning_to_dock",
translation_key="returning_to_dock",
value_fn=lambda data: data.mower.activity == MowerActivities.GOING_HOME,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: AutomowerConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up binary sensor platform."""
coordinator = entry.runtime_data
async_add_entities(
AutomowerBinarySensorEntity(mower_id, coordinator, description)
for mower_id in coordinator.data
for description in BINARY_SENSOR_TYPES
)
class AutomowerBinarySensorEntity(AutomowerBaseEntity, BinarySensorEntity):
"""Defining the Automower Sensors with AutomowerBinarySensorEntityDescription."""
entity_description: AutomowerBinarySensorEntityDescription
def __init__(
self,
mower_id: str,
coordinator: AutomowerDataUpdateCoordinator,
description: AutomowerBinarySensorEntityDescription,
) -> None:
"""Set up AutomowerSensors."""
super().__init__(mower_id, coordinator)
self.entity_description = description
self._attr_unique_id = f"{mower_id}_{description.key}"
@property
def is_on(self) -> bool:
"""Return the state of the binary sensor."""
return self.entity_description.value_fn(self.mower_attributes)