Dont rely on config flow to monitor homekit_controller c# changes (#76861)

This commit is contained in:
Jc2k 2022-08-20 21:58:59 +01:00 committed by GitHub
parent 8b1713a691
commit eb0828efdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 83 deletions

View file

@ -11,10 +11,9 @@ from unittest import mock
from aiohomekit.model import Accessories, AccessoriesState, Accessory
from aiohomekit.testing import FakeController, FakePairing
from aiohomekit.zeroconf import HomeKitService
from homeassistant.components import zeroconf
from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.homekit_controller import config_flow
from homeassistant.components.homekit_controller.const import (
CONTROLLER,
DOMAIN,
@ -22,6 +21,7 @@ from homeassistant.components.homekit_controller.const import (
IDENTIFIER_ACCESSORY_ID,
IDENTIFIER_SERIAL_NUMBER,
)
from homeassistant.components.homekit_controller.utils import async_get_controller
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, State, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
@ -175,12 +175,11 @@ async def setup_platform(hass):
config = {"discovery": {}}
with mock.patch(
"homeassistant.components.homekit_controller.utils.Controller"
) as controller:
fake_controller = controller.return_value = FakeController()
"homeassistant.components.homekit_controller.utils.Controller", FakeController
):
await async_setup_component(hass, DOMAIN, config)
return fake_controller
return await async_get_controller(hass)
async def setup_test_accessories(hass, accessories):
@ -228,31 +227,24 @@ async def device_config_changed(hass, accessories):
pairing._accessories_state = AccessoriesState(
accessories_obj, pairing.config_num + 1
)
discovery_info = zeroconf.ZeroconfServiceInfo(
host="127.0.0.1",
addresses=["127.0.0.1"],
hostname="mock_hostname",
name="TestDevice._hap._tcp.local.",
port=8080,
properties={
"md": "TestDevice",
"id": "00:00:00:00:00:00",
"c#": "2",
"sf": "0",
},
type="mock_type",
pairing._async_description_update(
HomeKitService(
name="TestDevice.local",
id="00:00:00:00:00:00",
model="",
config_num=2,
state_num=3,
feature_flags=0,
status_flags=0,
category=1,
protocol_version="1.0",
type="_hap._tcp.local.",
address="127.0.0.1",
addresses=["127.0.0.1"],
port=8080,
)
)
# Config Flow will abort and notify us if the discovery event is of
# interest - in this case c# has incremented
flow = config_flow.HomekitControllerFlowHandler()
flow.hass = hass
flow.context = {}
result = await flow.async_step_zeroconf(discovery_info)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
# Wait for services to reconfigure
await hass.async_block_till_done()
await hass.async_block_till_done()

View file

@ -6,7 +6,7 @@ https://github.com/home-assistant/core/issues/15336
from unittest import mock
from aiohomekit import AccessoryDisconnectedError
from aiohomekit import AccessoryNotFoundError
from aiohomekit.testing import FakePairing
from homeassistant.components.climate.const import (
@ -184,9 +184,8 @@ async def test_ecobee3_setup_connection_failure(hass):
# Test that the connection fails during initial setup.
# No entities should be created.
list_accessories = "list_accessories_and_characteristics"
with mock.patch.object(FakePairing, list_accessories) as laac:
laac.side_effect = AccessoryDisconnectedError("Connection failed")
with mock.patch.object(FakePairing, "async_populate_accessories_state") as laac:
laac.side_effect = AccessoryNotFoundError("Connection failed")
# If there is no cached entity map and the accessory connection is
# failing then we have to fail the config entry setup.

View file

@ -1,7 +1,7 @@
"""Tests for homekit_controller config flow."""
import asyncio
import unittest.mock
from unittest.mock import AsyncMock, MagicMock, patch
from unittest.mock import AsyncMock, patch
import aiohomekit
from aiohomekit.exceptions import AuthenticationError
@ -524,7 +524,6 @@ async def test_discovery_already_configured_update_csharp(hass, controller):
entry.add_to_hass(hass)
connection_mock = AsyncMock()
connection_mock.async_notify_config_changed = MagicMock()
hass.data[KNOWN_DEVICES] = {"AA:BB:CC:DD:EE:FF": connection_mock}
device = setup_mock_accessory(controller)
@ -547,7 +546,6 @@ async def test_discovery_already_configured_update_csharp(hass, controller):
assert entry.data["AccessoryIP"] == discovery_info.host
assert entry.data["AccessoryPort"] == discovery_info.port
assert connection_mock.async_notify_config_changed.call_count == 1
@pytest.mark.parametrize("exception,expected", PAIRING_START_ABORT_ERRORS)

View file

@ -119,6 +119,7 @@ async def test_offline_device_raises(hass, controller):
nonlocal is_connected
if not is_connected:
raise AccessoryNotFoundError("any")
await super().async_populate_accessories_state(*args, **kwargs)
async def get_characteristics(self, chars, *args, **kwargs):
nonlocal is_connected
@ -173,6 +174,7 @@ async def test_ble_device_only_checks_is_available(hass, controller):
nonlocal is_available
if not is_available:
raise AccessoryNotFoundError("any")
await super().async_populate_accessories_state(*args, **kwargs)
async def get_characteristics(self, chars, *args, **kwargs):
nonlocal is_available