home-assistant.io/source/developers/add_new_platform.markdown
2015-01-11 22:44:39 -08:00

3.5 KiB

layout, title, description, date, sidebar, comments, sharing, footer
layout title description date sidebar comments sharing footer
page Adding support for a new platform Hints and tips for when you're adding a new platform to Home Assistant. 2014-12-21 13:27 false false true true

Components that interact with devices are structured in core- and platform logic. This allows the same logic to be used for different platforms.

For example, the built-in switch component consists of the following files in homeassistant/components/switch/:

File Description
__init__.py Contains the Switch core logic.
wemo.py WeMo platform logic. Included if in config platform=wemo.
tellstick.py Tellstick platform logic. Included if in config platform=tellstick.

If you are planning to add support for a new type of device to an existing component, you can get away with only writing platform logic. Have a look at how the component works with other platforms and create a similar file for the platform that you would like to add.

Platform logic should not interface directly with the devices but use a third-party Python 3 library that speaks the actual API.

Allowing your platform to be discovered

Home Assistant has a discovery service running in the background to discover new devices. Whenever a new device is discovered, an SERVICE_DISCOVERED event will be fired with the found service and the information. The discovery component has some knowledge about which components handle which type of services and will ensure those are loaded and listening before firing the SERVICE_DISCOVERED event.

Add discovery instructions

Device discovery for Home Assistant has been extracted into an external library called NetDisco. This library is integrated using the discovery component and scans the network in intervals for uPnP and zeroconf/mDNS services.

To have your device be discovered, you will have to extend the NetDisco library to be able to find your device. This is done by adding a new discoverable. See the repository for examples of existing discoverables.

Listening to SERVICE_DISCOVERED events

From your component, you will have to set up the listening for specific services. Below an example how one would listen for discovered Chromecasts:

from homeassistant.loader import get_component

def setup(hass, config):
    discovery = get_component('discovery')

    def chromecast_discovered(service, info):
        """ Called when a Chromecast has been discovered. """
        print("Discovered a new Chromecast: {}".format(info))

    discovery.listen(
        hass, discovery.services.GOOGLE_CAST, chromecast_discovered)

Auto-loading your component upon discovery

The Discovery component is capable of setting up your components before firing the SERVICE_DISCOVERD event. To do this you will have to update the SERVICE_HANDLERS constant in the discovery component.

This option is currently limited to built-in components.