Skip to content

Commit

Permalink
Revert "Allow MQTT device based auto discovery" (home-assistant#118746)
Browse files Browse the repository at this point in the history
Revert "Allow MQTT device based auto discovery (home-assistant#109030)"

This reverts commit 585892f.
  • Loading branch information
jbouwh authored and dgomes committed Jun 4, 2024
1 parent 68a0866 commit e278ac6
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 1,106 deletions.
1 change: 0 additions & 1 deletion homeassistant/components/mqtt/abbreviations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"cmd_on_tpl": "command_on_template",
"cmd_t": "command_topic",
"cmd_tpl": "command_template",
"cmp": "components",
"cod_arm_req": "code_arm_required",
"cod_dis_req": "code_disarm_required",
"cod_form": "code_format",
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
CONF_CERTIFICATE = "certificate"
CONF_CLIENT_KEY = "client_key"
CONF_CLIENT_CERT = "client_cert"
CONF_COMPONENTS = "components"
CONF_TLS_INSECURE = "tls_insecure"

# Device and integration info options
Expand Down
360 changes: 97 additions & 263 deletions homeassistant/components/mqtt/discovery.py

Large diffs are not rendered by default.

35 changes: 0 additions & 35 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,6 @@ def __init__(
self._config_entry = config_entry
self._config_entry_id = config_entry.entry_id
self._skip_device_removal: bool = False
self._migrate_discovery: str | None = None

discovery_hash = get_discovery_hash(discovery_data)
self._remove_discovery_updated = async_dispatcher_connect(
Expand Down Expand Up @@ -721,24 +720,6 @@ async def async_discovery_update(
discovery_hash,
discovery_payload,
)
if not discovery_payload and self._migrate_discovery is not None:
# Ignore empty update from migrated and removed discovery config.
self._discovery_data[ATTR_DISCOVERY_TOPIC] = self._migrate_discovery
self._migrate_discovery = None
_LOGGER.info("Component successfully migrated: %s", discovery_hash)
send_discovery_done(self.hass, self._discovery_data)
return

if discovery_payload and (
(discovery_topic := discovery_payload.discovery_data[ATTR_DISCOVERY_TOPIC])
!= self._discovery_data[ATTR_DISCOVERY_TOPIC]
):
# Make sure the migrated discovery topic is removed.
self._migrate_discovery = discovery_topic
_LOGGER.debug("Migrating component: %s", discovery_hash)
self.hass.async_create_task(
async_remove_discovery_payload(self.hass, self._discovery_data)
)
if (
discovery_payload
and discovery_payload != self._discovery_data[ATTR_DISCOVERY_PAYLOAD]
Expand Down Expand Up @@ -835,7 +816,6 @@ def __init__(
mqtt_data = hass.data[DATA_MQTT]
self._registry_hooks = mqtt_data.discovery_registry_hooks
discovery_hash: tuple[str, str] = discovery_data[ATTR_DISCOVERY_HASH]
self._migrate_discovery: str | None = None
if discovery_hash in self._registry_hooks:
self._registry_hooks.pop(discovery_hash)()

Expand Down Expand Up @@ -918,27 +898,12 @@ def _async_discovery_callback(self, payload: MQTTDiscoveryPayload) -> None:
old_payload = self._discovery_data[ATTR_DISCOVERY_PAYLOAD]
debug_info.update_entity_discovery_data(self.hass, payload, self.entity_id)
if not payload:
if self._migrate_discovery is not None:
# Ignore empty update of the migrated and removed discovery config.
self._discovery_data[ATTR_DISCOVERY_TOPIC] = self._migrate_discovery
self._migrate_discovery = None
_LOGGER.info("Component successfully migrated: %s", self.entity_id)
send_discovery_done(self.hass, self._discovery_data)
return
# Empty payload: Remove component
_LOGGER.info("Removing component: %s", self.entity_id)
self.hass.async_create_task(
self._async_process_discovery_update_and_remove()
)
elif self._discovery_update:
discovery_topic = payload.discovery_data[ATTR_DISCOVERY_TOPIC]
if discovery_topic != self._discovery_data[ATTR_DISCOVERY_TOPIC]:
# Make sure the migrated discovery topic is removed.
self._migrate_discovery = discovery_topic
_LOGGER.debug("Migrating component: %s", self.entity_id)
self.hass.async_create_task(
async_remove_discovery_payload(self.hass, self._discovery_data)
)
if old_payload != payload:
# Non-empty, changed payload: Notify component
_LOGGER.info("Updating component: %s", self.entity_id)
Expand Down
10 changes: 0 additions & 10 deletions homeassistant/components/mqtt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,5 @@ class MqttData:
tags: dict[str, dict[str, MQTTTagScanner]] = field(default_factory=dict)


@dataclass(slots=True)
class MqttComponentConfig:
"""(component, object_id, node_id, discovery_payload)."""

component: str
object_id: str
node_id: str | None
discovery_payload: MQTTDiscoveryPayload


DATA_MQTT: HassKey[MqttData] = HassKey("mqtt")
DATA_MQTT_AVAILABLE: HassKey[asyncio.Future[bool]] = HassKey("mqtt_client_available")
51 changes: 1 addition & 50 deletions homeassistant/components/mqtt/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from __future__ import annotations

import logging

import voluptuous as vol

from homeassistant.const import (
Expand All @@ -12,7 +10,6 @@
CONF_ICON,
CONF_MODEL,
CONF_NAME,
CONF_PLATFORM,
CONF_UNIQUE_ID,
CONF_VALUE_TEMPLATE,
)
Expand All @@ -27,13 +24,10 @@
CONF_AVAILABILITY_MODE,
CONF_AVAILABILITY_TEMPLATE,
CONF_AVAILABILITY_TOPIC,
CONF_COMMAND_TOPIC,
CONF_COMPONENTS,
CONF_CONFIGURATION_URL,
CONF_CONNECTIONS,
CONF_DEPRECATED_VIA_HUB,
CONF_ENABLED_BY_DEFAULT,
CONF_ENCODING,
CONF_HW_VERSION,
CONF_IDENTIFIERS,
CONF_JSON_ATTRS_TEMPLATE,
Expand All @@ -43,43 +37,16 @@
CONF_ORIGIN,
CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE,
CONF_QOS,
CONF_SERIAL_NUMBER,
CONF_STATE_TOPIC,
CONF_SUGGESTED_AREA,
CONF_SUPPORT_URL,
CONF_SW_VERSION,
CONF_TOPIC,
CONF_VIA_DEVICE,
DEFAULT_PAYLOAD_AVAILABLE,
DEFAULT_PAYLOAD_NOT_AVAILABLE,
SUPPORTED_COMPONENTS,
)
from .util import valid_publish_topic, valid_qos_schema, valid_subscribe_topic

_LOGGER = logging.getLogger(__name__)

# Device discovery options that are also available at entity component level
SHARED_OPTIONS = [
CONF_AVAILABILITY,
CONF_AVAILABILITY_MODE,
CONF_AVAILABILITY_TEMPLATE,
CONF_AVAILABILITY_TOPIC,
CONF_COMMAND_TOPIC,
CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE,
CONF_STATE_TOPIC,
]

MQTT_ORIGIN_INFO_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_SW_VERSION): cv.string,
vol.Optional(CONF_SUPPORT_URL): cv.configuration_url,
}
),
)
from .util import valid_subscribe_topic

MQTT_AVAILABILITY_SINGLE_SCHEMA = vol.Schema(
{
Expand Down Expand Up @@ -181,19 +148,3 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType
vol.Optional(CONF_UNIQUE_ID): cv.string,
}
)

COMPONENT_CONFIG_SCHEMA = vol.Schema(
{vol.Required(CONF_PLATFORM): vol.In(SUPPORTED_COMPONENTS)}
).extend({}, extra=True)

DEVICE_DISCOVERY_SCHEMA = MQTT_AVAILABILITY_SCHEMA.extend(
{
vol.Required(CONF_DEVICE): MQTT_ENTITY_DEVICE_INFO_SCHEMA,
vol.Required(CONF_COMPONENTS): vol.Schema({str: COMPONENT_CONFIG_SCHEMA}),
vol.Required(CONF_ORIGIN): MQTT_ORIGIN_INFO_SCHEMA,
vol.Optional(CONF_STATE_TOPIC): valid_subscribe_topic,
vol.Optional(CONF_COMMAND_TOPIC): valid_publish_topic,
vol.Optional(CONF_QOS): valid_qos_schema,
vol.Optional(CONF_ENCODING): cv.string,
}
)
9 changes: 1 addition & 8 deletions tests/components/mqtt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections.abc import Generator
from random import getrandbits
from unittest.mock import AsyncMock, patch
from unittest.mock import patch

import pytest

Expand All @@ -29,10 +29,3 @@ def mock_temp_dir(temp_dir_prefix: str) -> Generator[None, None, str]:
f"home-assistant-mqtt-{temp_dir_prefix}-{getrandbits(10):03x}",
) as mocked_temp_dir:
yield mocked_temp_dir


@pytest.fixture
def tag_mock() -> Generator[AsyncMock, None, None]:
"""Fixture to mock tag."""
with patch("homeassistant.components.tag.async_scan_tag") as mock_tag:
yield mock_tag
38 changes: 9 additions & 29 deletions tests/components/mqtt/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,22 @@ def calls(hass: HomeAssistant) -> list[ServiceCall]:
return async_mock_service(hass, "test", "automation")


@pytest.mark.parametrize(
("discovery_topic", "data"),
[
(
"homeassistant/device_automation/0AFFD2/bla/config",
'{ "automation_type":"trigger",'
' "device":{"identifiers":["0AFFD2"]},'
' "payload": "short_press",'
' "topic": "foobar/triggers/button1",'
' "type": "button_short_press",'
' "subtype": "button_1" }',
),
(
"homeassistant/device/0AFFD2/config",
'{ "device":{"identifiers":["0AFFD2"]},'
' "o": {"name": "foobar"}, "cmp": '
'{ "bla": {'
' "automation_type":"trigger", '
' "payload": "short_press",'
' "topic": "foobar/triggers/button1",'
' "type": "button_short_press",'
' "subtype": "button_1",'
' "platform":"device_automation"}}}',
),
],
)
async def test_get_triggers(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mqtt_mock_entry: MqttMockHAClientGenerator,
discovery_topic: str,
data: str,
) -> None:
"""Test we get the expected triggers from a discovered mqtt device."""
await mqtt_mock_entry()
async_fire_mqtt_message(hass, discovery_topic, data)
data1 = (
'{ "automation_type":"trigger",'
' "device":{"identifiers":["0AFFD2"]},'
' "payload": "short_press",'
' "topic": "foobar/triggers/button1",'
' "type": "button_short_press",'
' "subtype": "button_1" }'
)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1)
await hass.async_block_till_done()

device_entry = device_registry.async_get_device(identifiers={("mqtt", "0AFFD2")})
Expand Down
Loading

0 comments on commit e278ac6

Please # to comment.