Refactor homekit_controller config flow tests (#32141)

* Config flow test refactor

* Add a service and characteristic to the accessory so its more realistic

* Feedback from review

* Missing apostrophe
This commit is contained in:
Jc2k 2020-02-25 11:06:35 +00:00 committed by GitHub
parent ba4cc373c8
commit c9d78aa78c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 296 additions and 634 deletions

View file

@ -5,7 +5,6 @@ import os
import re
import aiohomekit
from aiohomekit import Controller
from aiohomekit.controller.ip import IpPairing
import voluptuous as vol
@ -59,7 +58,7 @@ def normalize_hkid(hkid):
def find_existing_host(hass, serial):
"""Return a set of the configured hosts."""
for entry in hass.config_entries.async_entries(DOMAIN):
if entry.data["AccessoryPairingID"] == serial:
if entry.data.get("AccessoryPairingID") == serial:
return entry
@ -89,7 +88,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
self.model = None
self.hkid = None
self.devices = {}
self.controller = Controller()
self.controller = aiohomekit.Controller()
self.finish_pairing = None
async def async_step_user(self, user_input=None):
@ -203,6 +202,14 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
_LOGGER.debug("Discovered device %s (%s - %s)", name, model, hkid)
# Device isn't paired with us or anyone else.
# But we have a 'complete' config entry for it - that is probably
# invalid. Remove it automatically.
existing = find_existing_host(self.hass, hkid)
if not paired and existing:
await self.hass.config_entries.async_remove(existing.entry_id)
# Set unique-id and error out if it's already configured
await self.async_set_unique_id(normalize_hkid(hkid))
self._abort_if_unique_id_configured()
@ -230,13 +237,6 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
if model in HOMEKIT_IGNORE:
return self.async_abort(reason="ignored_model")
# Device isn't paired with us or anyone else.
# But we have a 'complete' config entry for it - that is probably
# invalid. Remove it automatically.
existing = find_existing_host(self.hass, hkid)
if existing:
await self.hass.config_entries.async_remove(existing.entry_id)
self.model = model
self.hkid = hkid
@ -250,17 +250,6 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
hkid = discovery_props["id"]
existing = find_existing_host(self.hass, hkid)
if existing:
_LOGGER.info(
(
"Legacy configuration for homekit accessory %s"
"not loaded as already migrated"
),
hkid,
)
return self.async_abort(reason="already_configured")
_LOGGER.info(
(
"Legacy configuration %s for homekit"

View file

@ -3,7 +3,7 @@
"name": "HomeKit Controller",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/homekit_controller",
"requirements": ["aiohomekit[IP]==0.2.10"],
"requirements": ["aiohomekit[IP]==0.2.11"],
"dependencies": [],
"zeroconf": ["_hap._tcp.local."],
"codeowners": ["@Jc2k"]

View file

@ -163,7 +163,7 @@ aioftp==0.12.0
aioharmony==0.1.13
# homeassistant.components.homekit_controller
aiohomekit[IP]==0.2.10
aiohomekit[IP]==0.2.11
# homeassistant.components.emulated_hue
# homeassistant.components.http

View file

@ -62,7 +62,7 @@ aiobotocore==0.11.1
aioesphomeapi==2.6.1
# homeassistant.components.homekit_controller
aiohomekit[IP]==0.2.10
aiohomekit[IP]==0.2.11
# homeassistant.components.emulated_hue
# homeassistant.components.http

View file

@ -2,6 +2,8 @@
import datetime
from unittest import mock
from aiohomekit.testing import FakeController
import asynctest
import pytest
@ -12,3 +14,11 @@ def utcnow(request):
with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt
yield dt_utcnow
@pytest.fixture
def controller(hass):
"""Replace aiohomekit.Controller with an instance of aiohomekit.testing.FakeController."""
instance = FakeController()
with asynctest.patch("aiohomekit.Controller", return_value=instance):
yield instance

File diff suppressed because it is too large Load diff