Skip to content

Commit 5ca3208

Browse files
committed
check for empty or missing values when processing state messages for MQTT light entities using 'template' schema
1 parent 3a80a2d commit 5ca3208

File tree

1 file changed

+80
-46
lines changed

1 file changed

+80
-46
lines changed

homeassistant/components/mqtt/light/schema_template.py

+80-46
Original file line numberDiff line numberDiff line change
@@ -204,73 +204,107 @@ def _update_color_mode(self) -> None:
204204
@callback
205205
def _state_received(self, msg: ReceiveMessage) -> None:
206206
"""Handle new MQTT messages."""
207-
state = self._value_templates[CONF_STATE_TEMPLATE](msg.payload)
208-
if state == STATE_ON:
207+
state_value = self._value_templates[CONF_STATE_TEMPLATE](msg.payload)
208+
if not state_value:
209+
_LOGGER.debug(
210+
"Ignoring message from '%s' with empty state value", msg.topic
211+
)
212+
elif state_value == STATE_ON:
209213
self._attr_is_on = True
210-
elif state == STATE_OFF:
214+
elif state_value == STATE_OFF:
211215
self._attr_is_on = False
212-
elif state == PAYLOAD_NONE:
216+
elif state_value == PAYLOAD_NONE:
213217
self._attr_is_on = None
214218
else:
215-
_LOGGER.warning("Invalid state value received")
219+
_LOGGER.warning("Invalid state value received from '%s'", state_value)
216220

217221
if CONF_BRIGHTNESS_TEMPLATE in self._config:
218-
try:
219-
if brightness := int(
220-
self._value_templates[CONF_BRIGHTNESS_TEMPLATE](msg.payload)
221-
):
222-
self._attr_brightness = brightness
223-
else:
224-
_LOGGER.debug(
225-
"Ignoring zero brightness value for entity %s",
226-
self.entity_id,
222+
brightness_value = self._value_templates[CONF_BRIGHTNESS_TEMPLATE](
223+
msg.payload
224+
)
225+
if not brightness_value:
226+
_LOGGER.debug(
227+
"Ignoring message from '%s' with empty brightness value",
228+
msg.topic,
229+
)
230+
else:
231+
try:
232+
if brightness := int(brightness_value):
233+
self._attr_brightness = brightness
234+
else:
235+
_LOGGER.debug(
236+
"Ignoring zero brightness value for entity %s",
237+
self.entity_id,
238+
)
239+
except ValueError:
240+
_LOGGER.warning(
241+
"Invalid brightness value received from %s", msg.topic
227242
)
228243

229-
except ValueError:
230-
_LOGGER.warning("Invalid brightness value received from %s", msg.topic)
231-
232244
if CONF_COLOR_TEMP_TEMPLATE in self._config:
233-
try:
234-
color_temp = self._value_templates[CONF_COLOR_TEMP_TEMPLATE](
235-
msg.payload
236-
)
237-
self._attr_color_temp_kelvin = (
238-
int(color_temp)
239-
if self._color_temp_kelvin
240-
else color_util.color_temperature_mired_to_kelvin(int(color_temp))
241-
if color_temp != "None"
242-
else None
245+
color_temp_value = self._value_templates[CONF_COLOR_TEMP_TEMPLATE](
246+
msg.payload
247+
)
248+
if not color_temp_value:
249+
_LOGGER.debug(
250+
"Ignoring message from '%s' with empty color temperature value",
251+
msg.topic,
243252
)
244-
except ValueError:
245-
_LOGGER.warning("Invalid color temperature value received")
253+
else:
254+
try:
255+
self._attr_color_temp_kelvin = (
256+
int(color_temp_value)
257+
if self._color_temp_kelvin
258+
else color_util.color_temperature_mired_to_kelvin(
259+
int(color_temp_value)
260+
)
261+
if color_temp_value != "None"
262+
else None
263+
)
264+
except ValueError:
265+
_LOGGER.warning(
266+
"Invalid color temperature value received from %s", msg.topic
267+
)
246268

247269
if (
248270
CONF_RED_TEMPLATE in self._config
249271
and CONF_GREEN_TEMPLATE in self._config
250272
and CONF_BLUE_TEMPLATE in self._config
251273
):
252-
try:
253-
red = self._value_templates[CONF_RED_TEMPLATE](msg.payload)
254-
green = self._value_templates[CONF_GREEN_TEMPLATE](msg.payload)
255-
blue = self._value_templates[CONF_BLUE_TEMPLATE](msg.payload)
256-
if red == "None" and green == "None" and blue == "None":
257-
self._attr_hs_color = None
258-
else:
274+
red_value = self._value_templates[CONF_RED_TEMPLATE](msg.payload)
275+
green_value = self._value_templates[CONF_GREEN_TEMPLATE](msg.payload)
276+
blue_value = self._value_templates[CONF_BLUE_TEMPLATE](msg.payload)
277+
if not red_value or not green_value or not blue_value:
278+
_LOGGER.debug(
279+
"Ignoring message from '%s' with empty color value", msg.topic
280+
)
281+
elif red_value == "None" and green_value == "None" and blue_value == "None":
282+
self._attr_hs_color = None
283+
else:
284+
try:
259285
self._attr_hs_color = color_util.color_RGB_to_hs(
260-
int(red), int(green), int(blue)
286+
int(red_value), int(green_value), int(blue_value)
261287
)
262-
self._update_color_mode()
263-
except ValueError:
264-
_LOGGER.warning("Invalid color value received")
288+
self._update_color_mode()
289+
except ValueError:
290+
_LOGGER.warning("Invalid color value received from %s", msg.topic)
265291

266292
if CONF_EFFECT_TEMPLATE in self._config:
267-
effect = str(self._value_templates[CONF_EFFECT_TEMPLATE](msg.payload))
268-
if (
269-
effect_list := self._config[CONF_EFFECT_LIST]
270-
) and effect in effect_list:
271-
self._attr_effect = effect
293+
effect_value = self._value_templates[CONF_EFFECT_TEMPLATE](msg.payload)
294+
if not effect_value:
295+
_LOGGER.debug(
296+
"Ignoring message from '%s' with empty effect value", msg.topic
297+
)
298+
elif (effect_list := self._config[CONF_EFFECT_LIST]) and str(
299+
effect_value
300+
) in effect_list:
301+
self._attr_effect = str(effect_value)
272302
else:
273-
_LOGGER.warning("Unsupported effect value received")
303+
_LOGGER.warning(
304+
"Unsupported effect value '%s' received from %s",
305+
effect_value,
306+
msg.topic,
307+
)
274308

275309
@callback
276310
def _prepare_subscribe_topics(self) -> None:

0 commit comments

Comments
 (0)