mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Improved Homekit tests (#12647)
* Spelling and typos * Updated 'test_homekit_pyhap_interaction' * Patch ip_address
This commit is contained in:
parent
da832dbda2
commit
eacfbc048a
@ -1,4 +1,4 @@
|
|||||||
"""Support for Apple Homekit.
|
"""Support for Apple HomeKit.
|
||||||
|
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/homekit/
|
https://home-assistant.io/components/homekit/
|
||||||
@ -30,7 +30,7 @@ HOMEKIT_FILE = '.homekit.state'
|
|||||||
|
|
||||||
|
|
||||||
def valid_pin(value):
|
def valid_pin(value):
|
||||||
"""Validate pincode value."""
|
"""Validate pin code value."""
|
||||||
match = _RE_VALID_PINCODE.findall(value.strip())
|
match = _RE_VALID_PINCODE.findall(value.strip())
|
||||||
if match == []:
|
if match == []:
|
||||||
raise vol.Invalid("Pin must be in the format: '123-45-678'")
|
raise vol.Invalid("Pin must be in the format: '123-45-678'")
|
||||||
@ -47,14 +47,14 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup(hass, config):
|
def async_setup(hass, config):
|
||||||
"""Setup the homekit component."""
|
"""Setup the HomeKit component."""
|
||||||
_LOGGER.debug("Begin setup homekit")
|
_LOGGER.debug("Begin setup HomeKit")
|
||||||
|
|
||||||
conf = config[DOMAIN]
|
conf = config[DOMAIN]
|
||||||
port = conf.get(CONF_PORT)
|
port = conf.get(CONF_PORT)
|
||||||
pin = str.encode(conf.get(CONF_PIN_CODE))
|
pin = str.encode(conf.get(CONF_PIN_CODE))
|
||||||
|
|
||||||
homekit = Homekit(hass, port)
|
homekit = HomeKit(hass, port)
|
||||||
homekit.setup_bridge(pin)
|
homekit.setup_bridge(pin)
|
||||||
|
|
||||||
hass.bus.async_listen_once(
|
hass.bus.async_listen_once(
|
||||||
@ -63,7 +63,7 @@ def async_setup(hass, config):
|
|||||||
|
|
||||||
|
|
||||||
def import_types():
|
def import_types():
|
||||||
"""Import all types from files in the homekit dir."""
|
"""Import all types from files in the HomeKit dir."""
|
||||||
_LOGGER.debug("Import type files.")
|
_LOGGER.debug("Import type files.")
|
||||||
# pylint: disable=unused-variable
|
# pylint: disable=unused-variable
|
||||||
from .covers import Window # noqa F401
|
from .covers import Window # noqa F401
|
||||||
@ -90,11 +90,11 @@ def get_accessory(hass, state):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class Homekit():
|
class HomeKit():
|
||||||
"""Class to handle all actions between homekit and Home Assistant."""
|
"""Class to handle all actions between HomeKit and Home Assistant."""
|
||||||
|
|
||||||
def __init__(self, hass, port):
|
def __init__(self, hass, port):
|
||||||
"""Initialize a homekit object."""
|
"""Initialize a HomeKit object."""
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self._port = port
|
self._port = port
|
||||||
self.bridge = None
|
self.bridge = None
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
"""Constants used be the homekit component."""
|
"""Constants used be the HomeKit component."""
|
||||||
MANUFACTURER = 'HomeAssistant'
|
MANUFACTURER = 'HomeAssistant'
|
||||||
|
|
||||||
# Service: AccessoryInfomation
|
# Service: AccessoryInfomation
|
||||||
|
@ -28,7 +28,7 @@ class TestHomekitSensors(unittest.TestCase):
|
|||||||
self.hass.bus.listen(EVENT_CALL_SERVICE, record_event)
|
self.hass.bus.listen(EVENT_CALL_SERVICE, record_event)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
"""Stop down everthing that was started."""
|
"""Stop down everything that was started."""
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
def test_window_set_cover_position(self):
|
def test_window_set_cover_position(self):
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
"""Tests for the homekit component."""
|
"""Tests for the HomeKit component."""
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import call, patch
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import setup
|
from homeassistant import setup
|
||||||
from homeassistant.core import Event
|
from homeassistant.core import Event
|
||||||
from homeassistant.components.homekit import (
|
from homeassistant.components.homekit import (
|
||||||
CONF_PIN_CODE, BRIDGE_NAME, Homekit, valid_pin)
|
CONF_PIN_CODE, BRIDGE_NAME, HOMEKIT_FILE, HomeKit, valid_pin)
|
||||||
from homeassistant.components.homekit.covers import Window
|
|
||||||
from homeassistant.components.homekit.sensors import TemperatureSensor
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_PORT, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
CONF_PORT, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||||
|
|
||||||
@ -27,20 +25,20 @@ CONFIG = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class TestHomekit(unittest.TestCase):
|
class TestHomeKit(unittest.TestCase):
|
||||||
"""Test the Multicover component."""
|
"""Test setup of HomeKit component and HomeKit class."""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Setup things to be run when tests are started."""
|
"""Setup things to be run when tests are started."""
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
"""Stop down everthing that was started."""
|
"""Stop down everything that was started."""
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
@patch(HOMEKIT_PATH + '.Homekit.start_driver')
|
@patch(HOMEKIT_PATH + '.HomeKit.start_driver')
|
||||||
@patch(HOMEKIT_PATH + '.Homekit.setup_bridge')
|
@patch(HOMEKIT_PATH + '.HomeKit.setup_bridge')
|
||||||
@patch(HOMEKIT_PATH + '.Homekit.__init__')
|
@patch(HOMEKIT_PATH + '.HomeKit.__init__')
|
||||||
def test_setup_min(self, mock_homekit, mock_setup_bridge,
|
def test_setup_min(self, mock_homekit, mock_setup_bridge,
|
||||||
mock_start_driver):
|
mock_start_driver):
|
||||||
"""Test async_setup with minimal config option."""
|
"""Test async_setup with minimal config option."""
|
||||||
@ -57,9 +55,9 @@ class TestHomekit(unittest.TestCase):
|
|||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
self.assertEqual(mock_start_driver.call_count, 1)
|
self.assertEqual(mock_start_driver.call_count, 1)
|
||||||
|
|
||||||
@patch(HOMEKIT_PATH + '.Homekit.start_driver')
|
@patch(HOMEKIT_PATH + '.HomeKit.start_driver')
|
||||||
@patch(HOMEKIT_PATH + '.Homekit.setup_bridge')
|
@patch(HOMEKIT_PATH + '.HomeKit.setup_bridge')
|
||||||
@patch(HOMEKIT_PATH + '.Homekit.__init__')
|
@patch(HOMEKIT_PATH + '.HomeKit.__init__')
|
||||||
def test_setup_parameters(self, mock_homekit, mock_setup_bridge,
|
def test_setup_parameters(self, mock_homekit, mock_setup_bridge,
|
||||||
mock_start_driver):
|
mock_start_driver):
|
||||||
"""Test async_setup with full config option."""
|
"""Test async_setup with full config option."""
|
||||||
@ -82,20 +80,17 @@ class TestHomekit(unittest.TestCase):
|
|||||||
for value in ('123-45-678', '234-56-789'):
|
for value in ('123-45-678', '234-56-789'):
|
||||||
self.assertTrue(schema(value))
|
self.assertTrue(schema(value))
|
||||||
|
|
||||||
@patch('pyhap.accessory_driver.AccessoryDriver.persist')
|
@patch('pyhap.accessory_driver.AccessoryDriver')
|
||||||
@patch('pyhap.accessory_driver.AccessoryDriver.stop')
|
@patch('pyhap.accessory.Bridge.add_accessory')
|
||||||
@patch('pyhap.accessory_driver.AccessoryDriver.start')
|
|
||||||
@patch(HOMEKIT_PATH + '.import_types')
|
@patch(HOMEKIT_PATH + '.import_types')
|
||||||
@patch(HOMEKIT_PATH + '.get_accessory')
|
@patch(HOMEKIT_PATH + '.get_accessory')
|
||||||
def test_homekit_pyhap_interaction(
|
def test_homekit_pyhap_interaction(
|
||||||
self, mock_get_accessory, mock_import_types,
|
self, mock_get_accessory, mock_import_types,
|
||||||
mock_driver_start, mock_driver_stop, mock_file_persist):
|
mock_add_accessory, mock_acc_driver):
|
||||||
"""Test the interaction between the homekit class and pyhap."""
|
"""Test interaction between the HomeKit class and pyhap."""
|
||||||
acc1 = TemperatureSensor(self.hass, 'sensor.temp', 'Temperature')
|
mock_get_accessory.side_effect = ['TemperatureSensor', 'Window']
|
||||||
acc2 = Window(self.hass, 'cover.hall_window', 'Cover')
|
|
||||||
mock_get_accessory.side_effect = [acc1, acc2]
|
|
||||||
|
|
||||||
homekit = Homekit(self.hass, 51826)
|
homekit = HomeKit(self.hass, 51826)
|
||||||
homekit.setup_bridge(b'123-45-678')
|
homekit.setup_bridge(b'123-45-678')
|
||||||
|
|
||||||
self.assertEqual(homekit.bridge.display_name, BRIDGE_NAME)
|
self.assertEqual(homekit.bridge.display_name, BRIDGE_NAME)
|
||||||
@ -106,19 +101,24 @@ class TestHomekit(unittest.TestCase):
|
|||||||
self.hass.start()
|
self.hass.start()
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
homekit.start_driver(Event(EVENT_HOMEASSISTANT_START))
|
with patch('homeassistant.util.get_local_ip',
|
||||||
|
return_value='127.0.0.1'):
|
||||||
|
homekit.start_driver(Event(EVENT_HOMEASSISTANT_START))
|
||||||
|
|
||||||
|
ip_address = '127.0.0.1'
|
||||||
|
path = self.hass.config.path(HOMEKIT_FILE)
|
||||||
|
|
||||||
self.assertEqual(mock_get_accessory.call_count, 2)
|
self.assertEqual(mock_get_accessory.call_count, 2)
|
||||||
self.assertEqual(mock_import_types.call_count, 1)
|
self.assertEqual(mock_import_types.call_count, 1)
|
||||||
self.assertEqual(mock_driver_start.call_count, 1)
|
self.assertEqual(mock_acc_driver.mock_calls,
|
||||||
|
[call(homekit.bridge, 51826, ip_address, path),
|
||||||
|
call().start()])
|
||||||
|
|
||||||
accessories = homekit.bridge.accessories
|
self.assertEqual(mock_add_accessory.mock_calls,
|
||||||
self.assertEqual(accessories[2], acc1)
|
[call('TemperatureSensor'), call('Window')])
|
||||||
self.assertEqual(accessories[3], acc2)
|
|
||||||
|
|
||||||
mock_driver_stop.assert_not_called()
|
|
||||||
|
|
||||||
self.hass.bus.fire(EVENT_HOMEASSISTANT_STOP)
|
self.hass.bus.fire(EVENT_HOMEASSISTANT_STOP)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(mock_driver_stop.call_count, 1)
|
self.assertEqual(mock_acc_driver.mock_calls[2], call().stop())
|
||||||
|
self.assertEqual(len(mock_acc_driver.mock_calls), 3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user