-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathbinary_sensor.py
117 lines (101 loc) · 4.14 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Contains binary sensors exposed by the Starlink integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import StarlinkData
from .entity import StarlinkEntity
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up all binary sensors for this entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
StarlinkBinarySensorEntity(coordinator, description)
for description in BINARY_SENSORS
)
@dataclass(frozen=True, kw_only=True)
class StarlinkBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes a Starlink binary sensor entity."""
value_fn: Callable[[StarlinkData], bool | None]
class StarlinkBinarySensorEntity(StarlinkEntity, BinarySensorEntity):
"""A BinarySensorEntity for Starlink devices. Handles creating unique IDs."""
entity_description: StarlinkBinarySensorEntityDescription
@property
def is_on(self) -> bool | None:
"""Calculate the binary sensor value from the entity description."""
return self.entity_description.value_fn(self.coordinator.data)
BINARY_SENSORS = [
StarlinkBinarySensorEntityDescription(
key="update",
device_class=BinarySensorDeviceClass.UPDATE,
value_fn=lambda data: data.alert["alert_install_pending"],
),
StarlinkBinarySensorEntityDescription(
key="roaming",
translation_key="roaming",
value_fn=lambda data: data.alert["alert_roaming"],
),
StarlinkBinarySensorEntityDescription(
key="currently_obstructed",
translation_key="currently_obstructed",
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: data.status["currently_obstructed"],
),
StarlinkBinarySensorEntityDescription(
key="heating",
translation_key="heating",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_is_heating"],
),
StarlinkBinarySensorEntityDescription(
key="power_save_idle",
translation_key="power_save_idle",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_is_power_save_idle"],
),
StarlinkBinarySensorEntityDescription(
key="mast_near_vertical",
translation_key="mast_near_vertical",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_mast_not_near_vertical"],
),
StarlinkBinarySensorEntityDescription(
key="motors_stuck",
translation_key="motors_stuck",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_motors_stuck"],
),
StarlinkBinarySensorEntityDescription(
key="slow_ethernet",
translation_key="slow_ethernet",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_slow_ethernet_speeds"],
),
StarlinkBinarySensorEntityDescription(
key="thermal_throttle",
translation_key="thermal_throttle",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_thermal_throttle"],
),
StarlinkBinarySensorEntityDescription(
key="unexpected_location",
translation_key="unexpected_location",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.alert["alert_unexpected_location"],
),
]