Skip to content

Commit

Permalink
Refactor homekit_controller config flow tests (#32141)
Browse files Browse the repository at this point in the history
* Config flow test refactor

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

* Feedback from review

* Missing apostrophe
  • Loading branch information
Jc2k committed Feb 25, 2020
1 parent ba4cc37 commit c9d78aa
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 610 deletions.
31 changes: 10 additions & 21 deletions homeassistant/components/homekit_controller/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re

import aiohomekit
from aiohomekit import Controller
from aiohomekit.controller.ip import IpPairing
import voluptuous as vol

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -89,7 +88,7 @@ def __init__(self):
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):
Expand Down Expand Up @@ -203,6 +202,14 @@ async def async_step_zeroconf(self, discovery_info):

_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()

Expand Down Expand Up @@ -230,13 +237,6 @@ async def async_step_zeroconf(self, discovery_info):
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

Expand All @@ -250,17 +250,6 @@ async def async_import_legacy_pairing(self, discovery_props, pairing_data):

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"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/homekit_controller/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/components/homekit_controller/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import datetime
from unittest import mock

from aiohomekit.testing import FakeController
import asynctest
import pytest


Expand All @@ -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
Loading

0 comments on commit c9d78aa

Please sign in to comment.