diff --git a/homeassistant/components/mqtt/alarm_control_panel.py b/homeassistant/components/mqtt/alarm_control_panel.py index d0a71a5a109..55d33e2ca41 100644 --- a/homeassistant/components/mqtt/alarm_control_panel.py +++ b/homeassistant/components/mqtt/alarm_control_panel.py @@ -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.""" diff --git a/homeassistant/components/mqtt/button.py b/homeassistant/components/mqtt/button.py index 93fe0c4598e..b5fe2f17f64 100644 --- a/homeassistant/components/mqtt/button.py +++ b/homeassistant/components/mqtt/button.py @@ -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) diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index 5e866eedf17..d0a9175d9fc 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -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, diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index 33eb5d65c02..c0ee5d4254b 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -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( diff --git a/homeassistant/components/mqtt/fan.py b/homeassistant/components/mqtt/fan.py index 65961f7967a..7f5c521e9f3 100644 --- a/homeassistant/components/mqtt/fan.py +++ b/homeassistant/components/mqtt/fan.py @@ -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() diff --git a/homeassistant/components/mqtt/humidifier.py b/homeassistant/components/mqtt/humidifier.py index 00619605771..6bb4fdb8561 100644 --- a/homeassistant/components/mqtt/humidifier.py +++ b/homeassistant/components/mqtt/humidifier.py @@ -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() diff --git a/homeassistant/components/mqtt/lawn_mower.py b/homeassistant/components/mqtt/lawn_mower.py index dc592f16b48..65d1442c8de 100644 --- a/homeassistant/components/mqtt/lawn_mower.py +++ b/homeassistant/components/mqtt/lawn_mower.py @@ -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.""" diff --git a/homeassistant/components/mqtt/light/schema_basic.py b/homeassistant/components/mqtt/light/schema_basic.py index 394b34747b0..db6d695b4bb 100644 --- a/homeassistant/components/mqtt/light/schema_basic.py +++ b/homeassistant/components/mqtt/light/schema_basic.py @@ -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: diff --git a/homeassistant/components/mqtt/light/schema_json.py b/homeassistant/components/mqtt/light/schema_json.py index 3ae3e6a799d..3ec88026e9a 100644 --- a/homeassistant/components/mqtt/light/schema_json.py +++ b/homeassistant/components/mqtt/light/schema_json.py @@ -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: diff --git a/homeassistant/components/mqtt/light/schema_template.py b/homeassistant/components/mqtt/light/schema_template.py index 5a86ba84285..cc734253512 100644 --- a/homeassistant/components/mqtt/light/schema_template.py +++ b/homeassistant/components/mqtt/light/schema_template.py @@ -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: diff --git a/homeassistant/components/mqtt/lock.py b/homeassistant/components/mqtt/lock.py index f9da70377a7..ce0b97e74bf 100644 --- a/homeassistant/components/mqtt/lock.py +++ b/homeassistant/components/mqtt/lock.py @@ -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 diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index ed15a39a6bb..a89199ed173 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -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: diff --git a/homeassistant/components/mqtt/notify.py b/homeassistant/components/mqtt/notify.py index 57a213491a7..d3e6bdd3fcb 100644 --- a/homeassistant/components/mqtt/notify.py +++ b/homeassistant/components/mqtt/notify.py @@ -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) diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index defc880794d..ededdd14c12 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -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) diff --git a/homeassistant/components/mqtt/scene.py b/homeassistant/components/mqtt/scene.py index 24b4415a4b2..4381a4ea9a3 100644 --- a/homeassistant/components/mqtt/scene.py +++ b/homeassistant/components/mqtt/scene.py @@ -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] ) diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 14671abeac9..6526161d2de 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -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) diff --git a/homeassistant/components/mqtt/siren.py b/homeassistant/components/mqtt/siren.py index 819064e82dc..09fd5db2684 100644 --- a/homeassistant/components/mqtt/siren.py +++ b/homeassistant/components/mqtt/siren.py @@ -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. diff --git a/homeassistant/components/mqtt/switch.py b/homeassistant/components/mqtt/switch.py index 168cf903091..f66a7a80d3d 100644 --- a/homeassistant/components/mqtt/switch.py +++ b/homeassistant/components/mqtt/switch.py @@ -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. diff --git a/homeassistant/components/mqtt/text.py b/homeassistant/components/mqtt/text.py index 5bbed474e43..cc688403a5a 100644 --- a/homeassistant/components/mqtt/text.py +++ b/homeassistant/components/mqtt/text.py @@ -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() diff --git a/homeassistant/components/mqtt/update.py b/homeassistant/components/mqtt/update.py index 37d74489bbf..d9d8c961ae8 100644 --- a/homeassistant/components/mqtt/update.py +++ b/homeassistant/components/mqtt/update.py @@ -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: diff --git a/homeassistant/components/mqtt/vacuum.py b/homeassistant/components/mqtt/vacuum.py index 15b85edb229..b750fdcb49c 100644 --- a/homeassistant/components/mqtt/vacuum.py +++ b/homeassistant/components/mqtt/vacuum.py @@ -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) diff --git a/homeassistant/components/mqtt/valve.py b/homeassistant/components/mqtt/valve.py index dd6a6c3bf35..154680cf14a 100644 --- a/homeassistant/components/mqtt/valve.py +++ b/homeassistant/components/mqtt/valve.py @@ -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(