Update creating_component_generic_discovery.md (#139)

This commit is contained in:
Johann Kellerman 2018-11-01 10:30:55 +02:00 committed by Paulus Schoutsen
parent 4b982f9636
commit 3133501503

View File

@ -6,10 +6,10 @@ New controller or hub components often need to add platforms in sub-components (
This can be achieved using the `load_platform` or `async_load_platform` methods from `homeassistant.helpers.discovery`: This can be achieved using the `load_platform` or `async_load_platform` methods from `homeassistant.helpers.discovery`:
```python ```python
def load_platform(hass, component, platform, discovered=None, hass_config=None) def load_platform(hass, component, platform, discovered, hass_config)
``` ```
From more info on how this works, refer to the [load_platform](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/helpers/discovery.py#L136) method. From more info on how this works, refer to the [load_platform](https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/helpers/discovery.py#L117) method.
### Example ### Example
@ -30,24 +30,24 @@ The source for your component can be located in your configuration directory for
~/.homeassistant/custom_components/switch/myflashyhub.py ~/.homeassistant/custom_components/switch/myflashyhub.py
``` ```
In the hub component `myflashyhub.py` you can call your light and switch components. To pass any non-serializable information to the platforms in the sub-component, you can use a global variable. In the hub component `myflashyhub.py` you can call your light and switch components. To pass any non-serializable information to the platforms in the sub-component, you should use `hass.data`.
```python ```python
from homeassistant.helpers.discovery import load_platform from homeassistant.helpers.discovery import load_platform
DOMAIN = 'myflashyhub'
DOMAIN = 'myflashyhub'
DATA_MFH = 'MFH' DATA_MFH = 'MFH'
def setup(hass, config): def setup(hass, hass_config):
"""Your controller/hub specific code.""" """Your controller/hub specific code."""
hass.data[DATA_MFH] = SomeObjectToInitialiseGlobal() hass.data[DATA_MFH] = SomeObjectToInitialise()
#--- snip --- #--- snip ---
load_platform(hass, 'light', DOMAIN) load_platform(hass, 'light', DOMAIN, None, hass_config)
load_platform(hass, 'switch', DOMAIN, {'optional': 'arguments'}) load_platform(hass, 'switch', DOMAIN, {'optional': 'arguments'}, hass_config)
``` ```
Add your custom device specific code to the `setup_platform` method in `light/myflashyhub.py` and `switch/myflashyhub`. Add your custom device specific code to the `setup_platform` method in `light/myflashyhub.py` and `switch/myflashyhub.py`.
```python ```python
import custom_components.myflashyhub as myflashyhub import custom_components.myflashyhub as myflashyhub