core/homeassistant/components/zeroconf.py

65 lines
1.6 KiB
Python
Raw Normal View History

2016-04-10 22:34:04 +00:00
"""
This module exposes Home Assistant via Zeroconf.
For more details about this component, please refer to the documentation at
2016-04-10 22:34:04 +00:00
https://home-assistant.io/components/zeroconf/
"""
import logging
import socket
import voluptuous as vol
from homeassistant import util
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP, __version__)
2016-04-10 22:34:04 +00:00
2018-03-12 21:02:03 +00:00
REQUIREMENTS = ['zeroconf==0.20.0']
_LOGGER = logging.getLogger(__name__)
2016-04-10 22:34:04 +00:00
DEPENDENCIES = ['api']
DOMAIN = 'zeroconf'
2016-04-10 22:34:04 +00:00
ZEROCONF_TYPE = '_home-assistant._tcp.local.'
2016-04-10 22:34:04 +00:00
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({}),
}, extra=vol.ALLOW_EXTRA)
2016-04-10 22:34:04 +00:00
def setup(hass, config):
"""Set up Zeroconf and make Home Assistant discoverable."""
from zeroconf import Zeroconf, ServiceInfo
2016-04-10 22:34:04 +00:00
zeroconf = Zeroconf()
2016-04-10 22:34:04 +00:00
zeroconf_name = '{}.{}'.format(hass.config.location_name, ZEROCONF_TYPE)
2016-04-10 22:34:04 +00:00
requires_api_password = hass.config.api.api_password is not None
params = {
'version': __version__,
'base_url': hass.config.api.base_url,
'requires_api_password': requires_api_password,
}
2016-04-10 22:34:04 +00:00
host_ip = util.get_local_ip()
2016-12-02 05:43:33 +00:00
try:
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip)
2016-12-02 05:43:33 +00:00
except socket.error:
host_ip_pton = socket.inet_pton(socket.AF_INET6, host_ip)
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name, host_ip_pton,
hass.http.server_port, 0, 0, params)
2016-04-10 22:34:04 +00:00
zeroconf.register_service(info)
2016-04-10 22:34:04 +00:00
def stop_zeroconf(event):
"""Stop Zeroconf."""
zeroconf.unregister_service(info)
zeroconf.close()
2016-04-10 22:34:04 +00:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zeroconf)
2016-04-10 22:34:04 +00:00
return True