Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Ecovacs switch module."""
2 
3 from dataclasses import dataclass
4 from typing import Any
5 
6 from deebot_client.capabilities import CapabilitySetEnable
7 from deebot_client.events import EnableEvent
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 EcovacsConfigEntry
15 from .entity import (
16  EcovacsCapabilityEntityDescription,
17  EcovacsDescriptionEntity,
18  EcovacsEntity,
19 )
20 from .util import get_supported_entitites
21 
22 
23 @dataclass(kw_only=True, frozen=True)
25  SwitchEntityDescription,
26  EcovacsCapabilityEntityDescription[CapabilitySetEnable],
27 ):
28  """Ecovacs switch entity description."""
29 
30 
31 ENTITY_DESCRIPTIONS: tuple[EcovacsSwitchEntityDescription, ...] = (
33  capability_fn=lambda c: c.settings.advanced_mode,
34  key="advanced_mode",
35  translation_key="advanced_mode",
36  entity_registry_enabled_default=False,
37  entity_category=EntityCategory.CONFIG,
38  ),
40  capability_fn=lambda c: c.clean.continuous,
41  key="continuous_cleaning",
42  translation_key="continuous_cleaning",
43  entity_registry_enabled_default=False,
44  entity_category=EntityCategory.CONFIG,
45  ),
47  capability_fn=lambda c: c.settings.carpet_auto_fan_boost,
48  key="carpet_auto_fan_boost",
49  translation_key="carpet_auto_fan_boost",
50  entity_registry_enabled_default=False,
51  entity_category=EntityCategory.CONFIG,
52  ),
54  capability_fn=lambda c: c.clean.preference,
55  key="clean_preference",
56  translation_key="clean_preference",
57  entity_registry_enabled_default=False,
58  entity_category=EntityCategory.CONFIG,
59  ),
61  capability_fn=lambda c: c.settings.true_detect,
62  key="true_detect",
63  translation_key="true_detect",
64  entity_registry_enabled_default=False,
65  entity_category=EntityCategory.CONFIG,
66  ),
68  capability_fn=lambda c: c.settings.border_switch,
69  key="border_switch",
70  translation_key="border_switch",
71  entity_registry_enabled_default=False,
72  entity_category=EntityCategory.CONFIG,
73  ),
75  capability_fn=lambda c: c.settings.child_lock,
76  key="child_lock",
77  translation_key="child_lock",
78  entity_registry_enabled_default=False,
79  entity_category=EntityCategory.CONFIG,
80  ),
82  capability_fn=lambda c: c.settings.moveup_warning,
83  key="move_up_warning",
84  translation_key="move_up_warning",
85  entity_registry_enabled_default=False,
86  entity_category=EntityCategory.CONFIG,
87  ),
89  capability_fn=lambda c: c.settings.cross_map_border_warning,
90  key="cross_map_border_warning",
91  translation_key="cross_map_border_warning",
92  entity_registry_enabled_default=False,
93  entity_category=EntityCategory.CONFIG,
94  ),
96  capability_fn=lambda c: c.settings.safe_protect,
97  key="safe_protect",
98  translation_key="safe_protect",
99  entity_registry_enabled_default=False,
100  entity_category=EntityCategory.CONFIG,
101  ),
102 )
103 
104 
106  hass: HomeAssistant,
107  config_entry: EcovacsConfigEntry,
108  async_add_entities: AddEntitiesCallback,
109 ) -> None:
110  """Add entities for passed config_entry in HA."""
111  controller = config_entry.runtime_data
112  entities: list[EcovacsEntity] = get_supported_entitites(
113  controller, EcovacsSwitchEntity, ENTITY_DESCRIPTIONS
114  )
115  if entities:
116  async_add_entities(entities)
117 
118 
120  EcovacsDescriptionEntity[CapabilitySetEnable],
121  SwitchEntity,
122 ):
123  """Ecovacs switch entity."""
124 
125  entity_description: EcovacsSwitchEntityDescription
126 
127  _attr_is_on = False
128 
129  async def async_added_to_hass(self) -> None:
130  """Set up the event listeners now that hass is ready."""
131  await super().async_added_to_hass()
132 
133  async def on_event(event: EnableEvent) -> None:
134  self._attr_is_on_attr_is_on_attr_is_on = event.enable
135  self.async_write_ha_stateasync_write_ha_state()
136 
137  self._subscribe_subscribe(self._capability_capability.event, on_event)
138 
139  async def async_turn_on(self, **kwargs: Any) -> None:
140  """Turn the entity on."""
141  await self._device_device.execute_command(self._capability_capability.set(True))
142 
143  async def async_turn_off(self, **kwargs: Any) -> None:
144  """Turn the entity off."""
145  await self._device_device.execute_command(self._capability_capability.set(False))
None _subscribe(self, type[EventT] event_type, Callable[[EventT], Coroutine[Any, Any, None]] callback)
Definition: entity.py:87
None async_setup_entry(HomeAssistant hass, EcovacsConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:109
list[EcovacsEntity] get_supported_entitites(EcovacsController controller, type[EcovacsDescriptionEntity] entity_class, tuple[EcovacsCapabilityEntityDescription,...] descriptions)
Definition: util.py:37