diff --git a/homeassistant/components/alexa/capabilities.py b/homeassistant/components/alexa/capabilities.py index a7065a38686..5d749fdf430 100644 --- a/homeassistant/components/alexa/capabilities.py +++ b/homeassistant/components/alexa/capabilities.py @@ -580,8 +580,8 @@ class AlexaBrightnessController(AlexaCapability): """Read and return a property.""" if name != "brightness": raise UnsupportedProperty(name) - if "brightness" in self.entity.attributes: - return round(self.entity.attributes["brightness"] / 255.0 * 100) + if brightness := self.entity.attributes.get("brightness"): + return round(brightness / 255.0 * 100) return 0 @@ -683,10 +683,8 @@ class AlexaColorTemperatureController(AlexaCapability): """Read and return a property.""" if name != "colorTemperatureInKelvin": raise UnsupportedProperty(name) - if "color_temp" in self.entity.attributes: - return color_util.color_temperature_mired_to_kelvin( - self.entity.attributes["color_temp"] - ) + if color_temp := self.entity.attributes.get("color_temp"): + return color_util.color_temperature_mired_to_kelvin(color_temp) return None diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index bbdf3efeb5f..99dd79fe2e2 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -272,6 +272,46 @@ async def test_dimmable_light(hass: HomeAssistant) -> None: assert call.data["brightness_pct"] == 50 +async def test_dimmable_light_with_none_brightness(hass: HomeAssistant) -> None: + """Test dimmable light discovery.""" + device = ( + "light.test_2", + "on", + { + "brightness": None, + "friendly_name": "Test light 2", + "supported_color_modes": ["brightness"], + }, + ) + appliance = await discovery_test(device, hass) + + assert appliance["endpointId"] == "light#test_2" + assert appliance["displayCategories"][0] == "LIGHT" + assert appliance["friendlyName"] == "Test light 2" + + assert_endpoint_capabilities( + appliance, + "Alexa.BrightnessController", + "Alexa.PowerController", + "Alexa.EndpointHealth", + "Alexa", + ) + + properties = await reported_properties(hass, "light#test_2") + properties.assert_equal("Alexa.PowerController", "powerState", "ON") + properties.assert_equal("Alexa.BrightnessController", "brightness", 0) + + call, _ = await assert_request_calls_service( + "Alexa.BrightnessController", + "SetBrightness", + "light#test_2", + "light.turn_on", + hass, + payload={"brightness": "50"}, + ) + assert call.data["brightness_pct"] == 50 + + @pytest.mark.parametrize( "supported_color_modes", [["color_temp", "hs"], ["color_temp", "rgb"], ["color_temp", "xy"]],