Skip to content

Commit

Permalink
Bugfix HomeKit name and serial_number (#15600)
Browse files Browse the repository at this point in the history
* Bugfix HomeKit name and serial_number

* Revert serial_number changes
  • Loading branch information
cdce8p authored and balloob committed Jul 22, 2018
1 parent 33f1577 commit 4de847f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
19 changes: 13 additions & 6 deletions homeassistant/components/homekit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
from homeassistant.util import get_local_ip
from homeassistant.util.decorator import Registry
from .const import (
CONF_AUTO_START, CONF_ENTITY_CONFIG, CONF_FEATURE_LIST, CONF_FILTER,
DEFAULT_AUTO_START, DEFAULT_PORT, DEVICE_CLASS_CO2, DEVICE_CLASS_PM25,
DOMAIN, HOMEKIT_FILE, SERVICE_HOMEKIT_START, TYPE_OUTLET, TYPE_SWITCH)
BRIDGE_NAME, CONF_AUTO_START, CONF_ENTITY_CONFIG, CONF_FEATURE_LIST,
CONF_FILTER, DEFAULT_AUTO_START, DEFAULT_PORT, DEVICE_CLASS_CO2,
DEVICE_CLASS_PM25, DOMAIN, HOMEKIT_FILE, SERVICE_HOMEKIT_START,
TYPE_OUTLET, TYPE_SWITCH)
from .util import (
show_setup_message, validate_entity_config, validate_media_player_features)

Expand All @@ -43,6 +44,8 @@

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.All({
vol.Optional(CONF_NAME, default=BRIDGE_NAME):
vol.All(cv.string, vol.Length(min=3, max=25)),
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_IP_ADDRESS):
vol.All(ipaddress.ip_address, cv.string),
Expand All @@ -58,13 +61,15 @@ async def async_setup(hass, config):
_LOGGER.debug('Begin setup HomeKit')

conf = config[DOMAIN]
name = conf[CONF_NAME]
port = conf[CONF_PORT]
ip_address = conf.get(CONF_IP_ADDRESS)
auto_start = conf[CONF_AUTO_START]
entity_filter = conf[CONF_FILTER]
entity_config = conf[CONF_ENTITY_CONFIG]

homekit = HomeKit(hass, port, ip_address, entity_filter, entity_config)
homekit = HomeKit(hass, name, port, ip_address, entity_filter,
entity_config)
await hass.async_add_job(homekit.setup)

if auto_start:
Expand Down Expand Up @@ -176,9 +181,11 @@ def generate_aid(entity_id):
class HomeKit():
"""Class to handle all actions between HomeKit and Home Assistant."""

def __init__(self, hass, port, ip_address, entity_filter, entity_config):
def __init__(self, hass, name, port, ip_address, entity_filter,
entity_config):
"""Initialize a HomeKit object."""
self.hass = hass
self._name = name
self._port = port
self._ip_address = ip_address
self._filter = entity_filter
Expand All @@ -199,7 +206,7 @@ def setup(self):
path = self.hass.config.path(HOMEKIT_FILE)
self.driver = HomeDriver(self.hass, address=ip_addr,
port=self._port, persist_file=path)
self.bridge = HomeBridge(self.hass, self.driver)
self.bridge = HomeBridge(self.hass, self.driver, self._name)

def add_bridge_accessory(self, state):
"""Try adding accessory to bridge if configured beforehand."""
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit/accessories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from homeassistant.util import dt as dt_util

from .const import (
BRIDGE_MODEL, BRIDGE_NAME, BRIDGE_SERIAL_NUMBER, CHAR_BATTERY_LEVEL,
BRIDGE_MODEL, BRIDGE_SERIAL_NUMBER, CHAR_BATTERY_LEVEL,
CHAR_CHARGING_STATE, CHAR_STATUS_LOW_BATTERY, DEBOUNCE_TIMEOUT,
MANUFACTURER, SERV_BATTERY_SERVICE)
from .util import (
Expand Down Expand Up @@ -141,7 +141,7 @@ def update_state(self, new_state):
class HomeBridge(Bridge):
"""Adapter class for Bridge."""

def __init__(self, hass, driver, name=BRIDGE_NAME):
def __init__(self, hass, driver, name):
"""Initialize a Bridge object."""
super().__init__(driver, name)
self.set_info_service(
Expand Down
2 changes: 1 addition & 1 deletion tests/components/homekit/test_accessories.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async def test_battery_service(hass, hk_driver):

def test_home_bridge(hk_driver):
"""Test HomeBridge class."""
bridge = HomeBridge('hass', hk_driver)
bridge = HomeBridge('hass', hk_driver, BRIDGE_NAME)
assert bridge.hass == 'hass'
assert bridge.display_name == BRIDGE_NAME
assert bridge.category == 2 # Category.BRIDGE
Expand Down
27 changes: 15 additions & 12 deletions tests/components/homekit/test_homekit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
STATUS_STOPPED, STATUS_WAIT)
from homeassistant.components.homekit.accessories import HomeBridge
from homeassistant.components.homekit.const import (
CONF_AUTO_START, DEFAULT_PORT, DOMAIN, HOMEKIT_FILE, SERVICE_HOMEKIT_START)
CONF_AUTO_START, BRIDGE_NAME, DEFAULT_PORT, DOMAIN, HOMEKIT_FILE,
SERVICE_HOMEKIT_START)
from homeassistant.const import (
CONF_IP_ADDRESS, CONF_PORT,
CONF_NAME, CONF_IP_ADDRESS, CONF_PORT,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import State
from homeassistant.helpers.entityfilter import generate_filter
Expand Down Expand Up @@ -47,7 +48,8 @@ async def test_setup_min(hass):
assert await setup.async_setup_component(
hass, DOMAIN, {DOMAIN: {}})

mock_homekit.assert_any_call(hass, DEFAULT_PORT, None, ANY, {})
mock_homekit.assert_any_call(hass, BRIDGE_NAME, DEFAULT_PORT, None, ANY,
{})
assert mock_homekit().setup.called is True

# Test auto start enabled
Expand All @@ -60,15 +62,16 @@ async def test_setup_min(hass):

async def test_setup_auto_start_disabled(hass):
"""Test async_setup with auto start disabled and test service calls."""
config = {DOMAIN: {CONF_AUTO_START: False, CONF_PORT: 11111,
CONF_IP_ADDRESS: '172.0.0.0'}}
config = {DOMAIN: {CONF_AUTO_START: False, CONF_NAME: 'Test Name',
CONF_PORT: 11111, CONF_IP_ADDRESS: '172.0.0.0'}}

with patch(PATH_HOMEKIT + '.HomeKit') as mock_homekit:
mock_homekit.return_value = homekit = Mock()
assert await setup.async_setup_component(
hass, DOMAIN, config)

mock_homekit.assert_any_call(hass, 11111, '172.0.0.0', ANY, {})
mock_homekit.assert_any_call(hass, 'Test Name', 11111, '172.0.0.0', ANY,
{})
assert mock_homekit().setup.called is True

# Test auto_start disabled
Expand Down Expand Up @@ -96,7 +99,7 @@ async def test_setup_auto_start_disabled(hass):

async def test_homekit_setup(hass, hk_driver):
"""Test setup of bridge and driver."""
homekit = HomeKit(hass, DEFAULT_PORT, None, {}, {})
homekit = HomeKit(hass, BRIDGE_NAME, DEFAULT_PORT, None, {}, {})

with patch(PATH_HOMEKIT + '.accessories.HomeDriver',
return_value=hk_driver) as mock_driver, \
Expand All @@ -115,7 +118,7 @@ async def test_homekit_setup(hass, hk_driver):

async def test_homekit_setup_ip_address(hass, hk_driver):
"""Test setup with given IP address."""
homekit = HomeKit(hass, DEFAULT_PORT, '172.0.0.0', {}, {})
homekit = HomeKit(hass, BRIDGE_NAME, DEFAULT_PORT, '172.0.0.0', {}, {})

with patch(PATH_HOMEKIT + '.accessories.HomeDriver',
return_value=hk_driver) as mock_driver:
Expand All @@ -126,7 +129,7 @@ async def test_homekit_setup_ip_address(hass, hk_driver):

async def test_homekit_add_accessory():
"""Add accessory if config exists and get_acc returns an accessory."""
homekit = HomeKit('hass', None, None, lambda entity_id: True, {})
homekit = HomeKit('hass', None, None, None, lambda entity_id: True, {})
homekit.driver = 'driver'
homekit.bridge = mock_bridge = Mock()

Expand All @@ -149,7 +152,7 @@ async def test_homekit_add_accessory():
async def test_homekit_entity_filter(hass):
"""Test the entity filter."""
entity_filter = generate_filter(['cover'], ['demo.test'], [], [])
homekit = HomeKit(hass, None, None, entity_filter, {})
homekit = HomeKit(hass, None, None, None, entity_filter, {})

with patch(PATH_HOMEKIT + '.get_accessory') as mock_get_acc:
mock_get_acc.return_value = None
Expand All @@ -169,7 +172,7 @@ async def test_homekit_entity_filter(hass):
async def test_homekit_start(hass, hk_driver, debounce_patcher):
"""Test HomeKit start method."""
pin = b'123-45-678'
homekit = HomeKit(hass, None, None, {}, {'cover.demo': {}})
homekit = HomeKit(hass, None, None, None, {}, {'cover.demo': {}})
homekit.bridge = 'bridge'
homekit.driver = hk_driver

Expand Down Expand Up @@ -199,7 +202,7 @@ async def test_homekit_start(hass, hk_driver, debounce_patcher):

async def test_homekit_stop(hass):
"""Test HomeKit stop method."""
homekit = HomeKit(hass, None, None, None, None)
homekit = HomeKit(hass, None, None, None, None, None)
homekit.driver = Mock()

assert homekit.status == STATUS_READY
Expand Down

0 comments on commit 4de847f

Please sign in to comment.