Do not allow mqtt lights to set brightness to zero (#91296)

* Do not allow mqtt lights to set brightness to zero

* Loglevel to debug

* Typo
This commit is contained in:
Jan Bouwhuis 2023-04-14 09:01:29 +02:00 committed by GitHub
parent 025e1792db
commit fc8c5f1bbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 8 deletions

View File

@ -468,6 +468,10 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
return return
device_value = float(payload) device_value = float(payload)
if device_value == 0:
_LOGGER.debug("Ignoring zero brightness from '%s'", msg.topic)
return
percent_bright = device_value / self._config[CONF_BRIGHTNESS_SCALE] percent_bright = device_value / self._config[CONF_BRIGHTNESS_SCALE]
self._attr_brightness = min(round(percent_bright * 255), 255) self._attr_brightness = min(round(percent_bright * 255), 255)

View File

@ -378,11 +378,18 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
if brightness_supported(self.supported_color_modes): if brightness_supported(self.supported_color_modes):
try: try:
self._attr_brightness = int( if brightness := values["brightness"]:
values["brightness"] # type: ignore[operator] self._attr_brightness = int(
/ float(self._config[CONF_BRIGHTNESS_SCALE]) brightness # type: ignore[operator]
* 255 / float(self._config[CONF_BRIGHTNESS_SCALE])
) * 255
)
else:
_LOGGER.debug(
"Ignoring zero brightness value for entity %s",
self.entity_id,
)
except KeyError: except KeyError:
pass pass
except (TypeError, ValueError): except (TypeError, ValueError):

View File

@ -236,11 +236,20 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
if CONF_BRIGHTNESS_TEMPLATE in self._config: if CONF_BRIGHTNESS_TEMPLATE in self._config:
try: try:
self._attr_brightness = int( if brightness := int(
self._value_templates[CONF_BRIGHTNESS_TEMPLATE](msg.payload) self._value_templates[CONF_BRIGHTNESS_TEMPLATE](msg.payload)
) ):
self._attr_brightness = brightness
else:
_LOGGER.debug(
"Ignoring zero brightness value for entity %s",
self.entity_id,
)
except ValueError: except ValueError:
_LOGGER.warning("Invalid brightness value received") _LOGGER.warning(
"Invalid brightness value received from %s", msg.topic
)
if CONF_COLOR_TEMP_TEMPLATE in self._config: if CONF_COLOR_TEMP_TEMPLATE in self._config:
try: try:

View File

@ -794,6 +794,19 @@ async def test_controlling_state_via_topic_with_templates(
assert state.attributes.get(light.ATTR_COLOR_MODE) == "xy" assert state.attributes.get(light.ATTR_COLOR_MODE) == "xy"
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes
async_fire_mqtt_message(hass, "test_light_rgb/brightness/status", '{"hello": 100}')
state = hass.states.get("light.test")
assert state.attributes.get("brightness") == 100
async_fire_mqtt_message(hass, "test_light_rgb/brightness/status", '{"hello": 50}')
state = hass.states.get("light.test")
assert state.attributes.get("brightness") == 50
# test zero brightness received is ignored
async_fire_mqtt_message(hass, "test_light_rgb/brightness/status", '{"hello": 0}')
state = hass.states.get("light.test")
assert state.attributes.get("brightness") == 50
@pytest.mark.parametrize( @pytest.mark.parametrize(
"hass_config", "hass_config",

View File

@ -493,6 +493,37 @@ async def test_controlling_state_via_topic(
light_state = hass.states.get("light.test") light_state = hass.states.get("light.test")
assert light_state.attributes.get("effect") == "colorloop" assert light_state.attributes.get("effect") == "colorloop"
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"ON",'
'"color":{"r":255,"g":255,"b":255},'
'"brightness":128,'
'"color_temp":155,'
'"effect":"colorloop"}',
)
light_state = hass.states.get("light.test")
assert light_state.state == STATE_ON
assert light_state.attributes.get("brightness") == 128
async_fire_mqtt_message(
hass,
"test_light_rgb",
'{"state":"OFF","brightness":0}',
)
light_state = hass.states.get("light.test")
assert light_state.state == STATE_OFF
assert light_state.attributes.get("brightness") is None
# test previous zero brightness received was ignored and brightness is restored
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON"}')
light_state = hass.states.get("light.test")
assert light_state.attributes.get("brightness") == 128
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"ON","brightness":0}')
light_state = hass.states.get("light.test")
assert light_state.attributes.get("brightness") == 128
@pytest.mark.parametrize( @pytest.mark.parametrize(
"hass_config", "hass_config",

View File

@ -330,6 +330,12 @@ async def test_state_brightness_color_effect_temp_change_via_topic(
light_state = hass.states.get("light.test") light_state = hass.states.get("light.test")
assert light_state.attributes["brightness"] == 100 assert light_state.attributes["brightness"] == 100
# ignore a zero brightness
async_fire_mqtt_message(hass, "test_light_rgb", "on,0")
light_state = hass.states.get("light.test")
assert light_state.attributes["brightness"] == 100
# change the color temp # change the color temp
async_fire_mqtt_message(hass, "test_light_rgb", "on,,195") async_fire_mqtt_message(hass, "test_light_rgb", "on,,195")