Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensors for key RainMachine data."""
2 
3 from dataclasses import dataclass
4 
6  DOMAIN as BINARY_SENSOR_DOMAIN,
7  BinarySensorEntity,
8  BinarySensorEntityDescription,
9 )
10 from homeassistant.const import EntityCategory
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import RainMachineConfigEntry
15 from .const import DATA_PROVISION_SETTINGS, DATA_RESTRICTIONS_CURRENT
16 from .entity import RainMachineEntity, RainMachineEntityDescription
17 from .util import (
18  EntityDomainReplacementStrategy,
19  async_finish_entity_domain_replacements,
20  key_exists,
21 )
22 
23 TYPE_FLOW_SENSOR = "flow_sensor"
24 TYPE_FREEZE = "freeze"
25 TYPE_HOURLY = "hourly"
26 TYPE_MONTH = "month"
27 TYPE_RAINDELAY = "raindelay"
28 TYPE_RAINSENSOR = "rainsensor"
29 TYPE_WEEKDAY = "weekday"
30 
31 
32 @dataclass(frozen=True, kw_only=True)
34  BinarySensorEntityDescription, RainMachineEntityDescription
35 ):
36  """Describe a RainMachine binary sensor."""
37 
38  data_key: str
39 
40 
41 BINARY_SENSOR_DESCRIPTIONS = (
43  key=TYPE_FLOW_SENSOR,
44  translation_key=TYPE_FLOW_SENSOR,
45  api_category=DATA_PROVISION_SETTINGS,
46  data_key="useFlowSensor",
47  ),
49  key=TYPE_FREEZE,
50  translation_key=TYPE_FREEZE,
51  entity_category=EntityCategory.DIAGNOSTIC,
52  api_category=DATA_RESTRICTIONS_CURRENT,
53  data_key="freeze",
54  ),
56  key=TYPE_HOURLY,
57  translation_key=TYPE_HOURLY,
58  entity_category=EntityCategory.DIAGNOSTIC,
59  api_category=DATA_RESTRICTIONS_CURRENT,
60  data_key="hourly",
61  ),
63  key=TYPE_MONTH,
64  translation_key=TYPE_MONTH,
65  entity_category=EntityCategory.DIAGNOSTIC,
66  api_category=DATA_RESTRICTIONS_CURRENT,
67  data_key="month",
68  ),
70  key=TYPE_RAINDELAY,
71  translation_key=TYPE_RAINDELAY,
72  entity_category=EntityCategory.DIAGNOSTIC,
73  api_category=DATA_RESTRICTIONS_CURRENT,
74  data_key="rainDelay",
75  ),
77  key=TYPE_RAINSENSOR,
78  translation_key=TYPE_RAINSENSOR,
79  entity_category=EntityCategory.DIAGNOSTIC,
80  entity_registry_enabled_default=False,
81  api_category=DATA_RESTRICTIONS_CURRENT,
82  data_key="rainSensor",
83  ),
85  key=TYPE_WEEKDAY,
86  translation_key=TYPE_WEEKDAY,
87  entity_category=EntityCategory.DIAGNOSTIC,
88  api_category=DATA_RESTRICTIONS_CURRENT,
89  data_key="weekDay",
90  ),
91 )
92 
93 
95  hass: HomeAssistant,
96  entry: RainMachineConfigEntry,
97  async_add_entities: AddEntitiesCallback,
98 ) -> None:
99  """Set up RainMachine binary sensors based on a config entry."""
100  data = entry.runtime_data
101 
103  hass,
104  entry,
105  (
107  BINARY_SENSOR_DOMAIN,
108  f"{data.controller.mac}_freeze_protection",
109  f"switch.{data.controller.name.lower()}_freeze_protect_enabled",
110  breaks_in_ha_version="2022.12.0",
111  remove_old_entity=True,
112  ),
114  BINARY_SENSOR_DOMAIN,
115  f"{data.controller.mac}_extra_water_on_hot_days",
116  f"switch.{data.controller.name.lower()}_hot_days_extra_watering",
117  breaks_in_ha_version="2022.12.0",
118  remove_old_entity=True,
119  ),
120  ),
121  )
122 
123  api_category_sensor_map = {
124  DATA_PROVISION_SETTINGS: ProvisionSettingsBinarySensor,
125  DATA_RESTRICTIONS_CURRENT: CurrentRestrictionsBinarySensor,
126  }
127 
129  api_category_sensor_map[description.api_category](entry, data, description)
130  for description in BINARY_SENSOR_DESCRIPTIONS
131  if (
132  (coordinator := data.coordinators[description.api_category]) is not None
133  and coordinator.data
134  and key_exists(coordinator.data, description.data_key)
135  )
136  )
137 
138 
140  """Define a binary sensor that handles current restrictions data."""
141 
142  entity_description: RainMachineBinarySensorDescription
143 
144  @callback
145  def update_from_latest_data(self) -> None:
146  """Update the state."""
147  if self.entity_descriptionentity_description.key == TYPE_FREEZE:
148  self._attr_is_on_attr_is_on = self.coordinator.data.get("freeze")
149  elif self.entity_descriptionentity_description.key == TYPE_HOURLY:
150  self._attr_is_on_attr_is_on = self.coordinator.data.get("hourly")
151  elif self.entity_descriptionentity_description.key == TYPE_MONTH:
152  self._attr_is_on_attr_is_on = self.coordinator.data.get("month")
153  elif self.entity_descriptionentity_description.key == TYPE_RAINDELAY:
154  self._attr_is_on_attr_is_on = self.coordinator.data.get("rainDelay")
155  elif self.entity_descriptionentity_description.key == TYPE_RAINSENSOR:
156  self._attr_is_on_attr_is_on = self.coordinator.data.get("rainSensor")
157  elif self.entity_descriptionentity_description.key == TYPE_WEEKDAY:
158  self._attr_is_on_attr_is_on = self.coordinator.data.get("weekDay")
159 
160 
162  """Define a binary sensor that handles provisioning data."""
163 
164  entity_description: RainMachineBinarySensorDescription
165 
166  @callback
167  def update_from_latest_data(self) -> None:
168  """Update the state."""
169  if self.entity_descriptionentity_description.key == TYPE_FLOW_SENSOR:
170  self._attr_is_on_attr_is_on = self.coordinator.data.get("system", {}).get(
171  "useFlowSensor"
172  )
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_finish_entity_domain_replacements(HomeAssistant hass, ConfigEntry entry, Iterable[EntityDomainReplacementStrategy] entity_replacement_strategies)
Definition: util.py:41
None async_setup_entry(HomeAssistant hass, RainMachineConfigEntry entry, AddEntitiesCallback async_add_entities)
bool key_exists(dict[str, Any] data, str search_key)
Definition: util.py:70