-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add support for SwitchBot Ceiling Lights #159072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d55eabe
18fbc03
77d2a8f
77161bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| from switchbot_api import ( | ||||||||||||||||||||||||||||||||
| CeilingLightCommands, | ||||||||||||||||||||||||||||||||
| CommonCommands, | ||||||||||||||||||||||||||||||||
| Device, | ||||||||||||||||||||||||||||||||
| Remote, | ||||||||||||||||||||||||||||||||
|
|
@@ -53,6 +54,16 @@ class SwitchBotCloudLight(SwitchBotCloudEntity, LightEntity): | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _attr_color_mode = ColorMode.UNKNOWN | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def _get_default_color_mode(self) -> ColorMode: | ||||||||||||||||||||||||||||||||
| """Return the default color mode.""" | ||||||||||||||||||||||||||||||||
| if not self.supported_color_modes: | ||||||||||||||||||||||||||||||||
| return ColorMode.UNKNOWN | ||||||||||||||||||||||||||||||||
| if ColorMode.RGB in self.supported_color_modes: | ||||||||||||||||||||||||||||||||
| return ColorMode.RGB | ||||||||||||||||||||||||||||||||
| if ColorMode.COLOR_TEMP in self.supported_color_modes: | ||||||||||||||||||||||||||||||||
| return ColorMode.COLOR_TEMP | ||||||||||||||||||||||||||||||||
| return ColorMode.UNKNOWN | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def _set_attributes(self) -> None: | ||||||||||||||||||||||||||||||||
| """Set attributes from coordinator data.""" | ||||||||||||||||||||||||||||||||
| if self.coordinator.data is None: | ||||||||||||||||||||||||||||||||
|
|
@@ -83,8 +94,9 @@ async def async_turn_on(self, **kwargs: Any) -> None: | |||||||||||||||||||||||||||||||
| brightness: int | None = kwargs.get("brightness") | ||||||||||||||||||||||||||||||||
| rgb_color: tuple[int, int, int] | None = kwargs.get("rgb_color") | ||||||||||||||||||||||||||||||||
| color_temp_kelvin: int | None = kwargs.get("color_temp_kelvin") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if brightness is not None: | ||||||||||||||||||||||||||||||||
| self._attr_color_mode = ColorMode.RGB | ||||||||||||||||||||||||||||||||
| self._attr_color_mode = self._get_default_color_mode() | ||||||||||||||||||||||||||||||||
| await self._send_brightness_command(brightness) | ||||||||||||||||||||||||||||||||
| elif rgb_color is not None: | ||||||||||||||||||||||||||||||||
| self._attr_color_mode = ColorMode.RGB | ||||||||||||||||||||||||||||||||
|
|
@@ -93,7 +105,7 @@ async def async_turn_on(self, **kwargs: Any) -> None: | |||||||||||||||||||||||||||||||
| self._attr_color_mode = ColorMode.COLOR_TEMP | ||||||||||||||||||||||||||||||||
| await self._send_color_temperature_command(color_temp_kelvin) | ||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||
| self._attr_color_mode = ColorMode.RGB | ||||||||||||||||||||||||||||||||
| self._attr_color_mode = self._get_default_color_mode() | ||||||||||||||||||||||||||||||||
| await self.send_api_command(CommonCommands.ON) | ||||||||||||||||||||||||||||||||
| await asyncio.sleep(AFTER_COMMAND_REFRESH) | ||||||||||||||||||||||||||||||||
| await self.coordinator.async_request_refresh() | ||||||||||||||||||||||||||||||||
|
|
@@ -149,11 +161,36 @@ async def _send_rgb_color_command(self, rgb_color: tuple) -> None: | |||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class SwitchBotCloudCeilingLight(SwitchBotCloudLight): | ||||||||||||||||||||||||||||||||
| """Representation of SwitchBot Ceiling Light.""" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _attr_max_color_temp_kelvin = 6500 | ||||||||||||||||||||||||||||||||
| _attr_min_color_temp_kelvin = 2700 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _attr_supported_color_modes = {ColorMode.COLOR_TEMP} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| async def _send_brightness_command(self, brightness: int) -> None: | ||||||||||||||||||||||||||||||||
| """Send a brightness command.""" | ||||||||||||||||||||||||||||||||
| await self.send_api_command( | ||||||||||||||||||||||||||||||||
| CeilingLightCommands.SET_BRIGHTNESS, | ||||||||||||||||||||||||||||||||
| parameters=str(value_map_brightness(brightness)), | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| async def _send_color_temperature_command(self, color_temp_kelvin: int) -> None: | ||||||||||||||||||||||||||||||||
| """Send a color temperature command.""" | ||||||||||||||||||||||||||||||||
| await self.send_api_command( | ||||||||||||||||||||||||||||||||
| CeilingLightCommands.SET_COLOR_TEMPERATURE, | ||||||||||||||||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These, arguably, could be the same commands as But they're separate on the |
||||||||||||||||||||||||||||||||
| parameters=str(color_temp_kelvin), | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
+164
to
+185
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
+185
to
+186
|
||||||||||||||||||||||||||||||||
| async def async_turn_on(self, **kwargs: Any) -> None: | |
| """Turn on the ceiling light with the correct color mode.""" | |
| # Only COLOR_TEMP is supported | |
| if (brightness := kwargs.get("brightness")) is not None: | |
| await self._send_brightness_command(brightness) | |
| if (color_temp := kwargs.get("color_temp_kelvin")) is not None: | |
| await self._send_color_temperature_command(color_temp) | |
| if not kwargs: | |
| await self.send_api_command(CommonCommands.TURN_ON) | |
| self._attr_is_on = True | |
| self._attr_color_mode = ColorMode.COLOR_TEMP | |
| await asyncio.sleep(AFTER_COMMAND_REFRESH) | |
| await self.coordinator.async_request_refresh() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole
_get_default_color_mode()is here to preserve the existing logic of updating theColorModehere; but I am not sure why it's here in the first place.Would just not updating the
_attr_color_modehere (and in thebrightness is not Nonecase?) at all would be a better move? I don't fullly understand HASS internals to understand what the implications of that are, but it feels weird to always update this, even when only touching the brightness?