Skip to content

Commit

Permalink
Merge pull request #139 from petretiandrea/fix/initial-setup
Browse files Browse the repository at this point in the history
added better setup handling + logging light change param
  • Loading branch information
petretiandrea authored Feb 12, 2022
2 parents 4c14179 + 4b4ee96 commit 25a9274
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion custom_components/tapo/common_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def setup_tapo_coordinator(
client = TapoApiClient(host, username, password, session)

coordinator = TapoUpdateCoordinator(hass, client=client)
await coordinator.async_refresh()
await coordinator.async_config_entry_first_refresh()

if not coordinator.last_update_success:
raise Exception("Failed to retrieve first tapo data")
Expand Down
15 changes: 13 additions & 2 deletions custom_components/tapo/light.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Dict, Any, Callable
from custom_components.tapo.common_setup import (
TapoUpdateCoordinator,
Expand All @@ -22,6 +23,9 @@
from custom_components.tapo.const import DOMAIN, SUPPORTED_DEVICE_AS_LIGHT


_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_devices):
# get tapo helper
coordinator: TapoUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
Expand Down Expand Up @@ -96,9 +100,13 @@ async def async_turn_on(self, **kwargs):
color = kwargs.get(ATTR_HS_COLOR)
color_temp = kwargs.get(ATTR_COLOR_TEMP)

_LOGGER.info(f"Setting brightness: {brightness}")
_LOGGER.info(f"Setting color: {color}")
_LOGGER.info(f"Setting color_temp: {color_temp}")

if brightness or color or color_temp:
if brightness:
await self._change_brightness(kwargs.get(ATTR_BRIGHTNESS, 255))
await self._change_brightness(brightness)
if color and self.supported_features & SUPPORT_COLOR:
hue = int(color[0])
saturation = int(color[1])
Expand All @@ -116,14 +124,16 @@ async def async_turn_off(self, **kwargs):
await self._tapo_coordinator.async_request_refresh()

async def _change_brightness(self, new_brightness):
brightness_to_set = (new_brightness / 255) * 100
brightness_to_set = round((new_brightness / 255) * 100)
_LOGGER.info(f"Mapped brightness: {brightness_to_set}")

async def _set_brightness():
await self._tapo_coordinator.api.set_brightness(brightness_to_set)

await self._execute_with_fallback(_set_brightness)

async def _change_color_temp(self, color_temp):
_LOGGER.info(f"Mapped color temp: {color_temp}")
constraint_color_temp = clamp(color_temp, self._min_merids, self._max_merids)
kelvin_color_temp = clamp(
mired_to_kelvin(constraint_color_temp),
Expand All @@ -135,6 +145,7 @@ async def _change_color_temp(self, color_temp):
)

async def _change_color(self, hs_color):
_LOGGER.info(f"Mapped colors: {hs_color}")
await self._execute_with_fallback(
lambda: self._tapo_coordinator.api.set_hue_saturation(
hs_color[0], hs_color[1]
Expand Down

0 comments on commit 25a9274

Please sign in to comment.