To async or not to async?


#1

Hey all, just created a sensor for LightwaveRF Energy

(https://github.com/asantaga/lightwaverf_HA_EnergySensor-) and it works fine. The implementation listens for the UDP messages from the LightwaveRF hub and then updates HA.

Questions

  1. The code to listen to the hub is trivial like 10 lines or so, do I still need to create a library for PyPi?
  2. The code is currently a “sync” code, and the UDP messages can take up to 10 seconds to appear so the code is blocking till then… Is this ok in “sync” mode? or should I really be looking at “async”? Does HA poll its sensors in separate threads or will this slow HA down?
  3. If i need to go “async” does anyone have a tutorial on this? Im assuming i need to run a thread within my sensor right?

thanks
Advice welcome :slight_smile:


#2

Yes. The Home Assistant code should always be independet of the logic that is required to work with devices / vendors / protocols / etc.


#3

ok,

So whilst developing I guess I need to deploy the code to Pypi and Im assuming HA dynamically loads it right?


#4

While developing you of course can have it installed manually. But to be released with Home Assistant it has to be included like this. And it also has to be in the requirements-file. The development documentation talks about all of this: https://developers.home-assistant.io/docs/en/development_index.html


#5

yeah I saw that :slight_smile:

thanks, what Im going to do is first work on making it async so it doesnt block the main update() thread and then package it up…

I’ll look at other threads on hints for making it async (ie threading models etc). Is there a tutorial anywhere? I couldnt see one.


#6

Hi Daniel,

Ive been looking around for docs on how to code a async platform and Im totally confused do you by any chance have a reference example?


#7

No. I haven’t done anything async yet. If would, I’d look at other components similar to what I want to do and steal the code from there. :wink: