Use the attribute and action field enums in mqtt (#191539)

This commit is contained in:
epenet
2026-11-09 19:54:32 +01:00
committed by GitHub
parent 6acc8df05d
commit 17808738e9
5 changed files with 102 additions and 91 deletions
+9 -8
View File
@@ -10,9 +10,6 @@ import probatio
from homeassistant.components import climate
from homeassistant.components.climate import (
ATTR_HVAC_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
DEFAULT_MAX_HUMIDITY,
DEFAULT_MIN_HUMIDITY,
FAN_AUTO,
@@ -26,12 +23,12 @@ from homeassistant.components.climate import (
ClimateEntityCapabilityAttribute,
ClimateEntityFeature,
ClimateEntityStateAttribute,
ClimateServiceArgument,
HVACAction,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_NAME,
CONF_OPTIMISTIC,
CONF_PAYLOAD_OFF,
@@ -818,12 +815,12 @@ class MqttClimate(MqttTemperatureControlEntity, ClimateEntity):
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperatures."""
operation_mode: HVACMode | None
if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None:
if (operation_mode := kwargs.get(ClimateServiceArgument.HVAC_MODE)) is not None:
await self.async_set_hvac_mode(operation_mode)
optimistic_update = False
temperature: float | None
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is not None:
if (temperature := kwargs.get(ClimateServiceArgument.TEMPERATURE)) is not None:
if self._optimistic or self._topic[CONF_TEMP_STATE_TOPIC] is None:
optimistic_update = True
self._attr_native_target_temperature = temperature
@@ -835,7 +832,9 @@ class MqttClimate(MqttTemperatureControlEntity, ClimateEntity):
await self._publish(CONF_TEMP_COMMAND_TOPIC, mqtt_payload)
target_temp_low: float | None
if (target_temp_low := kwargs.get(ATTR_TARGET_TEMP_LOW)) is not None:
if (
target_temp_low := kwargs.get(ClimateServiceArgument.TARGET_TEMP_LOW)
) is not None:
if self._optimistic or self._topic[CONF_TEMP_LOW_STATE_TOPIC] is None:
optimistic_update = True
self._attr_native_target_temperature_low = target_temp_low
@@ -847,7 +846,9 @@ class MqttClimate(MqttTemperatureControlEntity, ClimateEntity):
await self._publish(CONF_TEMP_LOW_COMMAND_TOPIC, mqtt_payload)
target_temp_high: float | None
if (target_temp_high := kwargs.get(ATTR_TARGET_TEMP_HIGH)) is not None:
if (
target_temp_high := kwargs.get(ClimateServiceArgument.TARGET_TEMP_HIGH)
) is not None:
if self._optimistic or self._topic[CONF_TEMP_HIGH_STATE_TOPIC] is None:
optimistic_update = True
self._attr_native_target_temperature_high = target_temp_high
+3 -4
View File
@@ -8,12 +8,11 @@ import probatio
from homeassistant.components import cover
from homeassistant.components.cover import (
ATTR_POSITION,
ATTR_TILT_POSITION,
DEVICE_CLASSES_SCHEMA,
CoverEntity,
CoverEntityFeature,
CoverEntityStateAttribute,
CoverServiceArgument,
CoverState,
)
from homeassistant.config_entries import ConfigEntry
@@ -577,7 +576,7 @@ class MqttCover(MqttEntity, CoverEntity):
@override
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
"""Move the cover tilt to a specific position."""
tilt_percentage = kwargs[ATTR_TILT_POSITION]
tilt_percentage = kwargs[CoverServiceArgument.TILT_POSITION]
tilt_ranged = round(
percentage_to_ranged_value(self._tilt_range, tilt_percentage)
)
@@ -610,7 +609,7 @@ class MqttCover(MqttEntity, CoverEntity):
@override
async def async_set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position."""
position_percentage = kwargs[ATTR_POSITION]
position_percentage = kwargs[CoverServiceArgument.POSITION]
position_ranged = round(
percentage_to_ranged_value(self._pos_range, position_percentage)
)
@@ -7,18 +7,6 @@ from typing import TYPE_CHECKING, Any, cast, override
import probatio
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_MODE,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR,
ATTR_TRANSITION,
ATTR_WHITE,
ATTR_XY_COLOR,
DEFAULT_MAX_KELVIN,
DEFAULT_MIN_KELVIN,
ENTITY_ID_FORMAT,
@@ -28,6 +16,8 @@ from homeassistant.components.light import (
ColorMode,
LightEntity,
LightEntityFeature,
LightEntityStateAttribute,
LightServiceArgument,
brightness_supported,
valid_supported_color_modes,
)
@@ -339,31 +329,40 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
self._attr_is_on = last_state.state == STATE_ON
last_attributes = last_state.attributes
self._attr_brightness = last_attributes.get(
ATTR_BRIGHTNESS, self.brightness
LightEntityStateAttribute.BRIGHTNESS, self.brightness
)
self._attr_color_mode = (
last_attributes.get(ATTR_COLOR_MODE) or self.color_mode
last_attributes.get(LightEntityStateAttribute.COLOR_MODE)
or self.color_mode
)
self._attr_color_temp_kelvin = last_attributes.get(
ATTR_COLOR_TEMP_KELVIN, self.color_temp_kelvin
LightEntityStateAttribute.COLOR_TEMP_KELVIN, self.color_temp_kelvin
)
self._attr_effect = last_attributes.get(
LightEntityStateAttribute.EFFECT, self.effect
)
self._attr_hs_color = last_attributes.get(
LightEntityStateAttribute.HS_COLOR, self.hs_color
)
self._attr_rgb_color = last_attributes.get(
LightEntityStateAttribute.RGB_COLOR, self.rgb_color
)
self._attr_effect = last_attributes.get(ATTR_EFFECT, self.effect)
self._attr_hs_color = last_attributes.get(ATTR_HS_COLOR, self.hs_color)
self._attr_rgb_color = last_attributes.get(ATTR_RGB_COLOR, self.rgb_color)
self._attr_rgbw_color = last_attributes.get(
ATTR_RGBW_COLOR, self.rgbw_color
LightEntityStateAttribute.RGBW_COLOR, self.rgbw_color
)
self._attr_rgbww_color = last_attributes.get(
ATTR_RGBWW_COLOR, self.rgbww_color
LightEntityStateAttribute.RGBWW_COLOR, self.rgbww_color
)
self._attr_xy_color = last_attributes.get(
LightEntityStateAttribute.XY_COLOR, self.xy_color
)
self._attr_xy_color = last_attributes.get(ATTR_XY_COLOR, self.xy_color)
def _set_flash_and_transition(self, message: dict[str, Any], **kwargs: Any) -> None:
if ATTR_TRANSITION in kwargs:
message["transition"] = kwargs[ATTR_TRANSITION]
if LightServiceArgument.TRANSITION in kwargs:
message["transition"] = kwargs[LightServiceArgument.TRANSITION]
if ATTR_FLASH in kwargs:
flash: str = kwargs[ATTR_FLASH]
if LightServiceArgument.FLASH in kwargs:
flash: str = kwargs[LightServiceArgument.FLASH]
if flash == FLASH_LONG:
message["flash"] = self._flash_times[CONF_FLASH_TIME_LONG]
@@ -378,7 +377,7 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
if self._config[CONF_BRIGHTNESS]:
brightness = 255
else:
brightness = kwargs.pop(ATTR_BRIGHTNESS, 255)
brightness = kwargs.pop(LightServiceArgument.BRIGHTNESS, 255)
return tuple(round(i / 255 * brightness) for i in rgbxx)
def _supports_color_mode(self, color_mode: ColorMode | str) -> bool:
@@ -402,32 +401,40 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
rgbcw: tuple[int, ...]
xy_color: tuple[float, float]
if ATTR_HS_COLOR in kwargs and self._supports_color_mode(ColorMode.HS):
hs_color = kwargs[ATTR_HS_COLOR]
if LightServiceArgument.HS_COLOR in kwargs and self._supports_color_mode(
ColorMode.HS
):
hs_color = kwargs[LightServiceArgument.HS_COLOR]
message["color"] = {"h": hs_color[0], "s": hs_color[1]}
if self._optimistic:
self._attr_color_mode = ColorMode.HS
self._attr_hs_color = hs_color
should_update = True
if ATTR_RGB_COLOR in kwargs and self._supports_color_mode(ColorMode.RGB):
rgb = self._scale_rgbxx(kwargs[ATTR_RGB_COLOR], kwargs)
if LightServiceArgument.RGB_COLOR in kwargs and self._supports_color_mode(
ColorMode.RGB
):
rgb = self._scale_rgbxx(kwargs[LightServiceArgument.RGB_COLOR], kwargs)
message["color"] = {"r": rgb[0], "g": rgb[1], "b": rgb[2]}
if self._optimistic:
self._attr_color_mode = ColorMode.RGB
self._attr_rgb_color = cast(tuple[int, int, int], rgb)
should_update = True
if ATTR_RGBW_COLOR in kwargs and self._supports_color_mode(ColorMode.RGBW):
rgbw = self._scale_rgbxx(kwargs[ATTR_RGBW_COLOR], kwargs)
if LightServiceArgument.RGBW_COLOR in kwargs and self._supports_color_mode(
ColorMode.RGBW
):
rgbw = self._scale_rgbxx(kwargs[LightServiceArgument.RGBW_COLOR], kwargs)
message["color"] = {"r": rgbw[0], "g": rgbw[1], "b": rgbw[2], "w": rgbw[3]}
if self._optimistic:
self._attr_color_mode = ColorMode.RGBW
self._attr_rgbw_color = cast(tuple[int, int, int, int], rgbw)
should_update = True
if ATTR_RGBWW_COLOR in kwargs and self._supports_color_mode(ColorMode.RGBWW):
rgbcw = self._scale_rgbxx(kwargs[ATTR_RGBWW_COLOR], kwargs)
if LightServiceArgument.RGBWW_COLOR in kwargs and self._supports_color_mode(
ColorMode.RGBWW
):
rgbcw = self._scale_rgbxx(kwargs[LightServiceArgument.RGBWW_COLOR], kwargs)
message["color"] = {
"r": rgbcw[0],
"g": rgbcw[1],
@@ -440,8 +447,10 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
self._attr_rgbww_color = cast(tuple[int, int, int, int, int], rgbcw)
should_update = True
if ATTR_XY_COLOR in kwargs and self._supports_color_mode(ColorMode.XY):
xy_color = kwargs[ATTR_XY_COLOR]
if LightServiceArgument.XY_COLOR in kwargs and self._supports_color_mode(
ColorMode.XY
):
xy_color = kwargs[LightServiceArgument.XY_COLOR]
message["color"] = {"x": xy_color[0], "y": xy_color[1]}
if self._optimistic:
self._attr_color_mode = ColorMode.XY
@@ -450,44 +459,48 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
self._set_flash_and_transition(message, **kwargs)
if ATTR_BRIGHTNESS in kwargs and brightness_supported(
if LightServiceArgument.BRIGHTNESS in kwargs and brightness_supported(
self.supported_color_modes
):
device_brightness = color_util.brightness_to_value(
(1, self._config[CONF_BRIGHTNESS_SCALE]),
kwargs[ATTR_BRIGHTNESS],
kwargs[LightServiceArgument.BRIGHTNESS],
)
# Make sure the brightness is not rounded down to 0
device_brightness = max(round(device_brightness), 1)
message["brightness"] = device_brightness
if self._optimistic:
self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
self._attr_brightness = kwargs[LightServiceArgument.BRIGHTNESS]
should_update = True
if ATTR_COLOR_TEMP_KELVIN in kwargs:
if LightServiceArgument.COLOR_TEMP_KELVIN in kwargs:
message["color_temp"] = (
kwargs[ATTR_COLOR_TEMP_KELVIN]
kwargs[LightServiceArgument.COLOR_TEMP_KELVIN]
if self._color_temp_kelvin
else color_util.color_temperature_kelvin_to_mired(
kwargs[ATTR_COLOR_TEMP_KELVIN]
kwargs[LightServiceArgument.COLOR_TEMP_KELVIN]
)
)
if self._optimistic:
self._attr_color_mode = ColorMode.COLOR_TEMP
self._attr_color_temp_kelvin = kwargs[ATTR_COLOR_TEMP_KELVIN]
self._attr_color_temp_kelvin = kwargs[
LightServiceArgument.COLOR_TEMP_KELVIN
]
self._attr_hs_color = None
should_update = True
if ATTR_EFFECT in kwargs:
message["effect"] = kwargs[ATTR_EFFECT]
if LightServiceArgument.EFFECT in kwargs:
message["effect"] = kwargs[LightServiceArgument.EFFECT]
if self._optimistic:
self._attr_effect = kwargs[ATTR_EFFECT]
self._attr_effect = kwargs[LightServiceArgument.EFFECT]
should_update = True
if ATTR_WHITE in kwargs and self._supports_color_mode(ColorMode.WHITE):
white_normalized = kwargs[ATTR_WHITE] / DEFAULT_WHITE_SCALE
if LightServiceArgument.WHITE in kwargs and self._supports_color_mode(
ColorMode.WHITE
):
white_normalized = kwargs[LightServiceArgument.WHITE] / DEFAULT_WHITE_SCALE
white_scale = self._config[CONF_WHITE_SCALE]
device_white_level = min(round(white_normalized * white_scale), white_scale)
# Make sure the brightness is not rounded down to 0
@@ -496,7 +509,7 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
if self._optimistic:
self._attr_color_mode = ColorMode.WHITE
self._attr_brightness = kwargs[ATTR_WHITE]
self._attr_brightness = kwargs[LightServiceArgument.WHITE]
should_update = True
await self.async_publish_with_config(
@@ -7,12 +7,6 @@ from typing import Any, override
import probatio
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_TRANSITION,
DEFAULT_MAX_KELVIN,
DEFAULT_MIN_KELVIN,
ENTITY_ID_FORMAT,
@@ -20,6 +14,7 @@ from homeassistant.components.light import (
LightEntity,
LightEntityFeature,
LightEntityStateAttribute,
LightServiceArgument,
filter_supported_color_modes,
)
from homeassistant.const import (
@@ -390,28 +385,30 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
if self._optimistic:
self._attr_is_on = True
if ATTR_BRIGHTNESS in kwargs:
values["brightness"] = int(kwargs[ATTR_BRIGHTNESS])
if LightServiceArgument.BRIGHTNESS in kwargs:
values["brightness"] = int(kwargs[LightServiceArgument.BRIGHTNESS])
if self._optimistic:
self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
self._attr_brightness = kwargs[LightServiceArgument.BRIGHTNESS]
if ATTR_COLOR_TEMP_KELVIN in kwargs:
if LightServiceArgument.COLOR_TEMP_KELVIN in kwargs:
values["color_temp"] = (
kwargs[ATTR_COLOR_TEMP_KELVIN]
kwargs[LightServiceArgument.COLOR_TEMP_KELVIN]
if self._color_temp_kelvin
else color_util.color_temperature_kelvin_to_mired(
kwargs[ATTR_COLOR_TEMP_KELVIN]
kwargs[LightServiceArgument.COLOR_TEMP_KELVIN]
)
)
if self._optimistic:
self._attr_color_temp_kelvin = kwargs[ATTR_COLOR_TEMP_KELVIN]
self._attr_color_temp_kelvin = kwargs[
LightServiceArgument.COLOR_TEMP_KELVIN
]
self._attr_hs_color = None
self._update_color_mode()
if ATTR_HS_COLOR in kwargs:
hs_color = kwargs[ATTR_HS_COLOR]
if LightServiceArgument.HS_COLOR in kwargs:
hs_color = kwargs[LightServiceArgument.HS_COLOR]
# If there's a brightness topic set, we don't want to scale the RGB
# values given using the brightness.
@@ -419,7 +416,7 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
brightness = 255
else:
brightness = kwargs.get(
ATTR_BRIGHTNESS,
LightServiceArgument.BRIGHTNESS,
self._attr_brightness if self._attr_brightness is not None else 255,
)
rgb = color_util.color_hsv_to_RGB(
@@ -433,20 +430,20 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
if self._optimistic:
self._attr_color_temp_kelvin = None
self._attr_hs_color = kwargs[ATTR_HS_COLOR]
self._attr_hs_color = kwargs[LightServiceArgument.HS_COLOR]
self._update_color_mode()
if ATTR_EFFECT in kwargs:
values["effect"] = kwargs.get(ATTR_EFFECT)
if LightServiceArgument.EFFECT in kwargs:
values["effect"] = kwargs.get(LightServiceArgument.EFFECT)
if self._optimistic:
self._attr_effect = kwargs[ATTR_EFFECT]
self._attr_effect = kwargs[LightServiceArgument.EFFECT]
if ATTR_FLASH in kwargs:
values["flash"] = kwargs.get(ATTR_FLASH)
if LightServiceArgument.FLASH in kwargs:
values["flash"] = kwargs.get(LightServiceArgument.FLASH)
if ATTR_TRANSITION in kwargs:
values["transition"] = kwargs[ATTR_TRANSITION]
if LightServiceArgument.TRANSITION in kwargs:
values["transition"] = kwargs[LightServiceArgument.TRANSITION]
await self.async_publish_with_config(
str(self._topics[CONF_COMMAND_TOPIC]),
@@ -466,8 +463,8 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
if self._optimistic:
self._attr_is_on = False
if ATTR_TRANSITION in kwargs:
values["transition"] = kwargs[ATTR_TRANSITION]
if LightServiceArgument.TRANSITION in kwargs:
values["transition"] = kwargs[LightServiceArgument.TRANSITION]
await self.async_publish_with_config(
str(self._topics[CONF_COMMAND_TOPIC]),
@@ -7,7 +7,6 @@ import probatio
from homeassistant.components import water_heater
from homeassistant.components.water_heater import (
ATTR_OPERATION_MODE,
DEFAULT_MIN_TEMP,
STATE_ECO,
STATE_ELECTRIC,
@@ -18,11 +17,11 @@ from homeassistant.components.water_heater import (
WaterHeaterCapabilityAttribute,
WaterHeaterEntity,
WaterHeaterEntityFeature,
WaterHeaterServiceArgument,
WaterHeaterStateAttribute,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_NAME,
CONF_OPTIMISTIC,
CONF_PAYLOAD_OFF,
@@ -322,10 +321,12 @@ class MqttWaterHeater(MqttTemperatureControlEntity, WaterHeaterEntity):
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
operation_mode: str | None
if (operation_mode := kwargs.get(ATTR_OPERATION_MODE)) is not None:
if (
operation_mode := kwargs.get(WaterHeaterServiceArgument.OPERATION_MODE)
) is not None:
await self.async_set_operation_mode(operation_mode)
temperature: float = kwargs[ATTR_TEMPERATURE]
temperature: float = kwargs[WaterHeaterServiceArgument.TEMPERATURE]
mqtt_payload = self._command_templates[CONF_TEMP_COMMAND_TEMPLATE](temperature)
await self._publish(CONF_TEMP_COMMAND_TOPIC, mqtt_payload)