mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Skip disabled Mitsubishi Comfort devices in setup and address repair (#195853)
Co-authored-by: Nikolai Rahimi <nikolairahimi@users.noreply.github.com>
This commit is contained in:
co-authored by
Nikolai Rahimi
parent
8c163ad0e1
commit
2121c88cf8
@@ -14,9 +14,13 @@ from mitsubishi_comfort.exceptions import AuthenticationError, DeviceConnectionE
|
||||
|
||||
from homeassistant.components.dhcp import async_discovered_service_info
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
|
||||
from homeassistant.helpers import device_registry as dr, issue_registry as ir
|
||||
from homeassistant.helpers import (
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
issue_registry as ir,
|
||||
)
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import (
|
||||
@@ -99,8 +103,9 @@ async def async_setup_entry(
|
||||
# tracks it; DHCP discovery then supplies the IP via async_step_dhcp.
|
||||
device_registry = dr.async_get(hass)
|
||||
owned_macs = {dr.format_mac(info.mac) for info in devices.values() if info.mac}
|
||||
disabled: list[str] = []
|
||||
for serial, info in devices.items():
|
||||
device_registry.async_get_or_create(
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, serial)},
|
||||
# Connections are globally indexed: registering an empty MAC would
|
||||
@@ -112,6 +117,10 @@ async def async_setup_entry(
|
||||
name=info.label,
|
||||
serial_number=serial,
|
||||
)
|
||||
# An account can span several sites; the user disables units that are
|
||||
# not on this network so they are neither polled nor reported missing.
|
||||
if device_entry.disabled:
|
||||
disabled.append(serial)
|
||||
|
||||
# Cache the freshly discovered credentials (password, cryptoSerial, MAC) so
|
||||
# later setups replay them; also drops entries for devices no longer present.
|
||||
@@ -151,6 +160,8 @@ async def async_setup_entry(
|
||||
no_address: list[str] = []
|
||||
incomplete: list[str] = []
|
||||
for serial, info in devices.items():
|
||||
if serial in disabled:
|
||||
continue
|
||||
if not is_fully_credentialed(info):
|
||||
incomplete.append(info.label)
|
||||
continue
|
||||
@@ -178,14 +189,16 @@ async def async_setup_entry(
|
||||
async_create_missing_address_issue(hass, entry.entry_id)
|
||||
else:
|
||||
ir.async_delete_issue(hass, DOMAIN, f"missing_address_{entry.entry_id}")
|
||||
# The three buckets reconcile: set up + awaiting address + incomplete local
|
||||
# data equals the number of devices on the account.
|
||||
# The four buckets reconcile: set up + awaiting address + incomplete local
|
||||
# data + disabled equals the number of devices on the account.
|
||||
_LOGGER.debug(
|
||||
"Set up %d of %d device(s); %d awaiting a LAN address, %d with incomplete local data",
|
||||
"Set up %d of %d device(s); %d awaiting a LAN address, "
|
||||
"%d with incomplete local data, %d disabled",
|
||||
len(coordinators),
|
||||
len(devices),
|
||||
len(no_address),
|
||||
len(incomplete),
|
||||
len(disabled),
|
||||
)
|
||||
|
||||
results = await asyncio.gather(
|
||||
@@ -209,6 +222,37 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
entry.runtime_data = coordinators
|
||||
|
||||
@callback
|
||||
def _async_device_updated(
|
||||
event: Event[dr.EventDeviceRegistryUpdatedData],
|
||||
) -> None:
|
||||
"""Reload when the user disables or enables a device without entities.
|
||||
|
||||
Devices with entities are reloaded by the entity registry's own
|
||||
disabled handler; addressless or disabled devices have none, so
|
||||
toggling them would otherwise leave the repair issue and setup stale.
|
||||
"""
|
||||
if (
|
||||
event.data["action"] != "update"
|
||||
or "disabled_by" not in event.data["changes"]
|
||||
or (device := device_registry.async_get(event.data["device_id"])) is None
|
||||
or device.config_entry_id != entry.entry_id
|
||||
# Disabling the config entry also disables its devices; only a
|
||||
# user toggling a device should reload.
|
||||
or {event.data["changes"]["disabled_by"], device.disabled_by}
|
||||
- {None, dr.DeviceEntryDisabler.USER}
|
||||
):
|
||||
return
|
||||
if er.async_entries_for_device(
|
||||
er.async_get(hass), device.id, include_disabled_entities=True
|
||||
):
|
||||
return
|
||||
hass.config_entries.async_schedule_reload(entry.entry_id)
|
||||
|
||||
entry.async_on_unload(
|
||||
hass.bus.async_listen(dr.EVENT_DEVICE_REGISTRY_UPDATED, _async_device_updated)
|
||||
)
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
|
||||
|
||||
@@ -24,6 +24,21 @@ def has_full_credentials(cred: dict[str, str]) -> bool:
|
||||
return bool(cred["password"] and cred["crypto_serial"] and cred["mac"])
|
||||
|
||||
|
||||
def async_disabled_serials(hass: HomeAssistant, entry_id: str) -> set[str]:
|
||||
"""Return the serials of the entry's devices the user has disabled.
|
||||
|
||||
An account can span several sites; disabling a unit that is not on this
|
||||
network excludes it from setup and from the missing-address repair.
|
||||
"""
|
||||
return {
|
||||
identifier
|
||||
for device in dr.async_entries_for_config_entry(dr.async_get(hass), entry_id)
|
||||
if device.disabled
|
||||
for domain, identifier in device.identifiers
|
||||
if domain == DOMAIN
|
||||
}
|
||||
|
||||
|
||||
def async_create_missing_address_issue(hass: HomeAssistant, entry_id: str) -> None:
|
||||
"""Raise the fixable repair offering manual entry of missing LAN addresses."""
|
||||
ir.async_create_issue(
|
||||
@@ -50,10 +65,11 @@ def async_reconcile_missing_address_issue(
|
||||
"""
|
||||
addresses: dict[str, str] = entry.data.get(CONF_ADDRESSES, {})
|
||||
credentials: dict[str, dict[str, str]] = entry.data.get(CONF_CREDENTIALS, {})
|
||||
disabled = async_disabled_serials(hass, entry.entry_id)
|
||||
if any(
|
||||
dr.format_mac(cred["mac"]) not in addresses
|
||||
for cred in credentials.values()
|
||||
if has_full_credentials(cred)
|
||||
for serial, cred in credentials.items()
|
||||
if has_full_credentials(cred) and serial not in disabled
|
||||
):
|
||||
async_create_missing_address_issue(hass, entry.entry_id)
|
||||
else:
|
||||
|
||||
@@ -19,7 +19,11 @@ from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .const import CONF_ADDRESSES, CONF_CREDENTIALS
|
||||
from .coordinator import MitsubishiComfortConfigEntry
|
||||
from .helpers import async_reconcile_missing_address_issue, has_full_credentials
|
||||
from .helpers import (
|
||||
async_disabled_serials,
|
||||
async_reconcile_missing_address_issue,
|
||||
has_full_credentials,
|
||||
)
|
||||
|
||||
|
||||
async def _async_reload_entry(
|
||||
@@ -98,11 +102,14 @@ class MissingAddressRepairFlow(RepairsFlow):
|
||||
# may be empty (no discovery succeeded yet) or hold removed devices.
|
||||
# Only fully-credentialed devices (secrets plus MAC): a partial record
|
||||
# cannot pass the authenticated probe, and setup counts its device as
|
||||
# incomplete rather than addressless.
|
||||
# incomplete rather than addressless. Disabled devices are skipped:
|
||||
# they may sit at another site and never be reachable from here.
|
||||
disabled = async_disabled_serials(self.hass, self.entry.entry_id)
|
||||
macs: dict[str, str] = {
|
||||
formatted: serial
|
||||
for serial, cred in credentials.items()
|
||||
if has_full_credentials(cred)
|
||||
and serial not in disabled
|
||||
and (formatted := dr.format_mac(cred["mac"])) not in stored
|
||||
}
|
||||
# The registry supplies friendly names; a device never registered
|
||||
|
||||
@@ -18,7 +18,7 @@ from homeassistant.components.mitsubishi_comfort.const import (
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import (
|
||||
@@ -769,3 +769,192 @@ async def test_reload_keeps_ignored_missing_address_issue(
|
||||
issue = issue_registry.async_get_issue(DOMAIN, issue_id)
|
||||
assert issue
|
||||
assert issue.dismissed_version is not None
|
||||
|
||||
|
||||
REMOTE_SERIAL = "REMOTE001"
|
||||
REMOTE_MAC = "11:22:33:44:55:66"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_remote_device(
|
||||
mock_cloud_account: AsyncMock, mock_device_info: DeviceInfo
|
||||
) -> DeviceInfo:
|
||||
"""Discover a second unit at another site, which DHCP never sees."""
|
||||
remote = replace(
|
||||
mock_device_info, serial=REMOTE_SERIAL, label="Cabin", mac=REMOTE_MAC
|
||||
)
|
||||
mock_cloud_account.discover_devices.return_value[REMOTE_SERIAL] = remote
|
||||
return remote
|
||||
|
||||
|
||||
def _register_device(
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entry: MockConfigEntry,
|
||||
serial: str,
|
||||
disabled_by: dr.DeviceEntryDisabler | None,
|
||||
) -> dr.DeviceEntry:
|
||||
"""Register a device for the entry, as an earlier setup would have."""
|
||||
return device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, serial)},
|
||||
disabled_by=disabled_by,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_integration", "mock_remote_device")
|
||||
async def test_setup_entry_skips_disabled_devices(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test a disabled addressless device is neither set up nor reported.
|
||||
|
||||
An account can hold units at several sites; units at another site never
|
||||
get a LAN address here, so disabling them must clear the repair issue.
|
||||
"""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
_register_device(
|
||||
device_registry, mock_config_entry, REMOTE_SERIAL, dr.DeviceEntryDisabler.USER
|
||||
)
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
assert mock_config_entry.runtime_data.keys() == {MOCK_SERIAL}
|
||||
assert not issue_registry.async_get_issue(
|
||||
DOMAIN, f"missing_address_{mock_config_entry.entry_id}"
|
||||
)
|
||||
remote = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, REMOTE_SERIAL), mock_config_entry.entry_id
|
||||
)
|
||||
assert remote
|
||||
assert remote.disabled_by is dr.DeviceEntryDisabler.USER
|
||||
# Credentials stay cached so re-enabling the device needs no extra fetch.
|
||||
assert REMOTE_SERIAL in mock_config_entry.data[CONF_CREDENTIALS]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_integration", "mock_remote_device")
|
||||
async def test_toggling_addressless_device_reloads(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
mock_cloud_account: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test disabling and re-enabling an entity-less device reloads the entry.
|
||||
|
||||
The entity registry's disabled handler only reloads for devices that have
|
||||
entities; an addressless device has none.
|
||||
"""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
issue_id = f"missing_address_{mock_config_entry.entry_id}"
|
||||
assert issue_registry.async_get_issue(DOMAIN, issue_id)
|
||||
assert mock_cloud_account.discover_devices.call_count == 1
|
||||
|
||||
remote = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, REMOTE_SERIAL), mock_config_entry.entry_id
|
||||
)
|
||||
assert remote
|
||||
device_registry.async_update_device(
|
||||
remote.id, disabled_by=dr.DeviceEntryDisabler.USER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
assert mock_cloud_account.discover_devices.call_count == 2
|
||||
assert not issue_registry.async_get_issue(DOMAIN, issue_id)
|
||||
|
||||
device_registry.async_update_device(remote.id, disabled_by=None)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_cloud_account.discover_devices.call_count == 3
|
||||
assert issue_registry.async_get_issue(DOMAIN, issue_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_integration")
|
||||
async def test_disabling_device_with_entities_does_not_reload(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_cloud_account: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the entity registry, not this listener, handles devices with entities."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, MOCK_SERIAL), mock_config_entry.entry_id
|
||||
)
|
||||
assert device
|
||||
device_registry.async_update_device(
|
||||
device.id, disabled_by=dr.DeviceEntryDisabler.USER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_cloud_account.discover_devices.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_integration", "mock_remote_device")
|
||||
async def test_disabling_entry_does_not_reload_via_devices(
|
||||
hass: HomeAssistant,
|
||||
mock_cloud_account: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test disabling the config entry, which disables its devices, stays unloaded."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.config_entries.async_set_disabled_by(
|
||||
mock_config_entry.entry_id, ConfigEntryDisabler.USER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert mock_cloud_account.discover_devices.call_count == 1
|
||||
|
||||
|
||||
async def test_setup_retry_ignores_disabled_devices_for_issue(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
mock_cloud_account: AsyncMock,
|
||||
) -> None:
|
||||
"""Test a cloud-down setup does not report a disabled device as addressless."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_USERNAME: MOCK_USERNAME,
|
||||
CONF_PASSWORD: MOCK_PASSWORD,
|
||||
CONF_ADDRESSES: {dr.format_mac(MOCK_MAC): MOCK_ADDRESS},
|
||||
CONF_CREDENTIALS: {
|
||||
MOCK_SERIAL: {
|
||||
"password": "dGVzdHBhc3M=",
|
||||
"crypto_serial": "0102030405060708090a",
|
||||
"mac": MOCK_MAC,
|
||||
},
|
||||
REMOTE_SERIAL: {
|
||||
"password": "dGVzdHBhc3M=",
|
||||
"crypto_serial": "0102030405060708090a",
|
||||
"mac": REMOTE_MAC,
|
||||
},
|
||||
},
|
||||
},
|
||||
unique_id="user-12345",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
_register_device(device_registry, entry, REMOTE_SERIAL, dr.DeviceEntryDisabler.USER)
|
||||
mock_cloud_account.login.side_effect = DeviceConnectionError("cloud down")
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||
assert not issue_registry.async_get_issue(
|
||||
DOMAIN, f"missing_address_{entry.entry_id}"
|
||||
)
|
||||
|
||||
@@ -656,3 +656,42 @@ async def test_fix_flow_without_entry_falls_back_to_confirm(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not issue_registry.async_get_issue(DOMAIN, "missing_address_gone")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ignore_missing_translations", [IGNORE_FORM_TRANSLATIONS])
|
||||
async def test_fix_flow_omits_disabled_devices(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_setup_integration: tuple[AsyncMock, MagicMock],
|
||||
mock_device_info: DeviceInfo,
|
||||
) -> None:
|
||||
"""Test the form omits disabled devices, such as units at another site."""
|
||||
mock_account, _ = mock_setup_integration
|
||||
mock_account.discover_devices.return_value = {
|
||||
MOCK_SERIAL: mock_device_info,
|
||||
"SERIAL002": _second_device_info(),
|
||||
}
|
||||
assert await async_setup_component(hass, "repairs", {})
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_USERNAME: MOCK_USERNAME, CONF_PASSWORD: MOCK_PASSWORD},
|
||||
unique_id="user-12345",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, "SERIAL002")},
|
||||
disabled_by=dr.DeviceEntryDisabler.USER,
|
||||
)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
client = await hass_client()
|
||||
data = await start_repair_fix_flow(
|
||||
client, DOMAIN, f"missing_address_{entry.entry_id}"
|
||||
)
|
||||
|
||||
mac = dr.format_mac(MOCK_MAC)
|
||||
assert [field["name"] for field in data["data_schema"]] == [mac]
|
||||
assert data["description_placeholders"]["devices"] == f"Living Room ({mac})"
|
||||
|
||||
Reference in New Issue
Block a user