Help needed creating a custom Hub Component

Yes. That should work. You can also do a device list to see if the value has been updated.

It worked. It was not plugged in. :slight_smile: Just because it return true does not mean to took effect. Looks like we need to query after an update to see if the state changed.

I was typing my response when you sent yours :slight_smile:

Thanks. I was looking at the API doc and thought the example was the only choice. I started the code as a library to HA. Right now I have a CLI entry point so I can run the API from a command line. Right now phase 1 is reading. Next is updating.
What is taking so long I am learning python and git as the repository.
Have you had a chance to look at it yet? Right now I am still updating the master branch till I get the ability to read and update. Then I will start to look at building the component.

I saw the repo. That’s pretty good so far. I tried building a proper component with full support, but the Home assistant document was not great back then. I see that the developer site is much better now.
I don’t think I will have the time, but I will try and help as much as I can.

Thanks for your time. Your comments have help me connect the dots on where we need to go. We are going to get there :slight_smile:

Last night and this evening I have been spending time with asyncio.
I have a gap in python right now. I getting ready to redo the api and make more event driven. Right now it is still synchronous in it sends a message and waits for the response.
I was using a websocket plugin for chrome (Smart Websocket). So I left it connected while I was typing. When some of my rules started firing, the response was showing up! That when it dawn on me 1. I did not know enough python yet to handle asynchronous messages and 2. why the API need to be separate of component.
I say all this at my current understanding, asyncio is all running in the same thread. Good news when you are waiting for an I/O the thread is yielded, so now other code can run, but if something is blocking then the thread stops.
So…
My next task is to finish wrapping the API with a shell I can run from the command line to test it. I am starting to see how I need to handle asynchronous events. I finally connected the dots that my devices can generate a message all on their own. So I am going to redo the API so send and receive are two different functions. The command to turn on a light will send a message and I may queue that it was sent, so as to match the response and know if a sent message with a command or was someone turning on the light.

@kdvlr Thought I would throw out a quick update. I feel pretty comfortable commanding the A+ and consuming the events. I have been hung on python learning for awhile now. The library I was using is Websockets and it is asyncio based. I been trying to spin it off into another thread and that is where I been having problems. If my main() starts the event_loop (asyncio.get_event_loop().run_until_…) I can communicate all I want.

But, If I try and start it in a different thread, so I can control the API from the outside, that is where I run into issue. I kind of burned out my welcome on the dev chat. My first question was on topic, but then it went more into them teaching me python. For now I have stuck a pin understanding hass code and focus on getting the api not to block and calls. On the dev chat they said you can’t use asyncio and threads together. I assumed they were saying not together at my level of python understanding. Since Websockets is asyncio based, I guess I need a different library.

Just curious what is your “python understanding” level?
What are you using for websocket library?
I am going to post some links to threads in the python forum where I was looking for answers. It might trigger someone thinking here …
https://python-forum.io/Thread-Pro-s-and-Con-s-of-some-different-websocket-libraries
https://python-forum.io/Thread-Trying-to-understand-blocking-in-both-multitasking-and-asyncio

Here is some test code where I was launching the event_loop in a different thread. For some reason even if in a different thread, as soon as I run the event_loop it blocks other threads.
Here is an example code to demonstrate where threads are getting blocked.
The post has two modules and the output. The CLI is my command line interface and it creates an instance of a Test class. That class is representing my A+ api. Right before I go into a loop that represents my code that will control the API, I start the event_loop required for the websockets. ‘a = Test(f), a.start()’. Inside the a.start() is where a thread is created for the event_loop. The thread does start and the event loop does block the thread as I would expect. But it also blocks the other threads also. I did not expect that. Maybe I am creating threads wrong.

As posted the event_loop is rem out in the loop_helper function.
There are two way of lunching the main(). 1. is by an entry point created with “setup.py devlop”, the second is running it from the interpreter. Right now I an testing in PyCharm.
All work functions are a series of prints to show work is being done. There are three threads started from main() and the main() has a loop with prints to show work after the threads are started. Since each function is in it’s own thread, I expect that they will not block each other, including the loop in the main(). There is the class “Test” that starts a event_loop (more details below) from the main(). If it works the event_loop will run a function that does a loop of prints to show work.
When the event_loop is no longer rem out, I expect the loop_helper() to print “test from helper” and return, then the start() print “Receiver running” and return. Then the main() will fall into it’s loop. It does not. The loop.run_until blocks the main thread. The other threads started run as expected.
Why does it block the main() and how can I start the event_loop where it does not block the main()?

Here is the boiled down code and output. If I can get the event_loop to run and not block the other threads, I can make the API work.
https://hastebin.com/qayataqoxi.sql

@kdvlr Fixed it. https://python-forum.io/Thread-Trying-to-understand-blocking-in-both-multitasking-and-asyncio?pid=51143#pid51143

Bottom line when I was instancing the thread and defining the target I put the ‘()’ at the end. So it was evaluating the function instead of passing the argument.

Now that is working, I can see my current event model will not work. Time for the 3rd plan B.

@kdvlr Made lot of progress. The api is some what functional. It is the websocket branch. https://github.com/penright/pyalmondplus/tree/websocket

If you clone it, then run the CLI. I got tired of doing a command line, so if you create a secerts.yaml, you can put the url in it. dl will give you the list of devices. set will then ask you the ID, Device ID, and value. If you look at this output you can see me playing with it. If you go to the bottom, you can see where I actually open/closed the garage door. Here is the output https://hastebin.com/fiyifixowu.sql

Getting close to have the API done. Then the fun part of wrapping it into a component.

The websocket branch is not working. I did download the master and test. I think I need to make a few more changes to make it work. Will let you know.

I just updated it. Starting to move into the real world. In the almond branch of my hass fork is a config dir with the custom_components directory. This will be more the real world style. The platform has the Almond+ logic and it starts the platform that is a switch.
https://github.com/penright/home-assistant/tree/almondplus/config/custom_components

So the config will look something like …

almond_plus:
     url: ws://192.168.1.2:7681/root/xxxxx!

Right now the platform (almond_plus in the switch dir) does not look for only switch devices.
Also it is throwing an error right now.
Work in progress, but I am seeing the process now.

Actually this is getting closer to answering you OP. :slight_smile:

@kdvlr I wanted to get this to you.

There is a component and platform code. They both are called almond_plus.py. The component part goes into the custom_components directory off your config. Then off the custom_components directory create a directory called switch. Put the platform one in it.
Then in the configuration.yaml add

almond_plus:
     url: ws://192.168.1.2:7681/root/xxxxx

The component code is https://hastebin.com/bibiqesoqu.rb
The switch platform code is https://hastebin.com/pozotizadu.py

Right now the switch does not filter out switches, hence my garage door is showing as a switch.
Also, it does not turn anything off/on. And it maybe very unstable.

I am posting this to you because it is the glue to hass. It connected to the Almond+ and created some entities.
That is so… exciting for me with all the learning that has gone into it. :slight_smile:
I will be busy for the next week or so and I am not sure how much time I will get to spend on it. So I wanted to get this to you for now. Enjoy.

I think the switch on/off test is backwards. :blush:
Fruits of our labor …

This looks awesome! I will look at this over the weekend and let you know.

@kdvlr Been on vacation, back home now.
I moved what will be the API into the component for now. It makes debugging easier because there is a component and platform. I have them in the config directory for custom components. I also spin up a separate hass instance so I don’t have to keep restarting my production hass. I have not made much progress in gluing the API to hass component/platform. The dev_state now shows all correct information.
Were I am now struggling is how to connect to the “switch card” in the overview and to get state changes to update.

So it will make migrating the final changes into hass, I forked it and created a almond branch. There is a config director with me playing/learning component/platform integrations. So here is my current code trying to figure out how to update the states from components.

https://github.com/penright/home-assistant/tree/almondplus

https://github.com/penright/home-assistant/blob/almondplus/config/custom_components/almond_plus.py

https://github.com/penright/home-assistant/blob/almondplus/config/custom_components/switch/almond_plus.py

@kdvlr Just pushed an update that fixed the initial state. Now we need to figure out how to reflect changes from the Almond+ to the state.
#2 7/30/2018 @ 5:53pm my time.

BTW, do you what the “last active epoch” is used for and when is it updated?
I asked it on the Securifi forum: https://forum.securifi.com/index.php/topic,6123.0.html

Now to consume the overview toggle. :slight_smile:

@kdvlr I got the switch to work.
As of my last push this morning, switches are working either direction. This is what I would call a prototype. Right now I am calling I/O in the platform which is a no no. But it seems to work. Let me know what you think.

Do you have a garage door opener? Is yes, what brand?

@kdvlr what devices do you have?
I have switches
2 garage door
3 Almond Clicks

The reason is how deverist is our devices.
Been looking at cover (Garage Door) making good progress.

I have a bunch of devices and an A+ to test with, but no garage door opener for the A+. I got GarHAge set up on a NodeMCU and want to wire it up in the next few days.

Zigbee - Peanut plug, various door/window/temp moisture/temp, bulbs, motion
Z-wave - wall switches, smart plugs, locks with keypads, multi sensors, motion, door sensors, smoke/CO detectors

I’ll just have to take a look at what you’ve done and see if I can get it running :slight_smile:

Cool, I just put what is in the almond branch into my production hass.
Right now I am supporting two platforms, switches (type=1) and covers from a garage door type.

@gohassgo have you played with the Almond+ websocket api?

I don’t have a garage opener per se. I have a z wave isolated contact switch connected to my garage opener. So that registers as a switch in almond+

@kdvlr If I did not already have two go controls, I would do a open garage, nodecu. Have you seen one.

Just making sure I support platforms that will be used.
Looks like @gohassgomay have sensors, I don’t have any sensors to test or to see response.

I have about 6 multi sensors water temperature and motion , about 8 dimmer switches , 4 bulbs and 3 door locks.

I think once the base is there adding additional components should not be too difficult.