Reduce duplicate publish code in mqtt (#118163)
This commit is contained in:
parent
5d37217d96
commit
80371d3a73
22 changed files with 87 additions and 335 deletions
|
@ -310,13 +310,7 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
|
|||
"""Publish via mqtt."""
|
||||
variables = {"action": action, "code": code}
|
||||
payload = self._command_template(None, variables=variables)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
||||
def _validate_code(self, code: str | None, state: str) -> bool:
|
||||
"""Validate given code."""
|
||||
|
|
|
@ -14,13 +14,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .config import DEFAULT_RETAIN, MQTT_BASE_SCHEMA
|
||||
from .const import (
|
||||
CONF_COMMAND_TEMPLATE,
|
||||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
)
|
||||
from .const import CONF_COMMAND_TEMPLATE, CONF_COMMAND_TOPIC, CONF_RETAIN
|
||||
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||
from .models import MqttCommandTemplate
|
||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||
|
@ -91,10 +85,4 @@ class MqttButton(MqttEntity, ButtonEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
payload = self._command_template(self._config[CONF_PAYLOAD_PRESS])
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
|
|
@ -516,13 +516,7 @@ class MqttTemperatureControlEntity(MqttEntity, ABC):
|
|||
|
||||
async def _publish(self, topic: str, payload: PublishPayloadType) -> None:
|
||||
if self._topic[topic] is not None:
|
||||
await self.async_publish(
|
||||
self._topic[topic],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._topic[topic], payload)
|
||||
|
||||
async def _set_climate_attribute(
|
||||
self,
|
||||
|
|
|
@ -522,12 +522,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
self._config[CONF_PAYLOAD_OPEN],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_OPEN]
|
||||
)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that cover has changed state.
|
||||
|
@ -541,12 +537,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
self._config[CONF_PAYLOAD_CLOSE],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_CLOSE]
|
||||
)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that cover has changed state.
|
||||
|
@ -560,12 +552,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
self._config[CONF_PAYLOAD_STOP],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_STOP]
|
||||
)
|
||||
|
||||
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
||||
|
@ -580,12 +568,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
"tilt_max": self._config.get(CONF_TILT_MAX),
|
||||
}
|
||||
tilt_payload = self._set_tilt_template(tilt_open_position, variables=variables)
|
||||
await self.async_publish(
|
||||
self._config[CONF_TILT_COMMAND_TOPIC],
|
||||
tilt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_TILT_COMMAND_TOPIC], tilt_payload
|
||||
)
|
||||
if self._tilt_optimistic:
|
||||
self._attr_current_cover_tilt_position = self._tilt_open_percentage
|
||||
|
@ -605,12 +589,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
tilt_payload = self._set_tilt_template(
|
||||
tilt_closed_position, variables=variables
|
||||
)
|
||||
await self.async_publish(
|
||||
self._config[CONF_TILT_COMMAND_TOPIC],
|
||||
tilt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_TILT_COMMAND_TOPIC], tilt_payload
|
||||
)
|
||||
if self._tilt_optimistic:
|
||||
self._attr_current_cover_tilt_position = self._tilt_closed_percentage
|
||||
|
@ -633,13 +613,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
"tilt_max": self._config.get(CONF_TILT_MAX),
|
||||
}
|
||||
tilt_rendered = self._set_tilt_template(tilt_ranged, variables=variables)
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_TILT_COMMAND_TOPIC],
|
||||
tilt_rendered,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_TILT_COMMAND_TOPIC], tilt_rendered
|
||||
)
|
||||
if self._tilt_optimistic:
|
||||
_LOGGER.debug("Set tilt value optimistic")
|
||||
|
@ -663,13 +638,8 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
position_rendered = self._set_position_template(
|
||||
position_ranged, variables=variables
|
||||
)
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_SET_POSITION_TOPIC],
|
||||
position_rendered,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_SET_POSITION_TOPIC], position_rendered
|
||||
)
|
||||
if self._optimistic:
|
||||
self._update_state(
|
||||
|
|
|
@ -45,7 +45,6 @@ from .const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
CONF_STATE_VALUE_TEMPLATE,
|
||||
PAYLOAD_NONE,
|
||||
|
@ -497,12 +496,8 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[CONF_STATE](self._payload["STATE_ON"])
|
||||
await self.async_publish(
|
||||
self._topic[CONF_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
if percentage:
|
||||
await self.async_set_percentage(percentage)
|
||||
|
@ -518,12 +513,8 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[CONF_STATE](self._payload["STATE_OFF"])
|
||||
await self.async_publish(
|
||||
self._topic[CONF_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
if self._optimistic:
|
||||
self._attr_is_on = False
|
||||
|
@ -538,14 +529,9 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
percentage_to_ranged_value(self._speed_range, percentage)
|
||||
)
|
||||
mqtt_payload = self._command_templates[ATTR_PERCENTAGE](percentage_payload)
|
||||
await self.async_publish(
|
||||
self._topic[CONF_PERCENTAGE_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_PERCENTAGE_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
|
||||
if self._optimistic_percentage:
|
||||
self._attr_percentage = percentage
|
||||
self.async_write_ha_state()
|
||||
|
@ -556,15 +542,9 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[ATTR_PRESET_MODE](preset_mode)
|
||||
|
||||
await self.async_publish(
|
||||
self._topic[CONF_PRESET_MODE_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_PRESET_MODE_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
|
||||
if self._optimistic_preset_mode:
|
||||
self._attr_preset_mode = preset_mode
|
||||
self.async_write_ha_state()
|
||||
|
@ -582,15 +562,9 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
mqtt_payload = self._command_templates[ATTR_OSCILLATING](
|
||||
self._payload["OSCILLATE_OFF_PAYLOAD"]
|
||||
)
|
||||
|
||||
await self.async_publish(
|
||||
self._topic[CONF_OSCILLATION_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_OSCILLATION_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
|
||||
if self._optimistic_oscillation:
|
||||
self._attr_oscillating = oscillating
|
||||
self.async_write_ha_state()
|
||||
|
@ -601,15 +575,9 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[ATTR_DIRECTION](direction)
|
||||
|
||||
await self.async_publish(
|
||||
self._topic[CONF_DIRECTION_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_DIRECTION_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
|
||||
if self._optimistic_direction:
|
||||
self._attr_current_direction = direction
|
||||
self.async_write_ha_state()
|
||||
|
|
|
@ -47,7 +47,6 @@ from .const import (
|
|||
CONF_CURRENT_HUMIDITY_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
CONF_STATE_VALUE_TEMPLATE,
|
||||
PAYLOAD_NONE,
|
||||
|
@ -456,12 +455,8 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[CONF_STATE](self._payload["STATE_ON"])
|
||||
await self.async_publish(
|
||||
self._topic[CONF_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
if self._optimistic:
|
||||
self._attr_is_on = True
|
||||
|
@ -473,12 +468,8 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[CONF_STATE](self._payload["STATE_OFF"])
|
||||
await self.async_publish(
|
||||
self._topic[CONF_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
if self._optimistic:
|
||||
self._attr_is_on = False
|
||||
|
@ -490,14 +481,9 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
mqtt_payload = self._command_templates[ATTR_HUMIDITY](humidity)
|
||||
await self.async_publish(
|
||||
self._topic[CONF_TARGET_HUMIDITY_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_TARGET_HUMIDITY_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
|
||||
if self._optimistic_target_humidity:
|
||||
self._attr_target_humidity = humidity
|
||||
self.async_write_ha_state()
|
||||
|
@ -512,15 +498,9 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
|||
return
|
||||
|
||||
mqtt_payload = self._command_templates[ATTR_MODE](mode)
|
||||
|
||||
await self.async_publish(
|
||||
self._topic[CONF_MODE_COMMAND_TOPIC],
|
||||
mqtt_payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_MODE_COMMAND_TOPIC], mqtt_payload
|
||||
)
|
||||
|
||||
if self._optimistic_mode:
|
||||
self._attr_mode = mode
|
||||
self.async_write_ha_state()
|
||||
|
|
|
@ -213,14 +213,7 @@ class MqttLawnMower(MqttEntity, LawnMowerEntity, RestoreEntity):
|
|||
if self._attr_assumed_state:
|
||||
self._attr_activity = activity
|
||||
self.async_write_ha_state()
|
||||
|
||||
await self.async_publish(
|
||||
self._command_topics[option],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._command_topics[option], payload)
|
||||
|
||||
async def async_start_mowing(self) -> None:
|
||||
"""Start or resume mowing."""
|
||||
|
|
|
@ -49,7 +49,6 @@ from ..const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
CONF_STATE_VALUE_TEMPLATE,
|
||||
PAYLOAD_NONE,
|
||||
|
@ -665,13 +664,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||
|
||||
async def publish(topic: str, payload: PublishPayloadType) -> None:
|
||||
"""Publish an MQTT message."""
|
||||
await self.async_publish(
|
||||
str(self._topic[topic]),
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(str(self._topic[topic]), payload)
|
||||
|
||||
def scale_rgbx(
|
||||
color: tuple[int, ...],
|
||||
|
@ -876,12 +869,8 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
str(self._topic[CONF_COMMAND_TOPIC]),
|
||||
self._payload["off"],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
str(self._topic[CONF_COMMAND_TOPIC]), self._payload["off"]
|
||||
)
|
||||
|
||||
if self._optimistic:
|
||||
|
|
|
@ -738,12 +738,8 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
|||
self._attr_brightness = kwargs[ATTR_WHITE]
|
||||
should_update = True
|
||||
|
||||
await self.async_publish(
|
||||
str(self._topic[CONF_COMMAND_TOPIC]),
|
||||
json_dumps(message),
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
str(self._topic[CONF_COMMAND_TOPIC]), json_dumps(message)
|
||||
)
|
||||
|
||||
if self._optimistic:
|
||||
|
@ -763,12 +759,8 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
|||
|
||||
self._set_flash_and_transition(message, **kwargs)
|
||||
|
||||
await self.async_publish(
|
||||
str(self._topic[CONF_COMMAND_TOPIC]),
|
||||
json_dumps(message),
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
str(self._topic[CONF_COMMAND_TOPIC]), json_dumps(message)
|
||||
)
|
||||
|
||||
if self._optimistic:
|
||||
|
|
|
@ -41,7 +41,6 @@ from ..const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
PAYLOAD_NONE,
|
||||
)
|
||||
|
@ -365,12 +364,9 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
|
|||
if ATTR_TRANSITION in kwargs:
|
||||
values["transition"] = kwargs[ATTR_TRANSITION]
|
||||
|
||||
await self.async_publish(
|
||||
await self.async_publish_with_config(
|
||||
str(self._topics[CONF_COMMAND_TOPIC]),
|
||||
self._command_templates[CONF_COMMAND_ON_TEMPLATE](None, values),
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
|
||||
if self._optimistic:
|
||||
|
@ -388,12 +384,9 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
|
|||
if ATTR_TRANSITION in kwargs:
|
||||
values["transition"] = kwargs[ATTR_TRANSITION]
|
||||
|
||||
await self.async_publish(
|
||||
await self.async_publish_with_config(
|
||||
str(self._topics[CONF_COMMAND_TOPIC]),
|
||||
self._command_templates[CONF_COMMAND_OFF_TEMPLATE](None, values),
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
|
||||
if self._optimistic:
|
||||
|
|
|
@ -32,7 +32,6 @@ from .const import (
|
|||
CONF_ENCODING,
|
||||
CONF_PAYLOAD_RESET,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_OPEN,
|
||||
CONF_STATE_OPENING,
|
||||
CONF_STATE_TOPIC,
|
||||
|
@ -255,13 +254,7 @@ class MqttLock(MqttEntity, LockEntity):
|
|||
ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None
|
||||
}
|
||||
payload = self._command_template(self._config[CONF_PAYLOAD_LOCK], tpl_vars)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that the lock has changed state.
|
||||
self._attr_is_locked = True
|
||||
|
@ -276,13 +269,7 @@ class MqttLock(MqttEntity, LockEntity):
|
|||
ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None
|
||||
}
|
||||
payload = self._command_template(self._config[CONF_PAYLOAD_UNLOCK], tpl_vars)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that the lock has changed state.
|
||||
self._attr_is_locked = False
|
||||
|
@ -297,13 +284,7 @@ class MqttLock(MqttEntity, LockEntity):
|
|||
ATTR_CODE: kwargs.get(ATTR_CODE) if kwargs else None
|
||||
}
|
||||
payload = self._command_template(self._config[CONF_PAYLOAD_OPEN], tpl_vars)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that the lock unlocks when opened.
|
||||
self._attr_is_open = True
|
||||
|
|
|
@ -83,6 +83,7 @@ from .const import (
|
|||
CONF_PAYLOAD_AVAILABLE,
|
||||
CONF_PAYLOAD_NOT_AVAILABLE,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_SCHEMA,
|
||||
CONF_SERIAL_NUMBER,
|
||||
CONF_SUGGESTED_AREA,
|
||||
|
@ -1156,6 +1157,18 @@ class MqttEntity(
|
|||
encoding,
|
||||
)
|
||||
|
||||
async def async_publish_with_config(
|
||||
self, topic: str, payload: PublishPayloadType
|
||||
) -> None:
|
||||
"""Publish payload to a topic using config."""
|
||||
await self.async_publish(
|
||||
topic,
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@abstractmethod
|
||||
def config_schema() -> vol.Schema:
|
||||
|
|
|
@ -14,13 +14,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .config import DEFAULT_RETAIN, MQTT_BASE_SCHEMA
|
||||
from .const import (
|
||||
CONF_COMMAND_TEMPLATE,
|
||||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
)
|
||||
from .const import CONF_COMMAND_TEMPLATE, CONF_COMMAND_TOPIC, CONF_RETAIN
|
||||
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||
from .models import MqttCommandTemplate
|
||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||
|
@ -83,10 +77,4 @@ class MqttNotify(MqttEntity, NotifyEntity):
|
|||
async def async_send_message(self, message: str, title: str | None = None) -> None:
|
||||
"""Send a message."""
|
||||
payload = self._command_template(message)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
|
|
@ -39,7 +39,6 @@ from .const import (
|
|||
CONF_ENCODING,
|
||||
CONF_PAYLOAD_RESET,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
)
|
||||
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||
|
@ -239,11 +238,4 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||
if self._attr_assumed_state:
|
||||
self._attr_native_value = current_number
|
||||
self.async_write_ha_state()
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
|
|
@ -16,7 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .config import MQTT_BASE_SCHEMA
|
||||
from .const import CONF_COMMAND_TOPIC, CONF_ENCODING, CONF_QOS, CONF_RETAIN
|
||||
from .const import CONF_COMMAND_TOPIC, CONF_RETAIN
|
||||
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||
from .util import valid_publish_topic
|
||||
|
@ -83,10 +83,6 @@ class MqttScene(
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
self._config[CONF_PAYLOAD_ON],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_ON]
|
||||
)
|
||||
|
|
|
@ -25,7 +25,6 @@ from .const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
)
|
||||
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||
|
@ -174,11 +173,4 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
|
|||
if self._attr_assumed_state:
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
|
|
@ -43,7 +43,6 @@ from .const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
CONF_STATE_VALUE_TEMPLATE,
|
||||
PAYLOAD_EMPTY_JSON,
|
||||
|
@ -319,13 +318,7 @@ class MqttSiren(MqttEntity, SirenEntity):
|
|||
else:
|
||||
payload = json_dumps(template_variables)
|
||||
if payload and str(payload) != PAYLOAD_NONE:
|
||||
await self.async_publish(
|
||||
self._config[topic],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[topic], payload)
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the siren on.
|
||||
|
|
|
@ -33,7 +33,6 @@ from .const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
PAYLOAD_NONE,
|
||||
)
|
||||
|
@ -162,12 +161,8 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
self._config[CONF_PAYLOAD_ON],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_ON]
|
||||
)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that switch has changed state.
|
||||
|
@ -179,12 +174,8 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
self._config[CONF_PAYLOAD_OFF],
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_OFF]
|
||||
)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that switch has changed state.
|
||||
|
|
|
@ -32,7 +32,6 @@ from .const import (
|
|||
CONF_COMMAND_TOPIC,
|
||||
CONF_ENCODING,
|
||||
CONF_QOS,
|
||||
CONF_RETAIN,
|
||||
CONF_STATE_TOPIC,
|
||||
)
|
||||
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||
|
@ -204,14 +203,7 @@ class MqttTextEntity(MqttEntity, TextEntity):
|
|||
async def async_set_value(self, value: str) -> None:
|
||||
"""Change the text."""
|
||||
payload = self._command_template(value)
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
if self._optimistic:
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
|
|
|
@ -265,14 +265,7 @@ class MqttUpdate(MqttEntity, UpdateEntity, RestoreEntity):
|
|||
) -> None:
|
||||
"""Update the current value."""
|
||||
payload = self._config[CONF_PAYLOAD_INSTALL]
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
||||
@property
|
||||
def supported_features(self) -> UpdateEntityFeature:
|
||||
|
|
|
@ -360,13 +360,8 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity):
|
|||
"""Publish a command."""
|
||||
if self._command_topic is None:
|
||||
return
|
||||
|
||||
await self.async_publish(
|
||||
self._command_topic,
|
||||
self._payloads[_FEATURE_PAYLOADS[feature]],
|
||||
qos=self._config[CONF_QOS],
|
||||
retain=self._config[CONF_RETAIN],
|
||||
encoding=self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._command_topic, self._payloads[_FEATURE_PAYLOADS[feature]]
|
||||
)
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
@ -402,13 +397,7 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity):
|
|||
or (fan_speed not in self.fan_speed_list)
|
||||
):
|
||||
return
|
||||
await self.async_publish(
|
||||
self._set_fan_speed_topic,
|
||||
fan_speed,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._set_fan_speed_topic, fan_speed)
|
||||
|
||||
async def async_send_command(
|
||||
self,
|
||||
|
@ -428,10 +417,4 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity):
|
|||
payload = json_dumps(message)
|
||||
else:
|
||||
payload = command
|
||||
await self.async_publish(
|
||||
self._send_command_topic,
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._send_command_topic, payload)
|
||||
|
|
|
@ -376,13 +376,7 @@ class MqttValve(MqttEntity, ValveEntity):
|
|||
payload = self._command_template(
|
||||
self._config.get(CONF_PAYLOAD_OPEN, DEFAULT_PAYLOAD_OPEN)
|
||||
)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that valve has changed state.
|
||||
self._update_state(STATE_OPEN)
|
||||
|
@ -396,13 +390,7 @@ class MqttValve(MqttEntity, ValveEntity):
|
|||
payload = self._command_template(
|
||||
self._config.get(CONF_PAYLOAD_CLOSE, DEFAULT_PAYLOAD_CLOSE)
|
||||
)
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
if self._optimistic:
|
||||
# Optimistically assume that valve has changed state.
|
||||
self._update_state(STATE_CLOSED)
|
||||
|
@ -414,13 +402,7 @@ class MqttValve(MqttEntity, ValveEntity):
|
|||
This method is a coroutine.
|
||||
"""
|
||||
payload = self._command_template(self._config[CONF_PAYLOAD_STOP])
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
payload,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|
||||
|
||||
async def async_set_valve_position(self, position: int) -> None:
|
||||
"""Move the valve to a specific position."""
|
||||
|
@ -434,13 +416,8 @@ class MqttValve(MqttEntity, ValveEntity):
|
|||
"position_closed": self._config[CONF_POSITION_CLOSED],
|
||||
}
|
||||
rendered_position = self._command_template(scaled_position, variables=variables)
|
||||
|
||||
await self.async_publish(
|
||||
self._config[CONF_COMMAND_TOPIC],
|
||||
rendered_position,
|
||||
self._config[CONF_QOS],
|
||||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
await self.async_publish_with_config(
|
||||
self._config[CONF_COMMAND_TOPIC], rendered_position
|
||||
)
|
||||
if self._optimistic:
|
||||
self._update_state(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue