Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for control of ElkM1 outputs (relays)."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from elkm1_lib.const import ThermostatMode, ThermostatSetting
8 from elkm1_lib.elements import Element
9 from elkm1_lib.elk import Elk
10 from elkm1_lib.outputs import Output
11 from elkm1_lib.thermostats import Thermostat
12 
13 from homeassistant.components.switch import SwitchEntity
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import ElkM1ConfigEntry
18 from .entity import ElkAttachedEntity, ElkEntity, create_elk_entities
19 from .models import ELKM1Data
20 
21 
23  hass: HomeAssistant,
24  config_entry: ElkM1ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Create the Elk-M1 switch platform."""
28  elk_data = config_entry.runtime_data
29  elk = elk_data.elk
30  entities: list[ElkEntity] = []
31  create_elk_entities(elk_data, elk.outputs, "output", ElkOutput, entities)
33  elk_data, elk.thermostats, "thermostat", ElkThermostatEMHeat, entities
34  )
35  async_add_entities(entities)
36 
37 
39  """Elk output as switch."""
40 
41  _element: Output
42 
43  @property
44  def is_on(self) -> bool:
45  """Get the current output status."""
46  return self._element_element.output_on
47 
48  async def async_turn_on(self, **kwargs: Any) -> None:
49  """Turn on the output."""
50  self._element_element.turn_on(0)
51 
52  async def async_turn_off(self, **kwargs: Any) -> None:
53  """Turn off the output."""
54  self._element_element.turn_off()
55 
56 
58  """Elk Thermostat emergency heat as switch."""
59 
60  _element: Thermostat
61 
62  def __init__(self, element: Element, elk: Elk, elk_data: ELKM1Data) -> None:
63  """Initialize the emergency heat switch."""
64  super().__init__(element, elk, elk_data)
65  self._unique_id_unique_id_unique_id = f"{self._unique_id}emheat"
66  self._attr_name_attr_name_attr_name = f"{element.name} emergency heat"
67 
68  @property
69  def is_on(self) -> bool:
70  """Get the current emergency heat status."""
71  return self._element_element.mode == ThermostatMode.EMERGENCY_HEAT
72 
73  def _elk_set(self, mode: ThermostatMode) -> None:
74  """Set the thermostat mode."""
75  self._element_element.set(ThermostatSetting.MODE, mode)
76 
77  async def async_turn_on(self, **kwargs: Any) -> None:
78  """Turn on the output."""
79  self._elk_set_elk_set(ThermostatMode.EMERGENCY_HEAT)
80 
81  async def async_turn_off(self, **kwargs: Any) -> None:
82  """Turn off the output."""
83  self._elk_set_elk_set(ThermostatMode.EMERGENCY_HEAT)
None async_turn_off(self, **Any kwargs)
Definition: switch.py:52
None async_turn_on(self, **Any kwargs)
Definition: switch.py:48
None _elk_set(self, ThermostatMode mode)
Definition: switch.py:73
None __init__(self, Element element, Elk elk, ELKM1Data elk_data)
Definition: switch.py:62
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
list[ElkEntity]|None create_elk_entities(ELKM1Data elk_data, Iterable[Element] elk_elements, str element_type, Any class_, list[ElkEntity] entities)
Definition: entity.py:30
None async_setup_entry(HomeAssistant hass, ElkM1ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:26