mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Skip the WattWächter discovery for devices set up through MQTT (#189372)
This commit is contained in:
@@ -12,7 +12,7 @@ from aio_wattwaechter import (
|
||||
from aio_wattwaechter.models import SystemInfo
|
||||
import probatio
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.config_entries import SOURCE_ZEROCONF, ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE_ID,
|
||||
CONF_HOST,
|
||||
@@ -20,6 +20,9 @@ from homeassistant.const import (
|
||||
CONF_MODEL,
|
||||
CONF_TOKEN,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.data_entry_flow import AbortFlow
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.selector import (
|
||||
TextSelector,
|
||||
@@ -28,7 +31,14 @@ from homeassistant.helpers.selector import (
|
||||
)
|
||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||
|
||||
from .const import CONF_FW_VERSION, DOMAIN, LEGACY_MODEL_IDS, MODEL_ID_PLUS, MODEL_NAMES
|
||||
from .const import (
|
||||
CONF_FW_VERSION,
|
||||
DOMAIN,
|
||||
LEGACY_MODEL_IDS,
|
||||
MODEL_ID_PLUS,
|
||||
MODEL_NAMES,
|
||||
MQTT_DOMAIN,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -61,6 +71,7 @@ class WattwaechterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self._mac: str = ""
|
||||
self._device_name: str | None = None
|
||||
self._product_name: str | None = None
|
||||
self._mqtt_client_id: str | None = None
|
||||
|
||||
async def _async_test_connection(
|
||||
self, host: str, token: str | None = None
|
||||
@@ -80,8 +91,26 @@ class WattwaechterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
device_name = None
|
||||
else:
|
||||
device_name = settings.device_name
|
||||
self._mqtt_client_id = settings.mqtt.client_id
|
||||
return {}, system_info, device_name
|
||||
|
||||
@callback
|
||||
def _abort_if_set_up_through_mqtt(self) -> None:
|
||||
"""Abort a discovery of a device that is already set up through MQTT."""
|
||||
if self.source != SOURCE_ZEROCONF or not self._mqtt_client_id:
|
||||
return
|
||||
device_registry = dr.async_get(self.hass)
|
||||
if any(
|
||||
device_registry.async_get_devices(
|
||||
identifiers={(MQTT_DOMAIN, self._mqtt_client_id)},
|
||||
config_entry_id=entry.entry_id,
|
||||
)
|
||||
for entry in self.hass.config_entries.async_entries(
|
||||
MQTT_DOMAIN, include_ignore=False, include_disabled=False
|
||||
)
|
||||
):
|
||||
raise AbortFlow("already_configured_mqtt")
|
||||
|
||||
def _apply_system_info(self, system_info: SystemInfo) -> None:
|
||||
"""Take over the device details reported by the system info."""
|
||||
if not self._device_id:
|
||||
@@ -155,6 +184,7 @@ class WattwaechterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
assert system_info is not None
|
||||
self._apply_system_info(system_info)
|
||||
self._device_name = device_name
|
||||
self._abort_if_set_up_through_mqtt()
|
||||
self.context["title_placeholders"] = {
|
||||
"name": self._device_name or f"WattWächter {self._device_id[-4:]}"
|
||||
}
|
||||
@@ -175,6 +205,7 @@ class WattwaechterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
},
|
||||
)
|
||||
|
||||
self._abort_if_set_up_through_mqtt()
|
||||
return self._create_entry()
|
||||
|
||||
@override
|
||||
@@ -237,6 +268,7 @@ class WattwaechterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self._device_id, raise_on_progress=False
|
||||
)
|
||||
self._abort_if_unique_id_configured(updates={CONF_HOST: self._host})
|
||||
self._abort_if_set_up_through_mqtt()
|
||||
|
||||
return self._create_entry(token)
|
||||
|
||||
|
||||
@@ -30,3 +30,7 @@ LEGACY_MODEL_IDS: dict[str, str] = {
|
||||
# factor (calorific value times the gas correction factor). Reported for
|
||||
# gas only.
|
||||
OBIS_GAS_POWER = "7-0:16.7.0"
|
||||
|
||||
# The device's MQTT discovery uses its MQTT client ID as the device identifier
|
||||
# of the MQTT integration.
|
||||
MQTT_DOMAIN = "mqtt"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||
"already_configured_mqtt": "This device is already set up in Home Assistant through MQTT. To set it up with this integration as well, add it manually.",
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"wrong_device": "The device does not match the original WattWächter device."
|
||||
},
|
||||
|
||||
@@ -40,7 +40,10 @@ MOCK_CONFIG_DATA = {
|
||||
CONF_MAC: MOCK_MAC,
|
||||
}
|
||||
|
||||
MOCK_MQTT_CLIENT_ID = f"wattwaechter-{MOCK_DEVICE_ID.lower()}"
|
||||
|
||||
MOCK_SETTINGS = MagicMock(device_name=MOCK_DEVICE_NAME)
|
||||
MOCK_SETTINGS.mqtt.client_id = MOCK_MQTT_CLIENT_ID
|
||||
|
||||
MOCK_ALIVE_RESPONSE = AliveResponse(alive=True, version=MOCK_FW_VERSION)
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||
|
||||
from .conftest import (
|
||||
@@ -39,6 +40,7 @@ from .conftest import (
|
||||
MOCK_HOST,
|
||||
MOCK_MAC,
|
||||
MOCK_MODEL,
|
||||
MOCK_MQTT_CLIENT_ID,
|
||||
MOCK_SYSTEM_INFO_GAS,
|
||||
MOCK_TOKEN,
|
||||
)
|
||||
@@ -757,3 +759,154 @@ async def test_user_flow_legacy_firmware_without_model_id(
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == MOCK_DEVICE_ID
|
||||
assert result["data"][CONF_MODEL] == "ww_plus"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mqtt_device(hass: HomeAssistant, device_registry: dr.DeviceRegistry) -> None:
|
||||
"""Register the device as set up through MQTT discovery."""
|
||||
mqtt_entry = MockConfigEntry(domain="mqtt")
|
||||
mqtt_entry.add_to_hass(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=mqtt_entry.entry_id,
|
||||
identifiers={("mqtt", MOCK_MQTT_CLIENT_ID)},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_client", "mock_setup_entry", "mqtt_device")
|
||||
async def test_user_flow_set_up_through_mqtt(hass: HomeAssistant) -> None:
|
||||
"""Test the user flow also sets up a device set up through MQTT."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: MOCK_HOST}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == MOCK_DEVICE_ID
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry", "mqtt_device")
|
||||
async def test_user_flow_auth_step_set_up_through_mqtt(
|
||||
hass: HomeAssistant, mock_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test the user flow with a token also sets up a device set up through MQTT."""
|
||||
mock_client.system_info.side_effect = WattwaechterAuthenticationError(
|
||||
"Auth required"
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: MOCK_HOST}
|
||||
)
|
||||
assert result["step_id"] == "auth"
|
||||
|
||||
mock_client.system_info.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_TOKEN: MOCK_TOKEN}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["result"].unique_id == MOCK_DEVICE_ID
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_client", "mqtt_device")
|
||||
async def test_zeroconf_flow_set_up_through_mqtt(hass: HomeAssistant) -> None:
|
||||
"""Test discovery of a device set up through MQTT is not offered."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=MOCK_ZEROCONF_DISCOVERY,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured_mqtt"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mqtt_device")
|
||||
async def test_zeroconf_auth_step_set_up_through_mqtt(
|
||||
hass: HomeAssistant, mock_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test discovery of a device needing a token aborts once MQTT is known."""
|
||||
mock_client.system_info.side_effect = WattwaechterAuthenticationError(
|
||||
"Auth required"
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=MOCK_ZEROCONF_DISCOVERY,
|
||||
)
|
||||
assert result["step_id"] == "auth"
|
||||
|
||||
# The MQTT client ID can only be read with the token
|
||||
mock_client.system_info.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_TOKEN: MOCK_TOKEN}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured_mqtt"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config_entry_domain", "client_id", "disabled_by"),
|
||||
[
|
||||
pytest.param("mqtt", "wattwaechter-other", None, id="other_client_id"),
|
||||
pytest.param("other", MOCK_MQTT_CLIENT_ID, None, id="not_an_mqtt_device"),
|
||||
pytest.param(
|
||||
"mqtt",
|
||||
MOCK_MQTT_CLIENT_ID,
|
||||
ConfigEntryDisabler.USER,
|
||||
id="mqtt_disabled",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_client")
|
||||
async def test_zeroconf_flow_other_mqtt_device(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
config_entry_domain: str,
|
||||
client_id: str,
|
||||
disabled_by: ConfigEntryDisabler | None,
|
||||
) -> None:
|
||||
"""Test only a device of an enabled MQTT entry skips the discovery."""
|
||||
MockConfigEntry(domain="mqtt").add_to_hass(hass)
|
||||
owner_entry = MockConfigEntry(domain=config_entry_domain, disabled_by=disabled_by)
|
||||
owner_entry.add_to_hass(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=owner_entry.entry_id, identifiers={("mqtt", client_id)}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=MOCK_ZEROCONF_DISCOVERY,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_client")
|
||||
async def test_zeroconf_confirm_set_up_through_mqtt_meanwhile(
|
||||
hass: HomeAssistant, device_registry: dr.DeviceRegistry
|
||||
) -> None:
|
||||
"""Test the confirmation aborts if MQTT set up the device in the meantime."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=MOCK_ZEROCONF_DISCOVERY,
|
||||
)
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
|
||||
mqtt_entry = MockConfigEntry(domain="mqtt")
|
||||
mqtt_entry.add_to_hass(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=mqtt_entry.entry_id,
|
||||
identifiers={("mqtt", MOCK_MQTT_CLIENT_ID)},
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured_mqtt"
|
||||
|
||||
Reference in New Issue
Block a user