Update component discovery doc for accuracy. (#4049)

* Update component discovery doc for accuracy.

* Replace name with a notional one
This commit is contained in:
Eitan Mosenkis 2017-12-04 08:50:18 +02:00 committed by Fabian Affolter
parent a120cee6cc
commit 6e3e782ffb

View File

@ -2,7 +2,7 @@
layout: page layout: page
title: "Component Discovery" title: "Component Discovery"
description: "How to make component discovery work." description: "How to make component discovery work."
date: 2016-04-16 14:24 -07:00 date: 2017-11-23 07:27 +02:00
sidebar: true sidebar: true
comments: false comments: false
sharing: true sharing: true
@ -23,22 +23,37 @@ To have your device be discovered, you will have to extend the NetDisco library
### {% linkable_title Listening to `SERVICE_DISCOVERED` events %} ### {% linkable_title Listening to `SERVICE_DISCOVERED` events %}
From your component, you will have to set up the listening for specific services. Given below is an example how one would listen for discovered Chromecasts: From your component, you will have to set up the listening for specific services. Given below is an example how one would listen for a discovered AwesomeDevice:
```python ```python
from homeassistant.loader import get_component from homeassistant.components.discovery import SERVICE_AWESOMEDEVICE
from homeassistant.helpers import discovery
DOMAIN = 'awesomedevice'
DEPENDENCIES = ['http']
def setup(hass, config): def setup(hass, config):
discovery = get_component('discovery') cfg = config.get(DOMAIN)
def chromecast_discovered(service, info): def device_discovered(service, info):
""" Called when a Chromecast has been discovered. """ """ Called when a Awesome device has been discovered. """
print("Discovered a new Chromecast: {}".format(info)) print("Discovered a new Awesome device: {}".format(info))
discovery.listen( discovery.listen(
hass, discovery.services.GOOGLE_CAST, chromecast_discovered) hass, SERVICE_AWESOMEDEVICE, device_discovered)
return True
``` ```
### {% linkable_title Auto-loading your component upon discovery %} ### {% linkable_title 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`](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/discovery.py#L29) constant in [the `discovery` component](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/discovery.py). The `discovery` component is capable of setting up your components before firing the `EVENT_PLATFORM_DISCOVERED` event. To do this you will have to update the [`SERVICE_HANDLERS`](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/discovery.py#L40) constant in [the `discovery` component](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/discovery.py):
```python
SERVICE_AWESOMEDEVICE = 'awesomedevice'
SERVICE_HANDLERS = {
...
SERVICE_AWESOMEDEVICE: ('awesomedevice', None),
}