Fix mqtt json light allows to set brightness value >255 (#104220)

This commit is contained in:
Jan Bouwhuis 2023-11-19 19:50:25 +01:00 committed by GitHub
parent ec069fbebf
commit 68f8b2cab5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View file

@ -367,10 +367,13 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
if brightness_supported(self.supported_color_modes):
try:
if brightness := values["brightness"]:
self._attr_brightness = int(
self._attr_brightness = min(
int(
brightness # type: ignore[operator]
/ float(self._config[CONF_BRIGHTNESS_SCALE])
* 255
),
255,
)
else:
_LOGGER.debug(

View file

@ -1785,6 +1785,24 @@ async def test_brightness_scale(
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
# Turn on the light with half brightness
async_fire_mqtt_message(
hass, "test_light_bright_scale", '{"state":"ON", "brightness": 50}'
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 128
# Test limmiting max brightness
async_fire_mqtt_message(
hass, "test_light_bright_scale", '{"state":"ON", "brightness": 103}'
)
state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
@pytest.mark.parametrize(
"hass_config",