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
82 changes: 16 additions & 66 deletions homeassistant/components/mqtt/light/schema_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,18 @@ async def async_turn_on(self, **kwargs): # noqa: C901
should_update = False
on_command_type = self._config[CONF_ON_COMMAND_TYPE]

if on_command_type == "first":
def publish(topic, payload):
"""Publish an MQTT message."""
mqtt.async_publish(
self.hass,
self._topic[CONF_COMMAND_TOPIC],
self._payload["on"],
self._topic[topic],
payload,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)

if on_command_type == "first":
publish(CONF_COMMAND_TOPIC, self._payload["on"])
should_update = True

# If brightness is being used instead of an on command, make sure
Expand Down Expand Up @@ -598,13 +602,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
else:
rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"

mqtt.async_publish(
self.hass,
self._topic[CONF_RGB_COMMAND_TOPIC],
rgb_color_str,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_RGB_COMMAND_TOPIC, rgb_color_str)

if self._optimistic_rgb:
self._hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -613,13 +611,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
if ATTR_HS_COLOR in kwargs and self._topic[CONF_HS_COMMAND_TOPIC] is not None:

hs_color = kwargs[ATTR_HS_COLOR]
mqtt.async_publish(
self.hass,
self._topic[CONF_HS_COMMAND_TOPIC],
f"{hs_color[0]},{hs_color[1]}",
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_HS_COMMAND_TOPIC, f"{hs_color[0]},{hs_color[1]}")

if self._optimistic_hs:
self._hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -628,13 +620,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
if ATTR_HS_COLOR in kwargs and self._topic[CONF_XY_COMMAND_TOPIC] is not None:

xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
mqtt.async_publish(
self.hass,
self._topic[CONF_XY_COMMAND_TOPIC],
f"{xy_color[0]},{xy_color[1]}",
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_XY_COMMAND_TOPIC, f"{xy_color[0]},{xy_color[1]}")

if self._optimistic_xy:
self._hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -651,13 +637,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
)
# Make sure the brightness is not rounded down to 0
device_brightness = max(device_brightness, 1)
mqtt.async_publish(
self.hass,
self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
device_brightness,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_BRIGHTNESS_COMMAND_TOPIC, device_brightness)

if self._optimistic_brightness:
self._brightness = kwargs[ATTR_BRIGHTNESS]
Expand All @@ -677,13 +657,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
else:
rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"

mqtt.async_publish(
self.hass,
self._topic[CONF_RGB_COMMAND_TOPIC],
rgb_color_str,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_RGB_COMMAND_TOPIC, rgb_color_str)

if self._optimistic_brightness:
self._brightness = kwargs[ATTR_BRIGHTNESS]
Expand All @@ -699,13 +673,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
if tpl:
color_temp = tpl({"value": color_temp})

mqtt.async_publish(
self.hass,
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
color_temp,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_COLOR_TEMP_COMMAND_TOPIC, color_temp)

if self._optimistic_color_temp:
self._color_temp = kwargs[ATTR_COLOR_TEMP]
Expand All @@ -714,13 +682,7 @@ async def async_turn_on(self, **kwargs): # noqa: C901
if ATTR_EFFECT in kwargs and self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
effect = kwargs[ATTR_EFFECT]
if effect in self._config.get(CONF_EFFECT_LIST):
mqtt.async_publish(
self.hass,
self._topic[CONF_EFFECT_COMMAND_TOPIC],
effect,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_EFFECT_COMMAND_TOPIC, effect)

if self._optimistic_effect:
self._effect = kwargs[ATTR_EFFECT]
Expand All @@ -733,26 +695,14 @@ async def async_turn_on(self, **kwargs): # noqa: C901
percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
white_scale = self._config[CONF_WHITE_VALUE_SCALE]
device_white_value = min(round(percent_white * white_scale), white_scale)
mqtt.async_publish(
self.hass,
self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
device_white_value,
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_WHITE_VALUE_COMMAND_TOPIC, device_white_value)

if self._optimistic_white_value:
self._white_value = kwargs[ATTR_WHITE_VALUE]
should_update = True

if on_command_type == "last":
mqtt.async_publish(
self.hass,
self._topic[CONF_COMMAND_TOPIC],
self._payload["on"],
self._config[CONF_QOS],
self._config[CONF_RETAIN],
)
publish(CONF_COMMAND_TOPIC, self._payload["on"])
should_update = True

if self._optimistic:
Expand Down