1 """Support for SwitchBee switch."""
3 from __future__
import annotations
7 from switchbee.api.central_unit
import SwitchBeeDeviceOfflineError, SwitchBeeError
8 from switchbee.device
import (
22 from .const
import DOMAIN
23 from .coordinator
import SwitchBeeCoordinator
24 from .entity
import SwitchBeeDeviceEntity
28 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
30 """Set up Switchbee switch."""
31 coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id]
35 for device
in coordinator.data.values()
49 _DeviceTypeT: SwitchBeeTimedSwitch
50 | SwitchBeeGroupSwitch
52 | SwitchBeeTimerSwitch
53 ](SwitchBeeDeviceEntity[_DeviceTypeT], SwitchEntity):
54 """Representation of a Switchbee switch."""
59 coordinator: SwitchBeeCoordinator,
61 """Initialize the Switchbee switch."""
62 super().
__init__(device, coordinator)
63 self._attr_is_on =
False
67 """Handle updated data from the coordinator."""
68 self._update_from_coordinator()
72 """Update the entity attributes from the coordinator data."""
74 coordinator_device = self._get_coordinator_device()
76 if coordinator_device.state == -1:
77 self._check_if_became_offline()
80 self._check_if_became_online()
84 self._attr_is_on = coordinator_device.state != ApiStateCommand.OFF
87 """Async function to set on to switch."""
88 return await self._async_set_state(ApiStateCommand.ON)
91 """Async function to set off to switch."""
92 return await self._async_set_state(ApiStateCommand.OFF)
96 await self.coordinator.api.set_state(self._device.id, state)
97 except (SwitchBeeError, SwitchBeeDeviceOfflineError)
as exp:
98 await self.coordinator.async_refresh()
100 f
"Failed to set {self._attr_name} state {state}, {exp!s}"
103 await self.coordinator.async_refresh()
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None _update_from_coordinator(self)
None _async_set_state(self, str state)
None _handle_coordinator_update(self)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None __init__(self, _DeviceTypeT device, SwitchBeeCoordinator coordinator)