4.6 KiB
layout, title, date, sidebar, comments, sharing, footer
layout | title | date | sidebar | comments | sharing | footer |
---|---|---|---|---|---|---|
page | Developers | 2014-12-21 13:32 | false | true | true | true |
Home Assistant can be extended by components. Components can listen for- or trigger events and offer services. Components are written in Python and can do all the goodness that Python has to offer.
Home Assistant offers built-in components but it is easy to built your own. An example component can be found in /config/custom_components/example.py
.
Note
Home Assistant will use the directory that contains your config file as the directory that holds your customizations. By default this is the config
folder in your current work directory. You can use a different folder by running Home Assistant with the --config argument python3 homeassistant --config /YOUR/CONFIG/PATH/
.
A component will be loaded on start if a section (ie. [light]
) for it exists in the config file. A component can also be loaded if another component is loaded that depends on it. When loading a component Home Assistant will check the following paths:
<config directory>/custom_components/<component name>
homeassistant/components/<component name>
(built-in components)
Once loaded, a component will only be setup if all dependencies can be loaded and are able to setup. Keep an eye on the logs to see if loading and setup of your component went well.
Warning
*Warning:* You can override a built-in component by offering a component with the same name in your custom_components folder. This is not recommended and may lead to unexpected behavior!
Each component is responsible for a specific domain within Home Assistant. An example is the switch component, which is responsible for interaction with different types of switches. The switch component consists of the following files in homeassistant/components/switch/
:
File | Description |
---|---|
__init__.py | Contains the Switch component code. |
wemo.py | WeMo platform support. Included if in config platform=wemo . |
tellstick.py | Tellstick platform support. Included if in config platform=tellstick . |
If a component exists, your job is easy. 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. If you cannot find a suitable component, you'll have to add it yourself. When writing a component try to structure it after the Switch component to maximize reusability.
Communication between Home Assistant and devices should happen via third-party libraries that implement the device API. This will make sure the platform support code stays as small as possible.
After a component is loaded the bootstrapper will call its setup method setup(hass, config)
:
Parameter | Description |
---|---|
hass |
The Home Assistant object. Call its methods to track time, register services or listen for events: Overview of available methods. |
config |
A dict containing the configuration. The keys of the config-dict are component names and the value is another dict with configuration attributes. |
Guidance on using the Home Assistant object
The Home Assistant object contains three objects to help you interact with the system.
Object | Description |
---|---|
hass.states |
This is the StateMachine. The StateMachine allows you to see which states are available and set/test states for specified entities. See available methods. |
hass.events |
This is the EventBus. The EventBus allows you to listen and trigger events. See available methods. |
hass.services |
This is the ServiceRegistry. The ServiceRegistry allows you to register services. See available methods. |
Example on using the configuration parameter
If your configuration file containes the following lines:
[example]
host=paulusschoutsen.nl
Then in the setup-method of your component you will be able to refer to config['example']['host']
to get the value paulusschoutsen.nl
.