mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
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:
parent
025e1792db
commit
fc8c5f1bbd
@ -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)
|
||||||
|
|
||||||
|
@ -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):
|
||||||
|
@ -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:
|
||||||
|
@ -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",
|
||||||
|
@ -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",
|
||||||
|
@ -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")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user