Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for 1-Wire environment switches."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import os
7 from typing import Any
8 
9 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
10 from homeassistant.const import EntityCategory
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import OneWireConfigEntry
15 from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
16 from .entity import OneWireEntity, OneWireEntityDescription
17 from .onewirehub import OneWireHub
18 
19 
20 @dataclass(frozen=True)
22  """Class describing OneWire switch entities."""
23 
24 
25 DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
26  "05": (
28  key="PIO",
29  entity_registry_enabled_default=False,
30  read_mode=READ_MODE_BOOL,
31  translation_key="pio",
32  ),
33  ),
34  "12": tuple(
35  [
37  key=f"PIO.{device_key}",
38  entity_registry_enabled_default=False,
39  read_mode=READ_MODE_BOOL,
40  translation_key="pio_id",
41  translation_placeholders={"id": str(device_key)},
42  )
43  for device_key in DEVICE_KEYS_A_B
44  ]
45  + [
47  key=f"latch.{device_key}",
48  entity_registry_enabled_default=False,
49  read_mode=READ_MODE_BOOL,
50  translation_key="latch_id",
51  translation_placeholders={"id": str(device_key)},
52  )
53  for device_key in DEVICE_KEYS_A_B
54  ]
55  ),
56  "26": (
58  key="IAD",
59  entity_registry_enabled_default=False,
60  entity_category=EntityCategory.CONFIG,
61  read_mode=READ_MODE_BOOL,
62  translation_key="iad",
63  ),
64  ),
65  "29": tuple(
66  [
68  key=f"PIO.{device_key}",
69  entity_registry_enabled_default=False,
70  read_mode=READ_MODE_BOOL,
71  translation_key="pio_id",
72  translation_placeholders={"id": str(device_key)},
73  )
74  for device_key in DEVICE_KEYS_0_7
75  ]
76  + [
78  key=f"latch.{device_key}",
79  entity_registry_enabled_default=False,
80  read_mode=READ_MODE_BOOL,
81  translation_key="latch_id",
82  translation_placeholders={"id": str(device_key)},
83  )
84  for device_key in DEVICE_KEYS_0_7
85  ]
86  ),
87  "3A": tuple(
89  key=f"PIO.{device_key}",
90  entity_registry_enabled_default=False,
91  read_mode=READ_MODE_BOOL,
92  translation_key="pio_id",
93  translation_placeholders={"id": str(device_key)},
94  )
95  for device_key in DEVICE_KEYS_A_B
96  ),
97  "EF": (), # "HobbyBoard": special
98 }
99 
100 # EF sensors are usually hobbyboards specialized sensors.
101 
102 HOBBYBOARD_EF: dict[str, tuple[OneWireEntityDescription, ...]] = {
103  "HB_HUB": tuple(
105  key=f"hub/branch.{device_key}",
106  entity_registry_enabled_default=False,
107  read_mode=READ_MODE_BOOL,
108  entity_category=EntityCategory.CONFIG,
109  translation_key="hub_branch_id",
110  translation_placeholders={"id": str(device_key)},
111  )
112  for device_key in DEVICE_KEYS_0_3
113  ),
114  "HB_MOISTURE_METER": tuple(
115  [
117  key=f"moisture/is_leaf.{device_key}",
118  entity_registry_enabled_default=False,
119  read_mode=READ_MODE_BOOL,
120  entity_category=EntityCategory.CONFIG,
121  translation_key="leaf_sensor_id",
122  translation_placeholders={"id": str(device_key)},
123  )
124  for device_key in DEVICE_KEYS_0_3
125  ]
126  + [
128  key=f"moisture/is_moisture.{device_key}",
129  entity_registry_enabled_default=False,
130  read_mode=READ_MODE_BOOL,
131  entity_category=EntityCategory.CONFIG,
132  translation_key="moisture_sensor_id",
133  translation_placeholders={"id": str(device_key)},
134  )
135  for device_key in DEVICE_KEYS_0_3
136  ]
137  ),
138 }
139 
140 
142  device_sub_type: str,
143 ) -> dict[str, tuple[OneWireEntityDescription, ...]]:
144  """Return the proper info array for the device type."""
145  if "HobbyBoard" in device_sub_type:
146  return HOBBYBOARD_EF
147  return DEVICE_SWITCHES
148 
149 
151  hass: HomeAssistant,
152  config_entry: OneWireConfigEntry,
153  async_add_entities: AddEntitiesCallback,
154 ) -> None:
155  """Set up 1-Wire platform."""
156  entities = await hass.async_add_executor_job(
157  get_entities, config_entry.runtime_data
158  )
159  async_add_entities(entities, True)
160 
161 
162 def get_entities(onewire_hub: OneWireHub) -> list[OneWireSwitch]:
163  """Get a list of entities."""
164  if not onewire_hub.devices:
165  return []
166 
167  entities: list[OneWireSwitch] = []
168 
169  for device in onewire_hub.devices:
170  family = device.family
171  device_type = device.type
172  device_id = device.id
173  device_info = device.device_info
174  device_sub_type = "std"
175  if device_type and "EF" in family:
176  device_sub_type = "HobbyBoard"
177  family = device_type
178  elif "A6" in family:
179  # A6 is a secondary family code for DS2438
180  family = "26"
181 
182  if family not in get_sensor_types(device_sub_type):
183  continue
184  for description in get_sensor_types(device_sub_type)[family]:
185  device_file = os.path.join(os.path.split(device.path)[0], description.key)
186  entities.append(
188  description=description,
189  device_id=device_id,
190  device_file=device_file,
191  device_info=device_info,
192  owproxy=onewire_hub.owproxy,
193  )
194  )
195 
196  return entities
197 
198 
200  """Implementation of a 1-Wire switch."""
201 
202  entity_description: OneWireSwitchEntityDescription
203 
204  @property
205  def is_on(self) -> bool | None:
206  """Return true if switch is on."""
207  if self._state_state is None:
208  return None
209  return bool(self._state_state)
210 
211  def turn_on(self, **kwargs: Any) -> None:
212  """Turn the entity on."""
213  self._write_value_write_value(b"1")
214 
215  def turn_off(self, **kwargs: Any) -> None:
216  """Turn the entity off."""
217  self._write_value_write_value(b"0")
None async_setup_entry(HomeAssistant hass, OneWireConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:154
dict[str, tuple[OneWireEntityDescription,...]] get_sensor_types(str device_sub_type)
Definition: switch.py:143
list[OneWireSwitch] get_entities(OneWireHub onewire_hub)
Definition: switch.py:162