Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for SwitchBot switch."""
2 
3 from typing import Any
4 
5 from switchbot_api import CommonCommands, Device, PowerState, Remote, SwitchBotAPI
6 
7 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import SwitchbotCloudData
13 from .const import DOMAIN
14 from .coordinator import SwitchBotCoordinator
15 from .entity import SwitchBotCloudEntity
16 
17 
19  hass: HomeAssistant,
20  config: ConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up SwitchBot Cloud entry."""
24  data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id]
26  _async_make_entity(data.api, device, coordinator)
27  for device, coordinator in data.devices.switches
28  )
29 
30 
32  """Representation of a SwitchBot switch."""
33 
34  _attr_device_class = SwitchDeviceClass.SWITCH
35  _attr_name = None
36 
37  async def async_turn_on(self, **kwargs: Any) -> None:
38  """Turn the device on."""
39  await self.send_api_commandsend_api_command(CommonCommands.ON)
40  self._attr_is_on_attr_is_on = True
41  self.async_write_ha_stateasync_write_ha_state()
42 
43  async def async_turn_off(self, **kwargs: Any) -> None:
44  """Turn the device off."""
45  await self.send_api_commandsend_api_command(CommonCommands.OFF)
46  self._attr_is_on_attr_is_on = False
47  self.async_write_ha_stateasync_write_ha_state()
48 
49  @callback
50  def _handle_coordinator_update(self) -> None:
51  """Handle updated data from the coordinator."""
52  if not self.coordinator.data:
53  return
54  self._attr_is_on_attr_is_on = self.coordinator.data.get("power") == PowerState.ON.value
55  self.async_write_ha_stateasync_write_ha_state()
56 
57 
59  """Representation of a SwitchBot switch provider by a remote."""
60 
61  @callback
62  def _handle_coordinator_update(self) -> None:
63  """Handle updated data from the coordinator."""
64 
65 
66 class SwitchBotCloudPlugSwitch(SwitchBotCloudSwitch):
67  """Representation of a SwitchBot plug switch."""
68 
69  _attr_device_class = SwitchDeviceClass.OUTLET
70 
71 
72 @callback
74  api: SwitchBotAPI, device: Device | Remote, coordinator: SwitchBotCoordinator
75 ) -> SwitchBotCloudSwitch:
76  """Make a SwitchBotCloudSwitch or SwitchBotCloudRemoteSwitch."""
77  if isinstance(device, Remote):
78  return SwitchBotCloudRemoteSwitch(api, device, coordinator)
79  if "Plug" in device.device_type:
80  return SwitchBotCloudPlugSwitch(api, device, coordinator)
81  raise NotImplementedError(f"Unsupported device type: {device.device_type}")
None send_api_command(self, Commands command, str command_type="command", dict|str parameters="default")
Definition: entity.py:43
SwitchBotCloudSwitch _async_make_entity(SwitchBotAPI api, Device|Remote device, SwitchBotCoordinator coordinator)
Definition: switch.py:75
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)
Definition: switch.py:22