Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Support for MQTT discovery."""
import asyncio
from collections import deque
import datetime as dt
import functools
import json
import logging
import re
import time
from typing import TypedDict

from homeassistant.const import CONF_DEVICE, CONF_PLATFORM
from homeassistant.core import HomeAssistant
Expand All @@ -27,6 +29,7 @@
CONF_TOPIC,
DOMAIN,
)
from .models import ReceivePayloadType

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -86,6 +89,17 @@ class MQTTConfig(dict):
"""Dummy class to allow adding attributes."""


class MqttServiceInfo(TypedDict):
"""Prepared info from mqtt entries."""

topic: str
payload: ReceivePayloadType
qos: int
retain: bool
subscribed_topic: str
timestamp: dt.datetime


async def async_start( # noqa: C901
hass: HomeAssistant, discovery_topic, config_entry=None
) -> None:
Expand Down Expand Up @@ -288,14 +302,14 @@ async def async_integration_message_received(integration, msg):
if key not in hass.data[INTEGRATION_UNSUBSCRIBE]:
return

data = {
"topic": msg.topic,
"payload": msg.payload,
"qos": msg.qos,
"retain": msg.retain,
"subscribed_topic": msg.subscribed_topic,
"timestamp": msg.timestamp,
}
data = MqttServiceInfo(
topic=msg.topic,
payload=msg.payload,
qos=msg.qos,
retain=msg.retain,
subscribed_topic=msg.subscribed_topic,
timestamp=msg.timestamp,
)
result = await hass.config_entries.flow.async_init(
integration, context={"source": DOMAIN}, data=data
)
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/tasmota/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.mqtt import valid_subscribe_topic
from homeassistant.components.mqtt import discovery as mqtt, valid_subscribe_topic
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import CONF_DISCOVERY_PREFIX, DEFAULT_PREFIX, DOMAIN

Expand All @@ -22,7 +21,7 @@ def __init__(self) -> None:
"""Initialize flow."""
self._prefix = DEFAULT_PREFIX

async def async_step_mqtt(self, discovery_info: DiscoveryInfoType) -> FlowResult:
async def async_step_mqtt(self, discovery_info: mqtt.MqttServiceInfo) -> FlowResult:
"""Handle a flow initialized by MQTT discovery."""
if self._async_in_progress() or self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

if TYPE_CHECKING:
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.mqtt.discovery import MqttServiceInfo
from homeassistant.components.zeroconf import ZeroconfServiceInfo

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -1361,10 +1362,10 @@ async def async_step_homekit(
return await self.async_step_discovery(cast(dict, discovery_info))

async def async_step_mqtt(
self, discovery_info: DiscoveryInfoType
self, discovery_info: MqttServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle a flow initialized by MQTT discovery."""
return await self.async_step_discovery(discovery_info)
return await self.async_step_discovery(cast(dict, discovery_info))

async def async_step_ssdp(
self, discovery_info: DiscoveryInfoType
Expand Down
11 changes: 10 additions & 1 deletion homeassistant/helpers/config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from homeassistant import config_entries
from homeassistant.components import dhcp, zeroconf
from homeassistant.components.mqtt import discovery as mqtt
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import UNDEFINED, DiscoveryInfoType, UndefinedType
Expand Down Expand Up @@ -102,6 +103,15 @@ async def async_step_homekit(

return await self.async_step_confirm()

async def async_step_mqtt(self, discovery_info: mqtt.MqttServiceInfo) -> FlowResult:
"""Handle a flow initialized by mqtt discovery."""
if self._async_in_progress() or self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

await self.async_set_unique_id(self._domain)

return await self.async_step_confirm()

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
Expand All @@ -114,7 +124,6 @@ async def async_step_zeroconf(
return await self.async_step_confirm()

async_step_ssdp = async_step_discovery
async_step_mqtt = async_step_discovery

async def async_step_import(self, _: dict[str, Any] | None) -> FlowResult:
"""Handle a flow initialized by import."""
Expand Down