1 """Support for SwitchBee light."""
3 from __future__
import annotations
7 from switchbee.api.central_unit
import SwitchBeeDeviceOfflineError, SwitchBeeError
8 from switchbee.device
import ApiStateCommand, DeviceType, SwitchBeeDimmer
16 from .const
import DOMAIN
17 from .coordinator
import SwitchBeeCoordinator
18 from .entity
import SwitchBeeDeviceEntity
24 """Convert hass brightness to SwitchBee."""
25 sb_brightness =
int(100 * value / MAX_BRIGHTNESS)
27 return sb_brightness
if sb_brightness != 100
else 99
31 """Convert SwitchBee brightness to hass."""
34 return round(value * MAX_BRIGHTNESS / 100)
38 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
40 """Set up SwitchBee light."""
41 coordinator = hass.data[DOMAIN][entry.entry_id]
44 for switchbee_device
in coordinator.data.values()
45 if switchbee_device.type == DeviceType.Dimmer
50 """Representation of a SwitchBee light."""
52 _attr_color_mode = ColorMode.BRIGHTNESS
53 _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
57 device: SwitchBeeDimmer,
58 coordinator: SwitchBeeCoordinator,
60 """Initialize the SwitchBee light."""
61 super().
__init__(device, coordinator)
69 """Handle updated data from the coordinator."""
74 coordinator_device = self._get_coordinator_device()
75 brightness = coordinator_device.brightness
79 self._check_if_became_offline()
82 self._check_if_became_online()
87 if 0 < brightness < 100:
91 """Async function to set on to light."""
92 if ATTR_BRIGHTNESS
in kwargs:
95 state = ApiStateCommand.ON
100 await self.coordinator.api.set_state(self._device.id, state)
101 except (SwitchBeeError, SwitchBeeDeviceOfflineError)
as exp:
103 f
"Failed to set {self.name} state {state}, {exp!s}"
106 if not isinstance(state, int):
114 self._get_coordinator_device().brightness = state
115 self.coordinator.async_set_updated_data(self.coordinator.data)
118 """Turn off SwitchBee light."""
120 await self.coordinator.api.set_state(self._device.id, ApiStateCommand.OFF)
121 except (SwitchBeeError, SwitchBeeDeviceOfflineError)
as exp:
123 f
"Failed to turn off {self._attr_name}, {exp!s}"
127 self._get_coordinator_device().brightness = 0
128 self.coordinator.async_set_updated_data(self.coordinator.data)
int|None brightness(self)
None async_turn_off(self, **Any kwargs)
None _update_attrs_from_coordinator(self)
None async_turn_on(self, **Any kwargs)
None __init__(self, SwitchBeeDimmer device, SwitchBeeCoordinator coordinator)
None _handle_coordinator_update(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
int _hass_brightness_to_switchbee(int value)
int _switchbee_brightness_to_hass(int value)