mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Refactor away deprecated homekit_controller test helpers (#32177)
This commit is contained in:
parent
7d8da47309
commit
1ddc1ebc6b
@ -4,10 +4,10 @@ import json
|
||||
import os
|
||||
from unittest import mock
|
||||
|
||||
from aiohomekit.exceptions import AccessoryNotFoundError
|
||||
from aiohomekit.model import Accessory
|
||||
from aiohomekit.model import Accessories, Accessory
|
||||
from aiohomekit.model.characteristics import CharacteristicsTypes
|
||||
from aiohomekit.model.services import ServicesTypes
|
||||
from aiohomekit.testing import FakeController
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.homekit_controller import config_flow
|
||||
@ -22,77 +22,6 @@ import homeassistant.util.dt as dt_util
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||
|
||||
|
||||
class FakePairing:
|
||||
"""
|
||||
A test fake that pretends to be a paired HomeKit accessory.
|
||||
|
||||
This only contains methods and values that exist on the upstream Pairing
|
||||
class.
|
||||
"""
|
||||
|
||||
def __init__(self, accessories):
|
||||
"""Create a fake pairing from an accessory model."""
|
||||
self.accessories = accessories
|
||||
self.pairing_data = {}
|
||||
self.available = True
|
||||
|
||||
async def list_accessories_and_characteristics(self):
|
||||
"""Fake implementation of list_accessories_and_characteristics."""
|
||||
accessories = [a.to_accessory_and_service_list() for a in self.accessories]
|
||||
# replicate what happens upstream right now
|
||||
self.pairing_data["accessories"] = accessories
|
||||
return accessories
|
||||
|
||||
async def get_characteristics(self, characteristics):
|
||||
"""Fake implementation of get_characteristics."""
|
||||
if not self.available:
|
||||
raise AccessoryNotFoundError("Accessory not found")
|
||||
|
||||
results = {}
|
||||
for aid, cid in characteristics:
|
||||
for accessory in self.accessories:
|
||||
if aid != accessory.aid:
|
||||
continue
|
||||
for service in accessory.services:
|
||||
for char in service.characteristics:
|
||||
if char.iid != cid:
|
||||
continue
|
||||
results[(aid, cid)] = {"value": char.get_value()}
|
||||
return results
|
||||
|
||||
async def put_characteristics(self, characteristics):
|
||||
"""Fake implementation of put_characteristics."""
|
||||
for aid, cid, new_val in characteristics:
|
||||
for accessory in self.accessories:
|
||||
if aid != accessory.aid:
|
||||
continue
|
||||
for service in accessory.services:
|
||||
for char in service.characteristics:
|
||||
if char.iid != cid:
|
||||
continue
|
||||
char.set_value(new_val)
|
||||
return {}
|
||||
|
||||
|
||||
class FakeController:
|
||||
"""
|
||||
A test fake that pretends to be a paired HomeKit accessory.
|
||||
|
||||
This only contains methods and values that exist on the upstream Controller
|
||||
class.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Create a Fake controller with no pairings."""
|
||||
self.pairings = {}
|
||||
|
||||
def add(self, accessories):
|
||||
"""Create and register a fake pairing for a simulated accessory."""
|
||||
pairing = FakePairing(accessories)
|
||||
self.pairings["00:00:00:00:00:00"] = pairing
|
||||
return pairing
|
||||
|
||||
|
||||
class Helper:
|
||||
"""Helper methods for interacting with HomeKit fakes."""
|
||||
|
||||
@ -151,28 +80,22 @@ 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)
|
||||
pairing = fake_controller.add(accessories)
|
||||
|
||||
discovery_info = {
|
||||
"name": "TestDevice",
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
"properties": {"md": "TestDevice", "id": "00:00:00:00:00:00", "c#": 1},
|
||||
}
|
||||
pairing_id = "00:00:00:00:00:00"
|
||||
|
||||
pairing.pairing_data.update(
|
||||
{"AccessoryPairingID": discovery_info["properties"]["id"]}
|
||||
)
|
||||
accessories_obj = Accessories()
|
||||
for accessory in accessories:
|
||||
accessories_obj.add_accessory(accessory)
|
||||
pairing = await fake_controller.add_paired_device(accessories_obj, pairing_id)
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
version=1,
|
||||
domain="homekit_controller",
|
||||
entry_id="TestData",
|
||||
data=pairing.pairing_data,
|
||||
data={"AccessoryPairingID": pairing_id},
|
||||
title="test",
|
||||
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
pairing_cls_loc = "homeassistant.components.homekit_controller.connection.IpPairing"
|
||||
@ -189,7 +112,11 @@ async def device_config_changed(hass, accessories):
|
||||
# Update the accessories our FakePairing knows about
|
||||
controller = hass.data[CONTROLLER]
|
||||
pairing = controller.pairings["00:00:00:00:00:00"]
|
||||
pairing.accessories = accessories
|
||||
|
||||
accessories_obj = Accessories()
|
||||
for accessory in accessories:
|
||||
accessories_obj.add_accessory(accessory)
|
||||
pairing.accessories = accessories_obj
|
||||
|
||||
discovery_info = {
|
||||
"name": "TestDevice",
|
||||
|
@ -7,6 +7,7 @@ https://github.com/home-assistant/home-assistant/issues/15336
|
||||
from unittest import mock
|
||||
|
||||
from aiohomekit import AccessoryDisconnectedError
|
||||
from aiohomekit.testing import FakePairing
|
||||
|
||||
from homeassistant.components.climate.const import (
|
||||
SUPPORT_TARGET_HUMIDITY,
|
||||
@ -15,7 +16,6 @@ from homeassistant.components.climate.const import (
|
||||
from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
from tests.components.homekit_controller.common import (
|
||||
FakePairing,
|
||||
Helper,
|
||||
device_config_changed,
|
||||
setup_accessories_from_file,
|
||||
|
@ -4,6 +4,7 @@ from datetime import timedelta
|
||||
from unittest import mock
|
||||
|
||||
from aiohomekit.exceptions import AccessoryDisconnectedError, EncryptionError
|
||||
from aiohomekit.testing import FakePairing
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.light import SUPPORT_BRIGHTNESS, SUPPORT_COLOR
|
||||
@ -11,7 +12,6 @@ import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.homekit_controller.common import (
|
||||
FakePairing,
|
||||
Helper,
|
||||
setup_accessories_from_file,
|
||||
setup_test_accessories,
|
||||
|
@ -505,8 +505,11 @@ async def test_parse_new_homekit_json(hass):
|
||||
on_char = service.add_char(CharacteristicsTypes.ON)
|
||||
on_char.value = 0
|
||||
|
||||
accessories = Accessories()
|
||||
accessories.add_accessory(accessory)
|
||||
|
||||
fake_controller = await setup_platform(hass)
|
||||
pairing = fake_controller.add([accessory])
|
||||
pairing = await fake_controller.add_paired_device(accessories, "00:00:00:00:00:00")
|
||||
pairing.pairing_data = {"AccessoryPairingID": "00:00:00:00:00:00"}
|
||||
|
||||
mock_path = mock.Mock()
|
||||
@ -551,8 +554,11 @@ async def test_parse_old_homekit_json(hass):
|
||||
on_char = service.add_char(CharacteristicsTypes.ON)
|
||||
on_char.value = 0
|
||||
|
||||
accessories = Accessories()
|
||||
accessories.add_accessory(accessory)
|
||||
|
||||
fake_controller = await setup_platform(hass)
|
||||
pairing = fake_controller.add([accessory])
|
||||
pairing = await fake_controller.add_paired_device(accessories, "00:00:00:00:00:00")
|
||||
pairing.pairing_data = {"AccessoryPairingID": "00:00:00:00:00:00"}
|
||||
|
||||
mock_path = mock.Mock()
|
||||
@ -601,8 +607,11 @@ async def test_parse_overlapping_homekit_json(hass):
|
||||
on_char = service.add_char(CharacteristicsTypes.ON)
|
||||
on_char.value = 0
|
||||
|
||||
accessories = Accessories()
|
||||
accessories.add_accessory(accessory)
|
||||
|
||||
fake_controller = await setup_platform(hass)
|
||||
pairing = fake_controller.add([accessory])
|
||||
pairing = await fake_controller.add_paired_device(accessories)
|
||||
pairing.pairing_data = {"AccessoryPairingID": "00:00:00:00:00:00"}
|
||||
|
||||
mock_listdir = mock.Mock()
|
||||
|
Loading…
x
Reference in New Issue
Block a user