Await startup in homekit controller (#75021)
This commit is contained in:
parent
a3c1926da5
commit
a31dde9cb4
7 changed files with 133 additions and 30 deletions
|
@ -186,6 +186,13 @@ async def setup_platform(hass):
|
|||
async def setup_test_accessories(hass, accessories):
|
||||
"""Load a fake homekit device based on captured JSON profile."""
|
||||
fake_controller = await setup_platform(hass)
|
||||
return await setup_test_accessories_with_controller(
|
||||
hass, accessories, fake_controller
|
||||
)
|
||||
|
||||
|
||||
async def setup_test_accessories_with_controller(hass, accessories, fake_controller):
|
||||
"""Load a fake homekit device based on captured JSON profile."""
|
||||
|
||||
pairing_id = "00:00:00:00:00:00"
|
||||
|
||||
|
|
|
@ -515,7 +515,7 @@ 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_refresh_entity_map.await_count == 1
|
||||
assert connection_mock.async_refresh_entity_map_and_entities.await_count == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exception,expected", PAIRING_START_ABORT_ERRORS)
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
"""Tests for homekit_controller init."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from aiohomekit import exceptions
|
||||
from aiohomekit.model import Accessory
|
||||
from aiohomekit.model.characteristics import CharacteristicsTypes
|
||||
from aiohomekit.model.services import ServicesTypes
|
||||
from aiohomekit.testing import FakeController, FakeDiscovery, FakePairing
|
||||
|
||||
from homeassistant.components.homekit_controller.const import ENTITY_MAP
|
||||
from homeassistant.components.homekit_controller.const import DOMAIN, ENTITY_MAP
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .common import Helper, remove_device
|
||||
from .common import Helper, remove_device, setup_test_accessories_with_controller
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.homekit_controller.common import setup_test_component
|
||||
|
||||
ALIVE_DEVICE_NAME = "testdevice"
|
||||
|
@ -89,3 +96,76 @@ async def test_device_remove_devices(hass, hass_ws_client):
|
|||
await remove_device(await hass_ws_client(hass), dead_device_entry.id, entry_id)
|
||||
is True
|
||||
)
|
||||
|
||||
|
||||
async def test_offline_device_raises(hass):
|
||||
"""Test an offline device raises ConfigEntryNotReady."""
|
||||
|
||||
is_connected = False
|
||||
|
||||
class OfflineFakePairing(FakePairing):
|
||||
"""Fake pairing that always returns False for is_connected."""
|
||||
|
||||
@property
|
||||
def is_connected(self):
|
||||
nonlocal is_connected
|
||||
return is_connected
|
||||
|
||||
class OfflineFakeDiscovery(FakeDiscovery):
|
||||
"""Fake discovery that returns an offline pairing."""
|
||||
|
||||
async def start_pairing(self, alias: str):
|
||||
if self.description.id in self.controller.pairings:
|
||||
raise exceptions.AlreadyPairedError(
|
||||
f"{self.description.id} already paired"
|
||||
)
|
||||
|
||||
async def finish_pairing(pairing_code):
|
||||
if pairing_code != self.pairing_code:
|
||||
raise exceptions.AuthenticationError("M4")
|
||||
pairing_data = {}
|
||||
pairing_data["AccessoryIP"] = self.info["address"]
|
||||
pairing_data["AccessoryPort"] = self.info["port"]
|
||||
pairing_data["Connection"] = "IP"
|
||||
|
||||
obj = self.controller.pairings[alias] = OfflineFakePairing(
|
||||
self.controller, pairing_data, self.accessories
|
||||
)
|
||||
return obj
|
||||
|
||||
return finish_pairing
|
||||
|
||||
class OfflineFakeController(FakeController):
|
||||
"""Fake controller that always returns a discovery with a pairing that always returns False for is_connected."""
|
||||
|
||||
def add_device(self, accessories):
|
||||
device_id = "00:00:00:00:00:00"
|
||||
discovery = self.discoveries[device_id] = OfflineFakeDiscovery(
|
||||
self,
|
||||
device_id,
|
||||
accessories=accessories,
|
||||
)
|
||||
return discovery
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.homekit_controller.utils.Controller"
|
||||
) as controller:
|
||||
fake_controller = controller.return_value = OfflineFakeController()
|
||||
await async_setup_component(hass, DOMAIN, {})
|
||||
|
||||
accessory = Accessory.create_with_info(
|
||||
"TestDevice", "example.com", "Test", "0001", "0.1"
|
||||
)
|
||||
create_alive_service(accessory)
|
||||
|
||||
config_entry, _ = await setup_test_accessories_with_controller(
|
||||
hass, [accessory], fake_controller
|
||||
)
|
||||
|
||||
assert config_entry.state == ConfigEntryState.SETUP_RETRY
|
||||
|
||||
is_connected = True
|
||||
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.state == ConfigEntryState.LOADED
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue