Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch Platform for Chacon Dio REV-LIGHT and switch plug devices."""
2 
3 import logging
4 from typing import Any
5 
6 from dio_chacon_wifi_api.const import DeviceTypeEnum
7 
8 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import ChaconDioConfigEntry
13 from .entity import ChaconDioEntity
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  hass: HomeAssistant,
20  config_entry: ChaconDioConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up Chacon Dio switch devices."""
24  data = config_entry.runtime_data
25  client = data.client
26 
28  ChaconDioSwitch(client, device)
29  for device in data.list_devices
30  if device["type"]
31  in (DeviceTypeEnum.SWITCH_LIGHT.value, DeviceTypeEnum.SWITCH_PLUG.value)
32  )
33 
34 
36  """Object for controlling a Chacon Dio switch."""
37 
38  _attr_device_class = SwitchDeviceClass.SWITCH
39  _attr_name = None
40 
41  def _update_attr(self, data: dict[str, Any]) -> None:
42  """Recomputes the attributes values either at init or when the device state changes."""
43  self._attr_available_attr_available = data["connected"]
44  self._attr_is_on_attr_is_on = data["is_on"]
45 
46  async def async_turn_on(self, **kwargs: Any) -> None:
47  """Turn on the switch.
48 
49  Turned on status is effective after the server callback that triggers callback_device_state.
50  """
51 
52  _LOGGER.debug(
53  "Turn on the switch %s , %s, %s",
54  self.target_id,
55  self.entity_identity_id,
56  self._attr_is_on_attr_is_on,
57  )
58 
59  await self.clientclient.switch_switch(self.target_id, True)
60 
61  async def async_turn_off(self, **kwargs: Any) -> None:
62  """Turn off the switch.
63 
64  Turned on status is effective after the server callback that triggers callback_device_state.
65  """
66 
67  _LOGGER.debug(
68  "Turn off the switch %s , %s, %s",
69  self.target_id,
70  self.entity_identity_id,
71  self._attr_is_on_attr_is_on,
72  )
73 
74  await self.clientclient.switch_switch(self.target_id, False)
None _update_attr(self, dict[str, Any] data)
Definition: switch.py:41
None async_setup_entry(HomeAssistant hass, ChaconDioConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:22