2019-02-13 20:21:14 +00:00
|
|
|
"""Support for exposing Home Assistant via Zeroconf."""
|
2016-04-10 22:34:04 +00:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-01-03 20:39:42 +00:00
|
|
|
from homeassistant import util
|
2016-04-10 23:02:07 +00:00
|
|
|
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP, __version__)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
DOMAIN = 'zeroconf'
|
2016-04-10 23:09:52 +00:00
|
|
|
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
ZEROCONF_TYPE = '_home-assistant._tcp.local.'
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
|
|
|
|
2019-05-14 03:58:13 +00:00
|
|
|
async def async_setup(hass, config):
|
2016-04-10 23:02:07 +00:00
|
|
|
"""Set up Zeroconf and make Home Assistant discoverable."""
|
2019-05-14 03:58:13 +00:00
|
|
|
from aiozeroconf import Zeroconf, ServiceInfo
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2019-05-14 03:58:13 +00:00
|
|
|
zeroconf = Zeroconf(hass.loop)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
zeroconf_name = '{}.{}'.format(hass.config.location_name, ZEROCONF_TYPE)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
params = {
|
|
|
|
'version': __version__,
|
|
|
|
'base_url': hass.config.api.base_url,
|
2019-03-11 02:55:36 +00:00
|
|
|
# always needs authentication
|
|
|
|
'requires_api_password': True,
|
2016-09-30 02:02:22 +00:00
|
|
|
}
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2017-01-03 20:39:42 +00:00
|
|
|
host_ip = util.get_local_ip()
|
|
|
|
|
2016-12-02 05:43:33 +00:00
|
|
|
try:
|
2017-01-03 20:39:42 +00:00
|
|
|
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip)
|
2019-05-14 03:58:13 +00:00
|
|
|
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, address=host_ip_pton,
|
|
|
|
port=hass.http.server_port, weight=0, priority=0,
|
|
|
|
properties=params)
|
2016-12-02 05:43:33 +00:00
|
|
|
except socket.error:
|
2017-01-03 20:39:42 +00:00
|
|
|
host_ip_pton = socket.inet_pton(socket.AF_INET6, host_ip)
|
2019-05-14 03:58:13 +00:00
|
|
|
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, address6=host_ip_pton,
|
|
|
|
port=hass.http.server_port, weight=0, priority=0,
|
|
|
|
properties=params)
|
2017-01-03 20:39:42 +00:00
|
|
|
|
2019-05-14 03:58:13 +00:00
|
|
|
await zeroconf.register_service(info)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2019-05-14 03:58:13 +00:00
|
|
|
async def stop_zeroconf(event):
|
2016-04-10 23:02:07 +00:00
|
|
|
"""Stop Zeroconf."""
|
2019-05-14 03:58:13 +00:00
|
|
|
await zeroconf.unregister_service(info)
|
|
|
|
await zeroconf.close()
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2019-05-14 03:58:13 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_zeroconf)
|
2016-04-10 22:34:04 +00:00
|
|
|
|
2016-04-10 23:02:07 +00:00
|
|
|
return True
|