Skip to content

Commit

Permalink
Fix missed case alexa light attr can be None (home-assistant#102612)
Browse files Browse the repository at this point in the history
* Fix missed cased alexa light attr can be None

* Add test
  • Loading branch information
jbouwh authored Oct 23, 2023
1 parent d5e7ccc commit 6372bc3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
12 changes: 8 additions & 4 deletions homeassistant/components/alexa/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,16 @@ def get_property(self, name: str) -> Any:
if name != "color":
raise UnsupportedProperty(name)

hue, saturation = self.entity.attributes.get(light.ATTR_HS_COLOR, (0, 0))
hue_saturation: tuple[float, float] | None
if (hue_saturation := self.entity.attributes.get(light.ATTR_HS_COLOR)) is None:
hue_saturation = (0, 0)
if (brightness := self.entity.attributes.get(light.ATTR_BRIGHTNESS)) is None:
brightness = 0

return {
"hue": hue,
"saturation": saturation / 100.0,
"brightness": self.entity.attributes.get(light.ATTR_BRIGHTNESS, 0) / 255.0,
"hue": hue_saturation[0],
"saturation": hue_saturation[1] / 100.0,
"brightness": brightness / 255.0,
}


Expand Down
49 changes: 49 additions & 0 deletions tests/components/alexa/test_smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,55 @@ async def test_color_light(
# tests


async def test_color_light_turned_off(hass: HomeAssistant) -> None:
"""Test color light discovery with turned off light."""
device = (
"light.test_off",
"off",
{
"friendly_name": "Test light off",
"supported_color_modes": ["color_temp", "hs"],
"hs_color": None,
"color_temp": None,
"brightness": None,
},
)
appliance = await discovery_test(device, hass)

assert appliance["endpointId"] == "light#test_off"
assert appliance["displayCategories"][0] == "LIGHT"
assert appliance["friendlyName"] == "Test light off"

assert_endpoint_capabilities(
appliance,
"Alexa.BrightnessController",
"Alexa.PowerController",
"Alexa.ColorController",
"Alexa.ColorTemperatureController",
"Alexa.EndpointHealth",
"Alexa",
)

properties = await reported_properties(hass, "light#test_off")
properties.assert_equal("Alexa.PowerController", "powerState", "OFF")
properties.assert_equal("Alexa.BrightnessController", "brightness", 0)
properties.assert_equal(
"Alexa.ColorController",
"color",
{"hue": 0.0, "saturation": 0.0, "brightness": 0.0},
)

call, _ = await assert_request_calls_service(
"Alexa.BrightnessController",
"SetBrightness",
"light#test_off",
"light.turn_on",
hass,
payload={"brightness": "50"},
)
assert call.data["brightness_pct"] == 50


@pytest.mark.freeze_time("2022-04-19 07:53:05")
async def test_script(hass: HomeAssistant) -> None:
"""Test script discovery."""
Expand Down

0 comments on commit 6372bc3

Please sign in to comment.