Skip to content
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
59 changes: 22 additions & 37 deletions homeassistant/components/homekit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from aiohttp import web
import voluptuous as vol
from zeroconf import InterfaceChoice

from homeassistant.components import zeroconf
from homeassistant.components.binary_sensor import (
Expand Down Expand Up @@ -71,7 +70,6 @@
DEFAULT_AUTO_START,
DEFAULT_PORT,
DEFAULT_SAFE_MODE,
DEFAULT_ZEROCONF_DEFAULT_INTERFACE,
DOMAIN,
EVENT_HOMEKIT_CHANGED,
HOMEKIT,
Expand Down Expand Up @@ -113,23 +111,24 @@ def _has_all_unique_names_and_ports(bridges):
return bridges


BRIDGE_SCHEMA = vol.Schema(
{
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),
vol.Optional(CONF_ADVERTISE_IP): vol.All(ipaddress.ip_address, cv.string),
vol.Optional(CONF_AUTO_START, default=DEFAULT_AUTO_START): cv.boolean,
vol.Optional(CONF_SAFE_MODE, default=DEFAULT_SAFE_MODE): cv.boolean,
vol.Optional(CONF_FILTER, default={}): BASE_FILTER_SCHEMA,
vol.Optional(CONF_ENTITY_CONFIG, default={}): validate_entity_config,
vol.Optional(
CONF_ZEROCONF_DEFAULT_INTERFACE, default=DEFAULT_ZEROCONF_DEFAULT_INTERFACE,
): cv.boolean,
},
extra=vol.ALLOW_EXTRA,
BRIDGE_SCHEMA = vol.All(
cv.deprecated(CONF_ZEROCONF_DEFAULT_INTERFACE),
vol.Schema(
{
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),
vol.Optional(CONF_ADVERTISE_IP): vol.All(ipaddress.ip_address, cv.string),
vol.Optional(CONF_AUTO_START, default=DEFAULT_AUTO_START): cv.boolean,
vol.Optional(CONF_SAFE_MODE, default=DEFAULT_SAFE_MODE): cv.boolean,
vol.Optional(CONF_FILTER, default={}): BASE_FILTER_SCHEMA,
vol.Optional(CONF_ENTITY_CONFIG, default={}): validate_entity_config,
vol.Optional(CONF_ZEROCONF_DEFAULT_INTERFACE): cv.boolean,
},
extra=vol.ALLOW_EXTRA,
),
)

CONFIG_SCHEMA = vol.Schema(
Expand Down Expand Up @@ -233,11 +232,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
},
)
)
interface_choice = (
InterfaceChoice.Default
if options.get(CONF_ZEROCONF_DEFAULT_INTERFACE)
else None
)

homekit = HomeKit(
hass,
Expand All @@ -248,11 +242,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
entity_config,
safe_mode,
advertise_ip,
interface_choice,
entry.entry_id,
)
await hass.async_add_executor_job(homekit.setup)
await homekit.async_setup_zeroconf()
zeroconf_instance = await zeroconf.async_get_instance(hass)
await hass.async_add_executor_job(homekit.setup, zeroconf_instance)

undo_listener = entry.add_update_listener(_async_update_listener)

Expand Down Expand Up @@ -404,7 +397,6 @@ def __init__(
entity_config,
safe_mode,
advertise_ip=None,
interface_choice=None,
entry_id=None,
):
"""Initialize a HomeKit object."""
Expand All @@ -416,14 +408,13 @@ def __init__(
self._config = entity_config
self._safe_mode = safe_mode
self._advertise_ip = advertise_ip
self._interface_choice = interface_choice
self._entry_id = entry_id
self.status = STATUS_READY

self.bridge = None
self.driver = None

def setup(self):
def setup(self, zeroconf_instance):
"""Set up bridge and accessory driver."""
# pylint: disable=import-outside-toplevel
from .accessories import HomeBridge, HomeDriver
Expand All @@ -440,7 +431,7 @@ def setup(self):
port=self._port,
persist_file=persist_file,
advertised_address=self._advertise_ip,
interface_choice=self._interface_choice,
zeroconf_instance=zeroconf_instance,
)

# If we do not load the mac address will be wrong
Expand All @@ -455,12 +446,6 @@ def setup(self):
_LOGGER.debug("Safe_mode selected for %s", self._name)
self.driver.safe_mode = True

async def async_setup_zeroconf(self):
"""Share the system zeroconf instance."""
# Replace the existing zeroconf instance.
await self.hass.async_add_executor_job(self.driver.advertiser.close)
self.driver.advertiser = await zeroconf.async_get_instance(self.hass)

def reset_accessories(self, entity_ids):
"""Reset the accessory to load the latest configuration."""
aid_storage = self.hass.data[DOMAIN][self._entry_id][AID_STORAGE]
Expand Down
11 changes: 1 addition & 10 deletions homeassistant/components/homekit/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
CONF_FILTER,
CONF_SAFE_MODE,
CONF_VIDEO_CODEC,
CONF_ZEROCONF_DEFAULT_INTERFACE,
DEFAULT_AUTO_START,
DEFAULT_CONFIG_FLOW_PORT,
DEFAULT_SAFE_MODE,
DEFAULT_ZEROCONF_DEFAULT_INTERFACE,
SHORT_BRIDGE_NAME,
VIDEO_CODEC_COPY,
)
Expand Down Expand Up @@ -227,14 +225,7 @@ async def async_step_advanced(self, user_input=None):
vol.Optional(
CONF_SAFE_MODE,
default=self.homekit_options.get(CONF_SAFE_MODE, DEFAULT_SAFE_MODE),
): bool,
vol.Optional(
CONF_ZEROCONF_DEFAULT_INTERFACE,
default=self.homekit_options.get(
CONF_ZEROCONF_DEFAULT_INTERFACE,
DEFAULT_ZEROCONF_DEFAULT_INTERFACE,
),
): bool,
): bool
}
)

Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/homekit/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
DEFAULT_PORT = 51827
DEFAULT_CONFIG_FLOW_PORT = 51828
DEFAULT_SAFE_MODE = False
DEFAULT_ZEROCONF_DEFAULT_INTERFACE = False
DEFAULT_VIDEO_CODEC = VIDEO_CODEC_LIBX264
DEFAULT_VIDEO_MAP = "0:v:0"
DEFAULT_VIDEO_PACKET_SIZE = 1316
Expand Down Expand Up @@ -267,7 +266,6 @@
CONFIG_OPTIONS = [
CONF_FILTER,
CONF_AUTO_START,
CONF_ZEROCONF_DEFAULT_INTERFACE,
CONF_SAFE_MODE,
CONF_ENTITY_CONFIG,
]
3 changes: 1 addition & 2 deletions homeassistant/components/homekit/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"advanced": {
"data": {
"auto_start": "[%key:component::homekit::config::step::user::data::auto_start%]",
"safe_mode": "Safe Mode (enable only if pairing fails)",
"zeroconf_default_interface": "Use default zeroconf interface (enable if the bridge cannot be found in the Home app)"
"safe_mode": "Safe Mode (enable only if pairing fails)"
},
"description": "These settings only need to be adjusted if the HomeKit bridge is not functional.",
"title": "Advanced Configuration"
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/homekit/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"advanced": {
"data": {
"auto_start": "Autostart (disable if using Z-Wave or other delayed start system)",
"safe_mode": "Safe Mode (enable only if pairing fails)",
"zeroconf_default_interface": "Use default zeroconf interface (enable if the bridge cannot be found in the Home app)"
"safe_mode": "Safe Mode (enable only if pairing fails)"
},
"description": "These settings only need to be adjusted if the HomeKit bridge is not functional.",
"title": "Advanced Configuration"
Expand Down
22 changes: 4 additions & 18 deletions tests/components/homekit/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _mock_config_entry_with_options_populated():
},
"auto_start": False,
"safe_mode": False,
"zeroconf_default_interface": True,
},
)

Expand Down Expand Up @@ -149,12 +148,7 @@ async def test_options_flow_advanced(hass):

with patch("homeassistant.components.homekit.async_setup_entry", return_value=True):
result3 = await hass.config_entries.options.async_configure(
result2["flow_id"],
user_input={
"auto_start": True,
"safe_mode": True,
"zeroconf_default_interface": False,
},
result2["flow_id"], user_input={"auto_start": True, "safe_mode": True},
)

assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Expand All @@ -167,7 +161,6 @@ async def test_options_flow_advanced(hass):
"include_entities": [],
},
"safe_mode": True,
"zeroconf_default_interface": False,
}


Expand Down Expand Up @@ -202,8 +195,7 @@ async def test_options_flow_basic(hass):

with patch("homeassistant.components.homekit.async_setup_entry", return_value=True):
result3 = await hass.config_entries.options.async_configure(
result2["flow_id"],
user_input={"safe_mode": True, "zeroconf_default_interface": False},
result2["flow_id"], user_input={"safe_mode": True},
)

assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Expand All @@ -216,7 +208,6 @@ async def test_options_flow_basic(hass):
"include_entities": [],
},
"safe_mode": True,
"zeroconf_default_interface": False,
}


Expand Down Expand Up @@ -264,8 +255,7 @@ async def test_options_flow_with_cameras(hass):

with patch("homeassistant.components.homekit.async_setup_entry", return_value=True):
result4 = await hass.config_entries.options.async_configure(
result3["flow_id"],
user_input={"safe_mode": True, "zeroconf_default_interface": False},
result3["flow_id"], user_input={"safe_mode": True},
)

assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Expand All @@ -279,7 +269,6 @@ async def test_options_flow_with_cameras(hass):
},
"entity_config": {"camera.native_h264": {"video_codec": "copy"}},
"safe_mode": True,
"zeroconf_default_interface": False,
}

# Now run though again and verify we can turn off copy
Expand Down Expand Up @@ -315,8 +304,7 @@ async def test_options_flow_with_cameras(hass):

with patch("homeassistant.components.homekit.async_setup_entry", return_value=True):
result4 = await hass.config_entries.options.async_configure(
result3["flow_id"],
user_input={"safe_mode": True, "zeroconf_default_interface": False},
result3["flow_id"], user_input={"safe_mode": True},
)

assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Expand All @@ -330,7 +318,6 @@ async def test_options_flow_with_cameras(hass):
},
"entity_config": {"camera.native_h264": {}},
"safe_mode": True,
"zeroconf_default_interface": False,
}


Expand All @@ -353,7 +340,6 @@ async def test_options_flow_blocked_when_from_yaml(hass):
"exclude_entities": ["climate.front_gate"],
},
"safe_mode": False,
"zeroconf_default_interface": True,
},
source=SOURCE_IMPORT,
)
Expand Down
Loading