Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 18 additions & 44 deletions homeassistant/components/mqtt/light/schema_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ async def _subscribe_topics(self): # noqa: C901

last_state = await self.async_get_last_state()

def add_topic(topic, msg_callback):
"""Add a topic."""
if self._topic[topic] is not None:
topics[topic] = {
"topic": self._topic[topic],
"msg_callback": msg_callback,
"qos": self._config[CONF_QOS],
}

@callback
@log_messages(self.hass, self.entity_id)
def state_received(msg):
Expand Down Expand Up @@ -298,13 +307,8 @@ def brightness_received(msg):
self._brightness = percent_bright * 255
self.async_write_ha_state()

if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None:
topics[CONF_BRIGHTNESS_STATE_TOPIC] = {
"topic": self._topic[CONF_BRIGHTNESS_STATE_TOPIC],
"msg_callback": brightness_received,
"qos": self._config[CONF_QOS],
}
elif (
add_topic(CONF_BRIGHTNESS_STATE_TOPIC, brightness_received)
if (
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The elif was not correct, we should restore the state if self._optimistic_brightness is True

Comment thread
MartinHjelmare marked this conversation as resolved.
self._optimistic_brightness
and last_state
and last_state.attributes.get(ATTR_BRIGHTNESS)
Expand All @@ -327,12 +331,7 @@ def rgb_received(msg):
self._brightness = percent_bright * 255
self.async_write_ha_state()

if self._topic[CONF_RGB_STATE_TOPIC] is not None:
topics[CONF_RGB_STATE_TOPIC] = {
"topic": self._topic[CONF_RGB_STATE_TOPIC],
"msg_callback": rgb_received,
"qos": self._config[CONF_QOS],
}
add_topic(CONF_RGB_STATE_TOPIC, rgb_received)
if (
self._optimistic_rgb
and last_state
Expand All @@ -354,12 +353,7 @@ def color_temp_received(msg):
self._color_temp = int(payload)
self.async_write_ha_state()

if self._topic[CONF_COLOR_TEMP_STATE_TOPIC] is not None:
topics[CONF_COLOR_TEMP_STATE_TOPIC] = {
"topic": self._topic[CONF_COLOR_TEMP_STATE_TOPIC],
"msg_callback": color_temp_received,
"qos": self._config[CONF_QOS],
}
add_topic(CONF_COLOR_TEMP_STATE_TOPIC, color_temp_received)
if (
self._optimistic_color_temp
and last_state
Expand All @@ -381,12 +375,7 @@ def effect_received(msg):
self._effect = payload
self.async_write_ha_state()

if self._topic[CONF_EFFECT_STATE_TOPIC] is not None:
topics[CONF_EFFECT_STATE_TOPIC] = {
"topic": self._topic[CONF_EFFECT_STATE_TOPIC],
"msg_callback": effect_received,
"qos": self._config[CONF_QOS],
}
add_topic(CONF_EFFECT_STATE_TOPIC, effect_received)
if (
self._optimistic_effect
and last_state
Expand All @@ -410,12 +399,7 @@ def hs_received(msg):
except ValueError:
_LOGGER.debug("Failed to parse hs state update: '%s'", payload)

if self._topic[CONF_HS_STATE_TOPIC] is not None:
topics[CONF_HS_STATE_TOPIC] = {
"topic": self._topic[CONF_HS_STATE_TOPIC],
"msg_callback": hs_received,
"qos": self._config[CONF_QOS],
}
add_topic(CONF_HS_STATE_TOPIC, hs_received)
if (
self._optimistic_hs
and last_state
Expand All @@ -439,13 +423,8 @@ def white_value_received(msg):
self._white_value = percent_white * 255
self.async_write_ha_state()

if self._topic[CONF_WHITE_VALUE_STATE_TOPIC] is not None:
topics[CONF_WHITE_VALUE_STATE_TOPIC] = {
"topic": self._topic[CONF_WHITE_VALUE_STATE_TOPIC],
"msg_callback": white_value_received,
"qos": self._config[CONF_QOS],
}
elif (
add_topic(CONF_WHITE_VALUE_STATE_TOPIC, white_value_received)
if (
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The elif was not correct, we should restore the state if self._optimistic_white_value is True

Comment thread
MartinHjelmare marked this conversation as resolved.
self._optimistic_white_value
and last_state
and last_state.attributes.get(ATTR_WHITE_VALUE)
Expand All @@ -465,12 +444,7 @@ def xy_received(msg):
self._hs = color_util.color_xy_to_hs(*xy_color)
self.async_write_ha_state()

if self._topic[CONF_XY_STATE_TOPIC] is not None:
topics[CONF_XY_STATE_TOPIC] = {
"topic": self._topic[CONF_XY_STATE_TOPIC],
"msg_callback": xy_received,
"qos": self._config[CONF_QOS],
}
add_topic(CONF_XY_STATE_TOPIC, xy_received)
if (
self._optimistic_xy
and last_state
Expand Down