hass-core/homeassistant/components/zeroconf.py

50 lines
1.3 KiB
Python
Raw Normal View History

2016-04-10 15:34:04 -07:00
"""
This module exposes Home Assistant via Zeroconf.
Zeroconf is also known as Bonjour, Avahi or Multicast DNS (mDNS).
2016-04-10 15:34:04 -07:00
For more details about Zeroconf, please refer to the documentation at
https://home-assistant.io/components/zeroconf/
"""
import logging
import socket
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP, __version__)
2016-04-10 15:34:04 -07:00
REQUIREMENTS = ["zeroconf==0.17.5"]
_LOGGER = logging.getLogger(__name__)
DOMAIN = "zeroconf"
ZEROCONF_TYPE = "_home-assistant._tcp.local."
DEPENDENCIES = ["http"]
2016-04-10 15:34:04 -07:00
def setup(hass, config):
"""Set up Zeroconf and make Home Assistant discoverable."""
from zeroconf import Zeroconf, ServiceInfo
2016-04-10 15:34:04 -07:00
zeroconf = Zeroconf()
2016-04-10 15:34:04 -07:00
zeroconf_name = "{}.{}".format(hass.config.location_name,
ZEROCONF_TYPE)
2016-04-10 15:34:04 -07:00
params = {"version": __version__, "base_url": hass.http.base_url,
"needs_auth": (hass.http.api_password != "")}
2016-04-10 15:34:04 -07:00
info = ServiceInfo(ZEROCONF_TYPE, zeroconf_name,
socket.inet_aton(hass.http.routable_address),
hass.http.server_address[1], 0, 0, params)
2016-04-10 15:34:04 -07:00
zeroconf.register_service(info)
2016-04-10 15:34:04 -07:00
def stop_zeroconf(event):
"""Stop Zeroconf."""
zeroconf.unregister_service(info)
2016-04-10 15:34:04 -07:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zeroconf)
2016-04-10 15:34:04 -07:00
return True