mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Refactor MQTT basic light pt3: Add publish helper (#50767)
This commit is contained in:
parent
b36021b4fd
commit
8c6f4a8c71
@ -531,14 +531,18 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
should_update = False
|
should_update = False
|
||||||
on_command_type = self._config[CONF_ON_COMMAND_TYPE]
|
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(
|
mqtt.async_publish(
|
||||||
self.hass,
|
self.hass,
|
||||||
self._topic[CONF_COMMAND_TOPIC],
|
self._topic[topic],
|
||||||
self._payload["on"],
|
payload,
|
||||||
self._config[CONF_QOS],
|
self._config[CONF_QOS],
|
||||||
self._config[CONF_RETAIN],
|
self._config[CONF_RETAIN],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if on_command_type == "first":
|
||||||
|
publish(CONF_COMMAND_TOPIC, self._payload["on"])
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
# If brightness is being used instead of an on command, make sure
|
# If brightness is being used instead of an on command, make sure
|
||||||
@ -568,13 +572,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
else:
|
else:
|
||||||
rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"
|
rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"
|
||||||
|
|
||||||
mqtt.async_publish(
|
publish(CONF_RGB_COMMAND_TOPIC, rgb_color_str)
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_RGB_COMMAND_TOPIC],
|
|
||||||
rgb_color_str,
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_rgb:
|
if self._optimistic_rgb:
|
||||||
self._hs_color = kwargs[ATTR_HS_COLOR]
|
self._hs_color = kwargs[ATTR_HS_COLOR]
|
||||||
@ -583,13 +581,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
if ATTR_HS_COLOR in kwargs and self._topic[CONF_HS_COMMAND_TOPIC] is not None:
|
if ATTR_HS_COLOR in kwargs and self._topic[CONF_HS_COMMAND_TOPIC] is not None:
|
||||||
|
|
||||||
hs_color = kwargs[ATTR_HS_COLOR]
|
hs_color = kwargs[ATTR_HS_COLOR]
|
||||||
mqtt.async_publish(
|
publish(CONF_HS_COMMAND_TOPIC, f"{hs_color[0]},{hs_color[1]}")
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_HS_COMMAND_TOPIC],
|
|
||||||
f"{hs_color[0]},{hs_color[1]}",
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_hs:
|
if self._optimistic_hs:
|
||||||
self._hs_color = kwargs[ATTR_HS_COLOR]
|
self._hs_color = kwargs[ATTR_HS_COLOR]
|
||||||
@ -598,13 +590,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
if ATTR_HS_COLOR in kwargs and self._topic[CONF_XY_COMMAND_TOPIC] is not None:
|
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])
|
xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
|
||||||
mqtt.async_publish(
|
publish(CONF_XY_COMMAND_TOPIC, f"{xy_color[0]},{xy_color[1]}")
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_XY_COMMAND_TOPIC],
|
|
||||||
f"{xy_color[0]},{xy_color[1]}",
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_xy:
|
if self._optimistic_xy:
|
||||||
self._hs_color = kwargs[ATTR_HS_COLOR]
|
self._hs_color = kwargs[ATTR_HS_COLOR]
|
||||||
@ -621,13 +607,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
)
|
)
|
||||||
# Make sure the brightness is not rounded down to 0
|
# Make sure the brightness is not rounded down to 0
|
||||||
device_brightness = max(device_brightness, 1)
|
device_brightness = max(device_brightness, 1)
|
||||||
mqtt.async_publish(
|
publish(CONF_BRIGHTNESS_COMMAND_TOPIC, device_brightness)
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
|
|
||||||
device_brightness,
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_brightness:
|
if self._optimistic_brightness:
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
@ -647,13 +627,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
else:
|
else:
|
||||||
rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"
|
rgb_color_str = f"{rgb[0]},{rgb[1]},{rgb[2]}"
|
||||||
|
|
||||||
mqtt.async_publish(
|
publish(CONF_RGB_COMMAND_TOPIC, rgb_color_str)
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_RGB_COMMAND_TOPIC],
|
|
||||||
rgb_color_str,
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_brightness:
|
if self._optimistic_brightness:
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
@ -669,13 +643,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
if tpl:
|
if tpl:
|
||||||
color_temp = tpl({"value": color_temp})
|
color_temp = tpl({"value": color_temp})
|
||||||
|
|
||||||
mqtt.async_publish(
|
publish(CONF_COLOR_TEMP_COMMAND_TOPIC, color_temp)
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
|
|
||||||
color_temp,
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_color_temp:
|
if self._optimistic_color_temp:
|
||||||
self._color_temp = kwargs[ATTR_COLOR_TEMP]
|
self._color_temp = kwargs[ATTR_COLOR_TEMP]
|
||||||
@ -684,13 +652,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
if ATTR_EFFECT in kwargs and self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
|
if ATTR_EFFECT in kwargs and self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
|
||||||
effect = kwargs[ATTR_EFFECT]
|
effect = kwargs[ATTR_EFFECT]
|
||||||
if effect in self._config.get(CONF_EFFECT_LIST):
|
if effect in self._config.get(CONF_EFFECT_LIST):
|
||||||
mqtt.async_publish(
|
publish(CONF_EFFECT_COMMAND_TOPIC, effect)
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_EFFECT_COMMAND_TOPIC],
|
|
||||||
effect,
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_effect:
|
if self._optimistic_effect:
|
||||||
self._effect = kwargs[ATTR_EFFECT]
|
self._effect = kwargs[ATTR_EFFECT]
|
||||||
@ -703,26 +665,14 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||||||
percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
|
percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
|
||||||
white_scale = self._config[CONF_WHITE_VALUE_SCALE]
|
white_scale = self._config[CONF_WHITE_VALUE_SCALE]
|
||||||
device_white_value = min(round(percent_white * white_scale), white_scale)
|
device_white_value = min(round(percent_white * white_scale), white_scale)
|
||||||
mqtt.async_publish(
|
publish(CONF_WHITE_VALUE_COMMAND_TOPIC, device_white_value)
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
|
|
||||||
device_white_value,
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._optimistic_white_value:
|
if self._optimistic_white_value:
|
||||||
self._white_value = kwargs[ATTR_WHITE_VALUE]
|
self._white_value = kwargs[ATTR_WHITE_VALUE]
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if on_command_type == "last":
|
if on_command_type == "last":
|
||||||
mqtt.async_publish(
|
publish(CONF_COMMAND_TOPIC, self._payload["on"])
|
||||||
self.hass,
|
|
||||||
self._topic[CONF_COMMAND_TOPIC],
|
|
||||||
self._payload["on"],
|
|
||||||
self._config[CONF_QOS],
|
|
||||||
self._config[CONF_RETAIN],
|
|
||||||
)
|
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user