Home Assistant component development


Caution this is an old version, it would not be accepted into HA

Please see the new version here.

While this code works it is not recommened.

This is my attempt to document the development of a component, binary_sensor platform and switch platform, for the Home Assistant project.


Ok lets start with the basics. I have a hardware device called a W800RF32A, it's a radio that receives data from X10 devices such as the KR19A, MS16A and the RSS16. Please see this page for a picture of the devices. They are keypads and motion sensors.

The W800RF32A receives data and transmits it to the computer via an RS232 serial link, There are also USB versions which should work fine. The W800RF32 can also be built using a 310MHz receiver module and an Arduino. We will cover that in another article.

So, why are we doing this, well, while the Home Assistant documentation is pretty good, it has a few weak areas, some examples tend to be a bit lacking in information and it is spread out over many pages. I tend to like a concise example that is documented all in one place.

Having said that, it would be a good idea for the new developer to read the following sections on the Home Assistant 101 page.

Right, lets start with a high level block diagram of what we are trying to accomplish, then we can dive into the code.
Here is the block diagram from the HA site. We are going to develop the Platform and the Component.



For our purposes and simplicity, I think we can safely reduce this down to the following:


This is an attempt at an event digram for the w800rf32 component/platform/pypi-module:

The next step is to boil the code down into blocks we need to have. Here is where the Home Assistant documentation does a credible job but fails in some of the detail. For areas that are covered well by the HA documentation I will link to, rather than duplicating.

For our component, we need to do the following:

    1. Import required python libraries
    2. Import HA constants, helpers and entities
    3. Add our requirements
    4. Set a DOMAIN
    5. Set some global constants and other variable we will require
    6. Setup the LOGGER
    7. Grab configuration from the yaml file
    8. Run setup
    9. Add required logic

For our platform, we need to do the following:

    1. Import required python libraries
    2. Import HA W800rf32 component, binary_sensor component, constants, helpers and entities
    3. Add our dependencies
    4. Set some global constants and other variable we will require
    5. Setup the LOGGER
    6. Grab configuration from the yaml file
    7. Run setup_platform
    8. Add required logic and binary sensor object

There will of course be some things that are part of both pieces of code and obviously we won't break those down in both cases. This is a long article and for those that want have the raw code and follow along or modify for there own purposes here are the links:

Note: I have renamed these files adding the component and platform portion. These files would normally live in the following folders, assuming Home Assistant is installed in:

/home/ha

Please note: This is a working piece of code but has not been accepted upstream yet.

The component file:
/home/ha/home-assistant/homeassistant/components/w800rf32.py
W800rf32 component code

The component breakdown and description

The binary sensor platform file:
/home/ha/home-assistant/homeassistant/components/binary_sensor/w800rf32.py
W800rf32 binary_sensor platform code

The binary sensor breakdown and description

The switch platform file:
/home/ha/home-assistant/homeassistant/components/switch/w800rf32.py
W800rf32 binary_sensor platform code

The switch breakdown and description