Skip to content

Commit

Permalink
Remove mqtt publish templates after 6 months of deprecation (home-ass…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh authored and sorgfresser committed Dec 27, 2024
1 parent 722ae44 commit 5e78d7d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 340 deletions.
105 changes: 16 additions & 89 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from collections.abc import Callable
from datetime import datetime
import logging
from typing import TYPE_CHECKING, Any, cast
from typing import Any, cast

import voluptuous as vol

from homeassistant import config as conf_util
from homeassistant.components import websocket_api
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DISCOVERY, CONF_PAYLOAD, SERVICE_RELOAD
from homeassistant.const import CONF_DISCOVERY, SERVICE_RELOAD
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import (
ConfigValidationError,
Expand All @@ -25,7 +25,6 @@
entity_registry as er,
event as ev,
issue_registry as ir,
template,
)
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand Down Expand Up @@ -113,8 +112,6 @@
SERVICE_PUBLISH = "publish"
SERVICE_DUMP = "dump"

ATTR_TOPIC_TEMPLATE = "topic_template"
ATTR_PAYLOAD_TEMPLATE = "payload_template"
ATTR_EVALUATE_PAYLOAD = "evaluate_payload"

MAX_RECONNECT_WAIT = 300 # seconds
Expand Down Expand Up @@ -155,25 +152,16 @@
extra=vol.ALLOW_EXTRA,
)


# The use of a topic_template and payload_template in an mqtt publish action call
# have been deprecated with HA Core 2024.8.0 and will be removed with HA Core 2025.2.0

# Publish action call validation schema
MQTT_PUBLISH_SCHEMA = vol.All(
vol.Schema(
{
vol.Exclusive(ATTR_TOPIC, CONF_TOPIC): valid_publish_topic,
vol.Exclusive(ATTR_TOPIC_TEMPLATE, CONF_TOPIC): cv.string,
vol.Exclusive(ATTR_PAYLOAD, CONF_PAYLOAD): cv.string,
vol.Exclusive(ATTR_PAYLOAD_TEMPLATE, CONF_PAYLOAD): cv.string,
vol.Optional(ATTR_EVALUATE_PAYLOAD): cv.boolean,
vol.Optional(ATTR_QOS, default=DEFAULT_QOS): valid_qos_schema,
vol.Optional(ATTR_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
},
required=True,
),
cv.has_at_least_one_key(ATTR_TOPIC, ATTR_TOPIC_TEMPLATE),
MQTT_PUBLISH_SCHEMA = vol.Schema(
{
vol.Required(ATTR_TOPIC): valid_publish_topic,
vol.Required(ATTR_PAYLOAD): cv.string,
vol.Optional(ATTR_EVALUATE_PAYLOAD): cv.boolean,
vol.Optional(ATTR_QOS, default=DEFAULT_QOS): valid_qos_schema,
vol.Optional(ATTR_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
},
required=True,
)


Expand Down Expand Up @@ -233,86 +221,25 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

async def async_publish_service(call: ServiceCall) -> None:
"""Handle MQTT publish service calls."""
msg_topic: str | None = call.data.get(ATTR_TOPIC)
msg_topic_template: str | None = call.data.get(ATTR_TOPIC_TEMPLATE)
msg_topic: str = call.data[ATTR_TOPIC]

if not mqtt_config_entry_enabled(hass):
raise ServiceValidationError(
translation_key="mqtt_not_setup_cannot_publish",
translation_domain=DOMAIN,
translation_placeholders={
"topic": str(msg_topic or msg_topic_template)
},
translation_placeholders={"topic": msg_topic},
)

mqtt_data = hass.data[DATA_MQTT]
payload: PublishPayloadType = call.data.get(ATTR_PAYLOAD)
payload: PublishPayloadType = call.data[ATTR_PAYLOAD]
evaluate_payload: bool = call.data.get(ATTR_EVALUATE_PAYLOAD, False)
payload_template: str | None = call.data.get(ATTR_PAYLOAD_TEMPLATE)
qos: int = call.data[ATTR_QOS]
retain: bool = call.data[ATTR_RETAIN]
if msg_topic_template is not None:
# The use of a topic_template in an mqtt publish action call
# has been deprecated with HA Core 2024.8.0
# and will be removed with HA Core 2025.2.0
rendered_topic: Any = MqttCommandTemplate(
template.Template(msg_topic_template, hass),
).async_render()
ir.async_create_issue(
hass,
DOMAIN,
f"topic_template_deprecation_{rendered_topic}",
breaks_in_ha_version="2025.2.0",
is_fixable=False,
severity=ir.IssueSeverity.WARNING,
translation_key="topic_template_deprecation",
translation_placeholders={
"topic_template": msg_topic_template,
"topic": rendered_topic,
},
)
try:
msg_topic = valid_publish_topic(rendered_topic)
except vol.Invalid as err:
err_str = str(err)
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="invalid_publish_topic",
translation_placeholders={
"error": err_str,
"topic": str(rendered_topic),
"topic_template": str(msg_topic_template),
},
) from err

if payload_template is not None:
# The use of a payload_template in an mqtt publish action call
# has been deprecated with HA Core 2024.8.0
# and will be removed with HA Core 2025.2.0
if TYPE_CHECKING:
assert msg_topic is not None
ir.async_create_issue(
hass,
DOMAIN,
f"payload_template_deprecation_{msg_topic}",
breaks_in_ha_version="2025.2.0",
is_fixable=False,
severity=ir.IssueSeverity.WARNING,
translation_key="payload_template_deprecation",
translation_placeholders={
"topic": msg_topic,
"payload_template": payload_template,
},
)
payload = MqttCommandTemplate(
template.Template(payload_template, hass)
).async_render()
elif evaluate_payload:

if evaluate_payload:
# Convert quoted binary literal to raw data
payload = convert_outgoing_mqtt_payload(payload)

if TYPE_CHECKING:
assert msg_topic is not None
await mqtt_data.client.async_publish(msg_topic, payload, qos, retain)

hass.services.async_register(
Expand Down
8 changes: 0 additions & 8 deletions homeassistant/components/mqtt/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
"invalid_platform_config": {
"title": "Invalid config found for mqtt {domain} item",
"description": "Home Assistant detected an invalid config for a manually configured item.\n\nPlatform domain: **{domain}**\nConfiguration file: **{config_file}**\nNear line: **{line}**\nConfiguration found:\n```yaml\n{config}\n```\nError: **{error}**.\n\nMake sure the configuration is valid and [reload](/developer-tools/yaml) the manually configured MQTT items or restart Home Assistant to fix this issue."
},
"payload_template_deprecation": {
"title": "Deprecated option used in mqtt publish action call",
"description": "Deprecated `payload_template` option used in MQTT publish action call to topic `{topic}` from payload template `{payload_template}`. Use the `payload` option instead. In automations templates are supported natively. Update the automation or script to use the `payload` option instead and restart Home Assistant to fix this issue."
},
"topic_template_deprecation": {
"title": "Deprecated option used in mqtt publish action call",
"description": "Deprecated `topic_template` option used in MQTT publish action call to topic `{topic}` from topic template `{topic_template}`. Use the `topic` option instead. In automations templates are supported natively. Update the automation or script to use the `topic` option instead and restart Home Assistant to fix this issue."
}
},
"config": {
Expand Down
Loading

0 comments on commit 5e78d7d

Please sign in to comment.