Create MQTT discovery flow when manual config is present (#66248)
* Create MQTT discovery flow when manual config is present * Change to integration_discovery flow * Add test * Add default handler for integration_discovery
This commit is contained in:
parent
a644baf3cd
commit
335a918118
3 changed files with 47 additions and 12 deletions
|
@ -21,6 +21,7 @@ import certifi
|
|||
import jinja2
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
|
@ -585,6 +586,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
conf = dict(conf)
|
||||
hass.data[DATA_MQTT_CONFIG] = conf
|
||||
|
||||
if not bool(hass.config_entries.async_entries(DOMAIN)):
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
||||
data={},
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
@ -1393,12 +1393,24 @@ class ConfigFlow(data_entry_flow.FlowHandler):
|
|||
reason=reason, description_placeholders=description_placeholders
|
||||
)
|
||||
|
||||
async def async_step_dhcp(
|
||||
self, discovery_info: DhcpServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by DHCP discovery."""
|
||||
return await self.async_step_discovery(dataclasses.asdict(discovery_info))
|
||||
|
||||
async def async_step_hassio(
|
||||
self, discovery_info: HassioServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by HASS IO discovery."""
|
||||
return await self.async_step_discovery(discovery_info.config)
|
||||
|
||||
async def async_step_integration_discovery(
|
||||
self, discovery_info: DiscoveryInfoType
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by integration specific discovery."""
|
||||
return await self.async_step_discovery(discovery_info)
|
||||
|
||||
async def async_step_homekit(
|
||||
self, discovery_info: ZeroconfServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
|
@ -1417,24 +1429,18 @@ class ConfigFlow(data_entry_flow.FlowHandler):
|
|||
"""Handle a flow initialized by SSDP discovery."""
|
||||
return await self.async_step_discovery(dataclasses.asdict(discovery_info))
|
||||
|
||||
async def async_step_zeroconf(
|
||||
self, discovery_info: ZeroconfServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by Zeroconf discovery."""
|
||||
return await self.async_step_discovery(dataclasses.asdict(discovery_info))
|
||||
|
||||
async def async_step_dhcp(
|
||||
self, discovery_info: DhcpServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by DHCP discovery."""
|
||||
return await self.async_step_discovery(dataclasses.asdict(discovery_info))
|
||||
|
||||
async def async_step_usb(
|
||||
self, discovery_info: UsbServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by USB discovery."""
|
||||
return await self.async_step_discovery(dataclasses.asdict(discovery_info))
|
||||
|
||||
async def async_step_zeroconf(
|
||||
self, discovery_info: ZeroconfServiceInfo
|
||||
) -> data_entry_flow.FlowResult:
|
||||
"""Handle a flow initialized by Zeroconf discovery."""
|
||||
return await self.async_step_discovery(dataclasses.asdict(discovery_info))
|
||||
|
||||
@callback
|
||||
def async_create_entry( # pylint: disable=arguments-differ
|
||||
self,
|
||||
|
|
|
@ -79,6 +79,26 @@ async def test_user_connection_fails(hass, mock_try_connection, mock_finish_setu
|
|||
assert len(mock_finish_setup.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_manual_config_starts_discovery_flow(
|
||||
hass, mock_try_connection, mock_finish_setup, mqtt_client_mock
|
||||
):
|
||||
"""Test manual config initiates a discovery flow."""
|
||||
# No flows in progress
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
# MQTT config present in yaml config
|
||||
assert await async_setup_component(hass, "mqtt", {"mqtt": {}})
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_finish_setup.mock_calls) == 0
|
||||
|
||||
# There should now be a discovery flow
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["source"] == "integration_discovery"
|
||||
assert flows[0]["handler"] == "mqtt"
|
||||
assert flows[0]["step_id"] == "broker"
|
||||
|
||||
|
||||
async def test_manual_config_set(
|
||||
hass, mock_try_connection, mock_finish_setup, mqtt_client_mock
|
||||
):
|
||||
|
|
Loading…
Add table
Reference in a new issue