New component which needs discovery, which example should i look at?

I’d like to make a deConz component that can discover the sensors beeing lights, swithces and motions sensors by the gateway. I have a hard time to find good example how does the search look like. I know that deConz has REST interface which is simmilar to Hue and i know how to get the list of the devices and their state. I also know that deconz has a websocket interface so i can get events. What i don’t know is where can i find some examples showing how to issue the rest command then parse the json output and start adding the entities into HASS. Also where do i run my loop that i have a websocket connection so i can react to events from deconz ? I think the documentation is not clear on this.

Hope somebody has some ideas.


1 Like

You can look at the Axis component if you want to. It has all the parts you ask for ;). But might not be the best design example.

I’m interested in getting a deconz to skip the hue hub further down the line. Was also thinking about doing this later this year. But it is great if you do it!


Thanx, i had a look but seems to overwelming for me at the moment, i spent most of today reading and trying various things to see how far i get. I would like to make it very basic as a start so i would like to read the sensor list from deconz let’s say first temperature, add it to HASS then open a websocket and keep the value up to date inside HASS. Next thing is to get my IKEA remote inside HASS so i can use it for something else then firat after that the lights. I had a look at hue component and it seems complex for me. I don’t need groups, scenes or anything fancy. I would like to just be able to pair the IKEA / Philips / Osram lights, make them available in hass and then react to the websockets events. Maybe i am naive believing it should be simple. I mean it’s just a REST call, parse the JSON, iterate and push the entities to HASS then react to events on websocket from deconz and push events from HASS to deconz thru rest interface.

Even though if you manage to hack together a working component I would assume, that the core dev-team would rather like to see you just importing a library that handles the protocol-specific stuff. I’ve done that for HomeMatic (which is using events, but via a XML-RPC server) where in the beginning the thermostats with their functionality where directly implementen in HASS. Later on though all that stripped out of the HASS code and is now maintained in a dedicated project.
I don’t know deconz, but I’ve had a quick look and found this:
Maybe that’s a useful start for using a library, whose functions you would then use in the HASS component.

No I agree with you, take it step by step getting things working. I did the same with the Axis component. Get a sensor or light working, when that works great you can move over to do a hub for all of deconz. Then when you’re satisfied with how the component works you should follow @danielperna84 advice about breaking out device specific code to a separate package.


I am trying to use the bloomsky example and tweak it to see if it can get my head around get one sensor in first.

Thank you, yes i see your project is quite extensive. I have to start small and simple to get my head around the components.

@Robban @danielperna84 where would code for the events be ? I mean i did a component in custom_components and then did a sensor in sensors folder, now where do i put the websocket connection and how do i make it call my update of the sensor when events come in ? I know how to do the websocket and i can see the events in a terminal but i don’t know where to place it ?

In case of HomeMatic it works by using callback functions. There’s the XML-RPC server which receives the messages of the HomeMatic hub, and my library has a way of passing functions to it which will be called as soon as a message arrives. In your case you would be running your own websocket-server that would receive the events. The component at the HASS side would then have update-functions that will be called so soon as your library receives the event. Kinda hard to explain. Your module would be some sort of man in the middle with a simple API hass would subscribe to.

@danielperna84 I see but is there an easy way to get this running now just in a simple way in my custom component before i get the hang of splitting things up like can i start this websocker in my component and make a method to call to ?

Also another question, i have this remote from IKEA which has 4 buttons and i’d like to get that into HASS. What kind of entity is that ? Is that a sensor ? binary_sensor ? The remote is zigbee and i get events in websocket when you click a button with a code so i can identify which button the user uses, there is no on of buttons just simple push buttons.

I have never done a custom component, so I’m not sure if there are any drawbacks to that approach. One that I would see is the async core HASS is using. It could be, that a component that somehow blocks while doing its work could mess up the rest of HASS. In your case you would need to start a websocket-server, which probably would be threaded when doing it the “easy” way. The HASS way would be to have an asynchronous serverm probably using the aiohttp module. Using async is a bit tricky though if you have never worked with it. But to wrap this up:
You custom component would need to:

  1. Start a websocket server which recives the messages
  2. Create entities according to what the “backend” says is available
  3. Tie the update-methods of your entities to the callback that’s handeling the websocket-messages

Regarding the IKEA: I have never used it, but depending on the implementation such devices with buttons don’t generate any entities. Instead only events on the HASS-eventbus are fired. You would see those if you set the log-level of HASS to debug. If you see something event- and device-related in the logs when pressing the buttons, then that’s how it has been implemented.

Thanks, so as i mentioned i used bloomsky component as inspiration and i copied as much as possible. I got the value from the temperature inside hass but only once. Its not getting updated. Does hass hass call the update by itself or do i need to do something ? When i have this running i will look into making a thread for the websocket then i guess i have to call update myslef and not rely on hass to call it. Brw i removed the throtteling from the bloomsky component just in case you were wondering. Maybe i should post some code but its so unstructured and ugly so i thought ill spare you that :wink:

Regarding the push button device. If it’s not an entity how i then use it in automations, how would i refer to it ?

I’m not up to date how HASS is doing this nowadays. Usually the update-method should be called once a minute or so. Maybe removing the throtteling disabled auto-updates. Not sure about that though.
Anyways, the easy way would indeed be to poll the states according to the update interval. That however would mean that it would take up to 60 seconds before HASS notices the change. That’s why going event-route would lead to much more accurate results.

The example for HomeMatic for automations based on events:

     platform: event
     event_type: homematic.keypress
       name: Kitchen Switch
       channel: 1
       param: PRESS_SHORT
     service: switch.turn_on
     entity_id: switch.Kitchen_Ambience

Other components have differnt event types and different even data. But in general that’s how event-based automations look like.

Hi @danielperna84, so i went the event way with an websocket and got it ALMOST to work. I have this issue: Event based Automation not working, why?. Maybe you have an idea ?

@donnib will the integration do push notifications? So the remotes will give instant events.

@Robban Yes, i made the first version of the component that supports Hue Motion Sensor, IKEA bulb (E14) and the 4 button remote. All work in HASS using Websockets. The remote gives instant response to HASS when you click on it using HASS events like @danielperna84 suggested albeit the code is VERY ugly and rough it works for the moment.

I like what I hear :+1:

Do you run deconz headless?

As soon as you get closer to release I will buy myself a conbee!

Well i don’t have a monitor connected to the rPi, it still run the ui app but i don’t use it. I just use the REST API and the Websocket to control and listen to events. I don’t know if i ever release the code as i am not sure it can reach prime time. There is a lot of work to make it nice i think. I am not even sure i will end up using it myself. I am a bit disappointed how zigbee devices work and the way the big companies implement it so if you want to make sure everything plays nice you have to use their own gateway sooooo… but then again none of the major companies, Osram, IKEA or Philips deliver a Websocket solution so polling is just plain stupid but i guess they are not quite interested their devices integrated with other things well.

I think Ikea Trådfri is actually pushing data, but I’d still prefer the internal hub.

If you’re not gonna finish the component, will you share it so someone else can pick it up and have a look and a try at getting it upstream? As I mentioned earlier, I am interested in getting this supported officially.

@Robban ok i didn’t know that.

Sure we will figure something out. I have not given up yet. I am implementing support for groups as we speak and expect to have it running during today.