diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 41b0791a352..ce5f30d7bf2 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -29,7 +29,7 @@ from .util import show_setup_message, validate_entity_config TYPES = Registry() _LOGGER = logging.getLogger(__name__) -REQUIREMENTS = ['HAP-python==2.0.0'] +REQUIREMENTS = ['HAP-python==2.1.0'] # #### Driver Status #### STATUS_READY = 0 @@ -185,7 +185,8 @@ class HomeKit(): ip_addr = self._ip_address or get_local_ip() path = self.hass.config.path(HOMEKIT_FILE) self.bridge = HomeBridge(self.hass) - self.driver = HomeDriver(self.bridge, self._port, ip_addr, path) + self.driver = HomeDriver(self.hass, self.bridge, port=self._port, + address=ip_addr, persist_file=path) def add_bridge_accessory(self, state): """Try adding accessory to bridge if configured beforehand.""" @@ -213,8 +214,8 @@ class HomeKit(): self.add_bridge_accessory(state) self.bridge.set_driver(self.driver) - if not self.bridge.paired: - show_setup_message(self.hass, self.bridge) + if not self.driver.state.paired: + show_setup_message(self.hass, self.driver.state.pincode) _LOGGER.debug('Driver start') self.hass.add_job(self.driver.start) diff --git a/homeassistant/components/homekit/accessories.py b/homeassistant/components/homekit/accessories.py index 7ec1fb542c9..ff835659221 100644 --- a/homeassistant/components/homekit/accessories.py +++ b/homeassistant/components/homekit/accessories.py @@ -115,20 +115,23 @@ class HomeBridge(Bridge): """Prevent print of pyhap setup message to terminal.""" pass - def add_paired_client(self, client_uuid, client_public): - """Override super function to dismiss setup message if paired.""" - super().add_paired_client(client_uuid, client_public) - dismiss_setup_message(self.hass) - - def remove_paired_client(self, client_uuid): - """Override super function to show setup message if unpaired.""" - super().remove_paired_client(client_uuid) - show_setup_message(self.hass, self) - class HomeDriver(AccessoryDriver): """Adapter class for AccessoryDriver.""" - def __init__(self, *args, **kwargs): + def __init__(self, hass, *args, **kwargs): """Initialize a AccessoryDriver object.""" super().__init__(*args, **kwargs) + self.hass = hass + + def pair(self, client_uuid, client_public): + """Override super function to dismiss setup message if paired.""" + value = super().pair(client_uuid, client_public) + if value: + dismiss_setup_message(self.hass) + return value + + def unpair(self, client_uuid): + """Override super function to show setup message if unpaired.""" + super().unpair(client_uuid) + show_setup_message(self.hass, self.state.pincode) diff --git a/homeassistant/components/homekit/util.py b/homeassistant/components/homekit/util.py index 5d86dbc4612..447257f9e8f 100644 --- a/homeassistant/components/homekit/util.py +++ b/homeassistant/components/homekit/util.py @@ -38,9 +38,9 @@ def validate_entity_config(values): return entities -def show_setup_message(hass, bridge): +def show_setup_message(hass, pincode): """Display persistent notification with setup information.""" - pin = bridge.pincode.decode() + pin = pincode.decode() _LOGGER.info('Pincode: %s', pin) message = 'To setup Home Assistant in the Home App, enter the ' \ 'following code:\n### {}'.format(pin) diff --git a/requirements_all.txt b/requirements_all.txt index 6797be02b7f..ffcbc6001cc 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -28,7 +28,7 @@ Adafruit-SHT31==1.0.2 DoorBirdPy==0.1.3 # homeassistant.components.homekit -HAP-python==2.0.0 +HAP-python==2.1.0 # homeassistant.components.notify.mastodon Mastodon.py==1.2.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index f12fdd7ce77..4baab1c79e9 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -19,7 +19,7 @@ requests_mock==1.5 # homeassistant.components.homekit -HAP-python==2.0.0 +HAP-python==2.1.0 # homeassistant.components.notify.html5 PyJWT==1.6.0 diff --git a/tests/components/homekit/test_accessories.py b/tests/components/homekit/test_accessories.py index f12b80632b6..1b06e245734 100644 --- a/tests/components/homekit/test_accessories.py +++ b/tests/components/homekit/test_accessories.py @@ -116,26 +116,6 @@ def test_home_bridge(): # setup_message bridge.setup_message() - # add_paired_client - with patch('pyhap.accessory.Accessory.add_paired_client') \ - as mock_add_paired_client, \ - patch('homeassistant.components.homekit.accessories.' - 'dismiss_setup_message') as mock_dissmiss_msg: - bridge.add_paired_client('client_uuid', 'client_public') - - mock_add_paired_client.assert_called_with('client_uuid', 'client_public') - mock_dissmiss_msg.assert_called_with('hass') - - # remove_paired_client - with patch('pyhap.accessory.Accessory.remove_paired_client') \ - as mock_remove_paired_client, \ - patch('homeassistant.components.homekit.accessories.' - 'show_setup_message') as mock_show_msg: - bridge.remove_paired_client('client_uuid') - - mock_remove_paired_client.assert_called_with('client_uuid') - mock_show_msg.assert_called_with('hass', bridge) - def test_home_driver(): """Test HomeDriver class.""" @@ -143,9 +123,30 @@ def test_home_driver(): ip_address = '127.0.0.1' port = 51826 path = '.homekit.state' + pin = b'123-45-678' with patch('pyhap.accessory_driver.AccessoryDriver.__init__') \ as mock_driver: - HomeDriver(bridge, ip_address, port, path) + driver = HomeDriver('hass', bridge, ip_address, port, path) mock_driver.assert_called_with(bridge, ip_address, port, path) + driver.state = Mock(pincode=pin) + + # pair + with patch('pyhap.accessory_driver.AccessoryDriver.pair') as mock_pair, \ + patch('homeassistant.components.homekit.accessories.' + 'dismiss_setup_message') as mock_dissmiss_msg: + driver.pair('client_uuid', 'client_public') + + mock_pair.assert_called_with('client_uuid', 'client_public') + mock_dissmiss_msg.assert_called_with('hass') + + # unpair + with patch('pyhap.accessory_driver.AccessoryDriver.unpair') \ + as mock_unpair, \ + patch('homeassistant.components.homekit.accessories.' + 'show_setup_message') as mock_show_msg: + driver.unpair('client_uuid') + + mock_unpair.assert_called_with('client_uuid') + mock_show_msg.assert_called_with('hass', pin) diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index 23f117b15a0..b22a7f63cda 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -107,7 +107,8 @@ async def test_homekit_setup(hass): path = hass.config.path(HOMEKIT_FILE) assert isinstance(homekit.bridge, HomeBridge) mock_driver.assert_called_with( - homekit.bridge, DEFAULT_PORT, IP_ADDRESS, path) + hass, homekit.bridge, port=DEFAULT_PORT, + address=IP_ADDRESS, persist_file=path) # Test if stop listener is setup assert hass.bus.async_listeners().get(EVENT_HOMEASSISTANT_STOP) == 1 @@ -119,7 +120,8 @@ async def test_homekit_setup_ip_address(hass): with patch(PATH_HOMEKIT + '.accessories.HomeDriver') as mock_driver: await hass.async_add_job(homekit.setup) - mock_driver.assert_called_with(ANY, DEFAULT_PORT, '172.0.0.0', ANY) + mock_driver.assert_called_with( + hass, ANY, port=DEFAULT_PORT, address='172.0.0.0', persist_file=ANY) async def test_homekit_add_accessory(hass): @@ -167,9 +169,10 @@ async def test_homekit_entity_filter(hass): async def test_homekit_start(hass, debounce_patcher): """Test HomeKit start method.""" + pin = b'123-45-678' homekit = HomeKit(hass, None, None, {}, {'cover.demo': {}}) homekit.bridge = HomeBridge(hass) - homekit.driver = Mock() + homekit.driver = Mock(state=Mock(paired=False, pincode=pin)) hass.states.async_set('light.demo', 'on') state = hass.states.async_all()[0] @@ -180,7 +183,7 @@ async def test_homekit_start(hass, debounce_patcher): await hass.async_add_job(homekit.start) mock_add_acc.assert_called_with(state) - mock_setup_msg.assert_called_with(hass, homekit.bridge) + mock_setup_msg.assert_called_with(hass, pin) assert homekit.driver.start.called is True assert homekit.status == STATUS_RUNNING diff --git a/tests/components/homekit/test_util.py b/tests/components/homekit/test_util.py index f3ce35ee06b..42f81387960 100644 --- a/tests/components/homekit/test_util.py +++ b/tests/components/homekit/test_util.py @@ -2,7 +2,6 @@ import pytest import voluptuous as vol -from homeassistant.components.homekit.accessories import HomeBridge from homeassistant.components.homekit.const import HOMEKIT_NOTIFY_ID from homeassistant.components.homekit.util import ( show_setup_message, dismiss_setup_message, convert_to_float, @@ -10,7 +9,7 @@ from homeassistant.components.homekit.util import ( from homeassistant.components.homekit.util import validate_entity_config \ as vec from homeassistant.components.persistent_notification import ( - DOMAIN, ATTR_NOTIFICATION_ID) + DOMAIN, ATTR_MESSAGE, ATTR_NOTIFICATION_ID) from homeassistant.const import ( ATTR_CODE, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT, CONF_NAME) @@ -74,16 +73,17 @@ def test_density_to_air_quality(): async def test_show_setup_msg(hass): """Test show setup message as persistence notification.""" - bridge = HomeBridge(hass) + pincode = b'123-45-678' call_create_notification = async_mock_service(hass, DOMAIN, 'create') - await hass.async_add_job(show_setup_message, hass, bridge) + await hass.async_add_job(show_setup_message, hass, pincode) await hass.async_block_till_done() assert call_create_notification assert call_create_notification[0].data[ATTR_NOTIFICATION_ID] == \ HOMEKIT_NOTIFY_ID + assert pincode.decode() in call_create_notification[0].data[ATTR_MESSAGE] async def test_dismiss_setup_msg(hass):