Skip to content

Commit

Permalink
Guard blowing up converting 0 mired/kelvin (home-assistant#35486)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Jun 4, 2020
1 parent 1edbdcb commit fae8062
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
19 changes: 15 additions & 4 deletions homeassistant/components/hue/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,29 @@ def color_temp(self):
@property
def min_mireds(self):
"""Return the coldest color_temp that this light supports."""
if self.is_group or "ct" not in self.light.controlcapabilities:
if self.is_group:
return super().min_mireds

min_mireds = self.light.controlcapabilities.get("ct", {}).get("min")

# We filter out '0' too, which can be incorrectly reported by 3rd party buls
if not min_mireds:
return super().min_mireds

return self.light.controlcapabilities["ct"]["min"]
return min_mireds

@property
def max_mireds(self):
"""Return the warmest color_temp that this light supports."""
if self.is_group or "ct" not in self.light.controlcapabilities:
if self.is_group:
return super().max_mireds

max_mireds = self.light.controlcapabilities.get("ct", {}).get("max")

if not max_mireds:
return super().max_mireds

return self.light.controlcapabilities["ct"]["max"]
return max_mireds

@property
def is_on(self):
Expand Down
36 changes: 14 additions & 22 deletions tests/util/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,20 @@ def test_gamut():
assert not color_util.check_valid_gamut(GAMUT_INVALID_4)


def test_should_return_25000_kelvin_when_input_is_40_mired():
"""Function should return 25000K if given 40 mired."""
kelvin = color_util.color_temperature_mired_to_kelvin(40)
assert kelvin == 25000


def test_should_return_5000_kelvin_when_input_is_200_mired():
"""Function should return 5000K if given 200 mired."""
kelvin = color_util.color_temperature_mired_to_kelvin(200)
assert kelvin == 5000


def test_should_return_40_mired_when_input_is_25000_kelvin():
"""Function should return 40 mired when given 25000 Kelvin."""
mired = color_util.color_temperature_kelvin_to_mired(25000)
assert mired == 40


def test_should_return_200_mired_when_input_is_5000_kelvin():
"""Function should return 200 mired when given 5000 Kelvin."""
mired = color_util.color_temperature_kelvin_to_mired(5000)
assert mired == 200
def test_color_temperature_mired_to_kelvin():
"""Test color_temperature_mired_to_kelvin."""
assert color_util.color_temperature_mired_to_kelvin(40) == 25000
assert color_util.color_temperature_mired_to_kelvin(200) == 5000
with pytest.raises(ZeroDivisionError):
assert color_util.color_temperature_mired_to_kelvin(0)


def test_color_temperature_kelvin_to_mired():
"""Test color_temperature_kelvin_to_mired."""
assert color_util.color_temperature_kelvin_to_mired(25000) == 40
assert color_util.color_temperature_kelvin_to_mired(5000) == 200
with pytest.raises(ZeroDivisionError):
assert color_util.color_temperature_kelvin_to_mired(0)


def test_returns_same_value_for_any_two_temperatures_below_1000():
Expand Down

0 comments on commit fae8062

Please sign in to comment.