Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor homekit_controller config flow tests #32141

Merged
merged 4 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Jc2k marked this conversation as resolved.
Show resolved Hide resolved

# 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)
Jc2k marked this conversation as resolved.
Show resolved Hide resolved
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
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
Jc2k marked this conversation as resolved.
Show resolved Hide resolved
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