developers.home-assistant/docs/creating_component_index.md
Paulus Schoutsen ba924f7330
Docs to talk about intregrations (#215)
* Add manifest docs

* Talk about integrations

* Apply suggestions from code review

Co-Authored-By: balloob <paulus@home-assistant.io>

* Address comments

* Apply suggestions from code review

Co-Authored-By: balloob <paulus@home-assistant.io>

* Address comments

* Final comment

* Mention codeowners script
2019-04-06 20:08:49 -07:00

1.4 KiB

title sidebar_label
Creating a Minimal Component Minimal Component

Alright, you learned about the manifest, so it's time to write your first code for your integration. AWESOME. Don't worry, we've tried hard to keep it as easy as possible.

More extensive examples of integrations are available from our example repository.

The code

Each component needs to have 2 basic parts: it needs to define a DOMAIN constant that contains the domain of the integration. The second part is that it needs to define a setup method that returns a boolean if the set up was successful. So let's take a look at how this looks:

DOMAIN = 'hello_state'

def setup(hass, config):
    hass.states.set('hello_state.world', 'Paulus')

    # Return boolean to indicate that initialization was successful.
    return True

And if you prefer an async component:

DOMAIN = 'hello_state'

async def async_setup(hass, config):
    hass.states.async_set('hello_state.world', 'Paulus')

    # Return boolean to indicate that initialization was successful.
    return True

That's it! If you load this, you will see a new state in the state machine.

To load this, add hello_state: to your configuration.yaml file and create a file <config_dir>/custom_components/hello_state/__init__.py with the below code to test it locally.