Skip to content

Commit

Permalink
Merge pull request #26 from petretiandrea/fix-kelvin
Browse files Browse the repository at this point in the history
Fix kelvin
  • Loading branch information
petretiandrea authored Apr 21, 2021
2 parents 66a7408 + 370895d commit 88713a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions custom_components/tapo/light.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from custom_components.tapo.utils import clamp
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.components.light import (
Expand Down Expand Up @@ -39,6 +40,8 @@ class TapoLight(TapoEntity, LightEntity):
def __init__(self, coordinator, config_entry, features: int):
super().__init__(coordinator, config_entry)
self.features = features
self._max_kelvin = 6500
self._min_kelvin = 2500
self._max_merids = kelvin_to_mired(2500)
self._min_merids = kelvin_to_mired(6500)

Expand Down Expand Up @@ -110,8 +113,8 @@ async def _set_brightness():
await self._execute_with_fallback(_set_brightness)

async def _change_color_temp(self, color_temp):
constraint_color_temp = max(self._min_merids, min(color_temp, self._max_merids))
kelvin_color_temp = mired_to_kelvin(constraint_color_temp)
constraint_color_temp = clamp(color_temp, self._min_merids, self._max_merids)
kelvin_color_temp = clamp(mired_to_kelvin(constraint_color_temp), min_value=self._min_kelvin, max_value=self._max_kelvin)
await self._execute_with_fallback(
lambda: self._tapo_coordinator.api.set_color_temperature(kelvin_color_temp)
)
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tapo/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

def clamp(value, min_value, max_value):
return max(min(value, max_value), min_value)

0 comments on commit 88713a2

Please sign in to comment.