Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Contains binary sensors exposed by the Starlink integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11  BinarySensorEntityDescription,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN
19 from .coordinator import StarlinkData
20 from .entity import StarlinkEntity
21 
22 
24  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
25 ) -> None:
26  """Set up all binary sensors for this entry."""
27  coordinator = hass.data[DOMAIN][entry.entry_id]
28 
30  StarlinkBinarySensorEntity(coordinator, description)
31  for description in BINARY_SENSORS
32  )
33 
34 
35 @dataclass(frozen=True, kw_only=True)
37  """Describes a Starlink binary sensor entity."""
38 
39  value_fn: Callable[[StarlinkData], bool | None]
40 
41 
43  """A BinarySensorEntity for Starlink devices. Handles creating unique IDs."""
44 
45  entity_description: StarlinkBinarySensorEntityDescription
46 
47  @property
48  def is_on(self) -> bool | None:
49  """Calculate the binary sensor value from the entity description."""
50  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
51 
52 
53 BINARY_SENSORS = [
55  key="update",
56  device_class=BinarySensorDeviceClass.UPDATE,
57  value_fn=lambda data: data.alert["alert_install_pending"],
58  ),
60  key="roaming",
61  translation_key="roaming",
62  value_fn=lambda data: data.alert["alert_roaming"],
63  ),
65  key="currently_obstructed",
66  translation_key="currently_obstructed",
67  device_class=BinarySensorDeviceClass.PROBLEM,
68  value_fn=lambda data: data.status["currently_obstructed"],
69  ),
71  key="heating",
72  translation_key="heating",
73  entity_category=EntityCategory.DIAGNOSTIC,
74  value_fn=lambda data: data.alert["alert_is_heating"],
75  ),
77  key="power_save_idle",
78  translation_key="power_save_idle",
79  entity_category=EntityCategory.DIAGNOSTIC,
80  value_fn=lambda data: data.alert["alert_is_power_save_idle"],
81  ),
83  key="mast_near_vertical",
84  translation_key="mast_near_vertical",
85  device_class=BinarySensorDeviceClass.PROBLEM,
86  entity_category=EntityCategory.DIAGNOSTIC,
87  value_fn=lambda data: data.alert["alert_mast_not_near_vertical"],
88  ),
90  key="motors_stuck",
91  translation_key="motors_stuck",
92  device_class=BinarySensorDeviceClass.PROBLEM,
93  entity_category=EntityCategory.DIAGNOSTIC,
94  value_fn=lambda data: data.alert["alert_motors_stuck"],
95  ),
97  key="slow_ethernet",
98  translation_key="slow_ethernet",
99  device_class=BinarySensorDeviceClass.PROBLEM,
100  entity_category=EntityCategory.DIAGNOSTIC,
101  value_fn=lambda data: data.alert["alert_slow_ethernet_speeds"],
102  ),
104  key="thermal_throttle",
105  translation_key="thermal_throttle",
106  device_class=BinarySensorDeviceClass.PROBLEM,
107  entity_category=EntityCategory.DIAGNOSTIC,
108  value_fn=lambda data: data.alert["alert_thermal_throttle"],
109  ),
111  key="unexpected_location",
112  translation_key="unexpected_location",
113  device_class=BinarySensorDeviceClass.PROBLEM,
114  entity_category=EntityCategory.DIAGNOSTIC,
115  value_fn=lambda data: data.alert["alert_unexpected_location"],
116  ),
117 ]