pawlizio / my_velux

Custom component of velux integration for Home Assistant
32 stars 9 forks source link

Controlling Somfy smart plugs #19

Closed TheDatadrainer closed 1 year ago

TheDatadrainer commented 1 year ago

Hello, I would like to now if it is possible to implement the control of smart plugs compatible with io-homecontrol like the Somfy smart plug I have. It is registered in the KLF-200 as "On/Off Switch" and I can control it through pyvlx with pyvlx.nodes['On/Off Switch'].set_off() and pyvlx.nodes['On/Off Switch'].set_on(). pyvlx have everything implemented I think (at least from the source code I have read), but neither the HA original velux add-on or my_velux use it. If it is doable, that would be gladly appreciated. I can test it for you if none of you have a compatible smart plug. Thanks.

pawlizio commented 1 year ago

@TheDatadrainer : Please install the pre-release (https://github.com/pawlizio/my_velux/releases/tag/2.8.2) and test if this adds a switch entity to HA for your plug correcty with the state and functions to turn it on and off.

TheDatadrainer commented 1 year ago

I just installed the 2.8.2 version. The switch is recognized as en entity, I can add it as a card button but when I try to use it, I get: Fail to call service switch//turn_on. cannot convert 'SwitchParameterOn' object to bytes.

pawlizio commented 1 year ago

Ok, have you ever tested the commands "pyvlx.nodes['On/Off Switch'].set_off() and pyvlx.nodes['On/Off Switch'].set_on()." you have mentioned above? The error looks like an issue within pyvlx.

TheDatadrainer commented 1 year ago

I made some tests during summer 2020. I have recovered my old script but using it with Python 3.10 and 3.11 given me errors with semaphore timeout and deprecated functions... So for now, I'm not able to to use my script with newer versions of Python. My smart plug is connected to a lamp and I was able to control it. That I remember perfectly. But at that time I was testing pyvlx and velux-klf200-api for node.js. I was pretty sure I got it working with pyvlx, but now I have doubts... Here is the script I used:

import asyncio
from pyvlx import PyVLX

async def main(loop):
    pyvlx = PyVLX('credits.yaml', loop=loop)
    await pyvlx.load_nodes()
    if pyvlx.nodes['On/Off Switch'].is_off():
        await pyvlx.nodes['On/Off Switch'].set_on()
    await asyncio.sleep(15)
    await pyvlx.nodes['On/Off Switch'].set_off()
    await pyvlx.disconnect()
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(loop))
    loop.close()

In newer versions of python, I think an asynchronous script is called with async.run() instead. But as there is no need for the loop event, pyvlx seems not happy. I tried setting the loop argument to None (the default behavior if no argument value is given) with no success. I'll try tomorrow to get it working with Python 3.11 to see if I got the same error as in HA. If it is a bug or something missing in the code, I'll try with pyvlx developer to know what can cause this.

pawlizio commented 1 year ago

Ok, I've updated my pyvlx as follows: https://github.com/pawlizio/pyvlx/commit/31001c98851c11264a8b6834acfa0a5acd4255ca Please activate debug for pyvlx and reboot HA, it should load pyvlx with v0.1.33. Please check if you still have the same issue.

TheDatadrainer commented 1 year ago

Hi. Tested with the patch. How does the update process work with your fork of pyvlx? Is your add-on downloading automatically the corresponding files after reading the latest version number somewhere? I'm interested. Though, unfortunately it seems it requires more than a pass through function. The switch is working. When activated, the plug turn On. But this state is not reported and remain Off from the system point of view. Trying to activate it again, does nothing. The plug remain On. On the KLR-200 (the touch screen remote) there is no state. Just two buttons, On and Off. So maybe the pyvlx.nodes[].is_off()/.is_on() command does not work or are are not complete, so maybe it is required to keep the state internally. I don't know. I haven't found a state information into the KLF-200 API. Just values of 0x0000 and 0xC800 to control 0x03C0. Can you please, confirm it? Thanks Here is my log. home-assistant.log Regards.

pawlizio commented 1 year ago

Hi @TheDatadrainer , in the manifest of my_velux custom component the requirement is set "git+https://github.com/pawlizio/pyvlx.git@master#pyvlx==0.2.21". With that my_velux loads always the latest github repository of my master branch unless official pyvlx is updated to 0.2.21. So once I update the version of my master pyvlx repository you just need to reboot and HA will check for updates.

So, now I'm on version v0.1.35 with pyvls where I have changed a bit the behaviour on how the state of the switch is updated. It should work now similar to blinds (covers). That means, once you change the state on the physical switch, maybe by a button or another device, HA should receive NodeStatePositionChangeNotifications with the updated state.

Please test if this works now. Best regards

TheDatadrainer commented 1 year ago

Hi @pawlizio, Unfortunately there is no change in the behavior of the switch. Activation put the plug to On. The virtual switch instantly go back to Off but the plug remain On. Its harder than I though to get this working :( But that's great to see you are working on it so this type of smart plug can be supported in a near future :)

pawlizio commented 1 year ago

@TheDatadrainer , can you please share a log file. There should be new entries which I would like to check.

TheDatadrainer commented 1 year ago

Here is a partial log. During the time of the log I have: Use HA switch one time. Used the KRL-200 to switch it Off. Use HA switch three time in a row. Used the KRL-200 to switch it Off. Hope it will help. home-assistant.log

pawlizio commented 1 year ago

OK, I've found some possible problems. However this time you need to update the latest my_velux component 2.8.3 before you restart your HA. Changes have been made in the custom component and also within pyvlx.

Changes at pyvlx: https://github.com/pawlizio/pyvlx/commit/a040fe67fe6654517b1d4267ad59e2e7326a8e2a Purpose is to avoid updates of the virtual switch while the switch is still operating, so current_state does not match target.

Changes at my_velux: https://github.com/pawlizio/my_velux/compare/2.8.2...2.8.3 Register callbacks when the switch state was updated, because polling was disabled for switches.

TheDatadrainer commented 1 year ago

That's it! It works flawlessly :) I tried multiple times and it works just fine. Thank you very much for this addition and for trying to make a modern HA Velux integration. Please continue. If you need the log to check everything is really fine, here it is. Full pyvlx log from fresh start. home-assistant.log If everything is ok, I think I can close the thread.