I am trying to integrate my securifi Almond+ to HASS and learning python at the same time. The API uses Websockets
Is there a tutorial on how to create a basic hub component? I tried to use wink as an example and it looks like it relies on pywink. I had a look at it, but since it is very feature rich, I am slightly confused.
If someone could just provide a very simple example /pseudo code of how to set this up, that would be much appreciated.
Here’s my understanding.
I need to create a pyalmond component that initiates and creates the websocket connection. This will be open on a single thread. I have this running partially.
I will need to have a device component, let us say a switch, that will talk to the pyalmond component and get/set the state.
I can send and receive messages using that, but I am more interested in what I need to do to integrate with HomeAssistant and how that works.
Take a light for example. Here are the three things that I need to accomplish.
Discover the lights. The API will allow me to get a list of devices, I can iterate through the list and fetch only the lights. Now how do I send this to HASS
Communicate state changes - Since its a constant connection I can get the state changes. I am trying to understand if HASS polls for any state change or does my script have to push the state change to HASS.
I think these are pretty basic questions, but I am unable to get the answers I need to come up with something from scratch. Any pointers would be appreciated.
Home assistant is in the process of moving to async instead of using threads. By doing so it will probably start using aiohttp for all internet communication including web sockets. I don’t know when this is all going to happen, but I am pretty sure this will be the future proof way of doing this…
It has taken me hours of study and experimentation but I think I have this figured out. It wasn’t easy as I am new to python too.
My implementation uses telnet to listen and talk to the Lutron Caseta Pro hub, your implementation my use a built in protocol or stack. You need to study the python libraries for asyncio (asynciolib) and how to add to the event loop in HA with coroutines. zone.py in the components directory is a good place to study as, they are using the basic functionality. Once I have the bugs wrung out on my code I will make it available.
Sorry about this but I have different answer now. After attempting to use the the event loop which utilizes asincio, appeared to be real stable, I had issues with future and exceptions. I have changed my methodology to a much simpler method. when using “hass.bus.listen_once(EVENT_HOMEASSISTANT_START, my_running_function)” I discovered that the function is started in i’s own thread so I just used a “while self.I_am_running” type loop with “time.sleep(self.interval)” in the loop to set my loop rate. Then a stop function which runs in a different thread can be used to turn off “self.I_am_running” which will stop the loop. This is simple but, I am finding that the timing in the loop stutters a bit since I run mine at 6 to 8 per second to read the switches. This is at least stable, handles the hub going offline and reconnecting and can be stopped and restarted.
If anyone has any input on dealing with this in the event loop using an outside socket handler (I am using telnetlib), I would like to hear it.
I too have a Almond+ and would like to use it as a bridge. I started googling and found this thread and another one you started https://community.home-assistant.io/t/newbie-hass-component/25826?u=penright. The other was newer but this had more activity. Which one?
I am a .NET guy, but can I speak other languages. I am willing to help.
So I have two questions: @kdvlr Where are you at on this project?
Anyone else interested?
I could not get around to creating a proper component, but ended up using command line sensors and switches. I have uploaded those very crude scripts into github here - https://github.com/kdvlr/almond-scripts/ That should get you started
Just started digging, but here is where I am at right now.
Somehow I find myself looking at …
As stupid as this sounds, I never really understood the HA architecture. Guess I still don’t.
Anyway, we do not want to do a “component”, we want to do a “platform”.
Thanks, quick question. I am trying to set up the environment to test. Is there a URL example I can test with from a browser so see if I am connected easily? If so what would the device list look like?
Was able to see the response. All that worked. Next step is a hello world python program. I have a mac at home and I am not a mac person. That step may take longer than it should seem. At least now, thanks to you, I can verify that the A+ is responding.
Thanks. How did you connect the dots on that looking at the API?
I guess whatever key/value you include gets updated?
So Value is the key and 0 or 40 is the value.