starkillerOG / motion-blinds

Python library for interfacing with Motion Blinds
MIT License
23 stars 9 forks source link

context.user_id #21

Closed wmollenvanger closed 2 years ago

wmollenvanger commented 2 years ago

Hi starkiller,

Sorry for the long read, needed to put down a lot of thoughts and efforts I had with this issue.

There are several ways to open/close my roller blinds and I need to distinguish between them in order to automate them correctly. For automation (close when it is sunny etc) I use node red with home assistant (love the graphical flow building). In node red I receive home assistant change events on the roller blinds.

One of the fields in the change event is the context.user_id which holds the home assistant user_id who triggered the event. I'd like to use this to check if a roller blind was manually set so automation shouldn't change it. The problem I experience is that this field sometimes is NULL instead of my user_id even when I triggered the action via the home assistant UI. The user_id being an actual user and not null seemed very inconsistent.

I didn't really know where to look (has, NR, integration) and did some testing with the following results:

Based on this I am suspecting that this has to do with the integration. Since my roller blinds can move for 20 to 30 seconds before the '"I've reached the requested position" is send, I think the integration cannot match the request with the position ack and loses the info on user_id. I guess there's a timeout involved where the integration waits for the result of a user action which, in my case, can take up to 30 seconds. Does this sound plausible? Can you reproduce this or do you want more info from me on something?

Thanks.

starkillerOG commented 2 years ago

@wmollenvanger Yes I think you explained it very well. Indeed when you request a blind to move through HA, only the command to move the blind is send and nothing more. Once the blind stops moving, it sends a 433MHz message to the gateway, the gateway then sends a UDP multicast message on the ethernet network which is picked up by HomeAssistant and that updates the state (position of the blind).

I indeed assume that HomeAssistant tries to match status updates to events by homeassistant with a certain timeout. So if the update comes fast enough HomeAssistant will assume it was due to the requested move, but if it takes to long it will see it as a stondalone event (like when you use a 433MHz remote to move the blind, then from the HomeAssistant side of things the blind moved on itself so the event will not have a user associated with it).

The whole user_id stuff is in the base code of HomeAssistant and not in the motionblinds integration.

starkillerOG commented 2 years ago

@wmollenvanger I am actually working on changing this behaviour right now. The new behaviour will be that when a move is requested through HASS, I will start asking the blind for its position every 5-10 seconds or so untill the postion of the blind does not change anymore. You will then get multiple postion updates, probably the first few will always have the user_id. And you can see the blind moving in the UI.

wmollenvanger commented 2 years ago

I saw somewhere you were working on that, hope I can use it as a workaround. Any ETA?

starkillerOG commented 2 years ago

@wmollenvanger I made the PR today and it is already merged into HomeAssistant: https://github.com/home-assistant/core/pull/68580. So the next version of HomeAssistant will have this new behaviour. The Beta release will be on March 30th and the official release will be on April 6th.

I already tested the code and it works.

wmollenvanger commented 2 years ago

That's great! I will test it when it comes out.

Let's close this issue.

wmollenvanger commented 2 years ago

It didn't work for me :-( I can see the events for the updates about every 5 sec but the user is still null. Would it be possible to have an update immediately after the move command to see if that will solve this? I can change and test the code on my installation if you tell me where it is (looked for it but can't find it).

Cheers.

starkillerOG commented 2 years ago

@wmollenvanger Thats unfortunate. You could try changing these lines: https://github.com/home-assistant/core/blob/eb10654e011621b4a7c3c5355f44587cf13270ca/homeassistant/components/motion_blinds/cover.py#L269-L271

Remove those lines and change them to: await self.async_scheduled_update_request()

starkillerOG commented 2 years ago

Where those files are in your system depends on the specific way you installed HomeAssistant. Core, OS etc.

wmollenvanger commented 2 years ago

It's a self hosted docker container; did the change, restarted, and: Unable to prepare setup for platform motion_blinds.cover: Platform not found (Exception importing homeassistant.components.motion_blinds.cover). 17:31:34 – (FOUT) setup.py Unexpected exception importing platform homeassistant.components.motion_blinds.cover 17:31:34 – (FOUT) loader.py

Need to import something?

starkillerOG commented 2 years ago

@wmollenvanger that is probably because the permission changed once you edited the cover.py file. reset the permission using: sudo chmod a+rwx homeassistant/components/motion_blinds/cover.py (the path may be diffrent in your enviroment)

wmollenvanger commented 2 years ago

got it working again with the change but there's no change in behavior, updates every 5 seconds and the first after about 5 seconds. Did restart HA and also the whole container.

Changed the update interval to 1 second and now I get events about every 2 (?) seconds and the first one has the correct user_id.

wmollenvanger commented 2 years ago

Think the change didn't work because at that time there's no real change in the position yet.

I experimented with some intervals and 3 seconds or lower seems to be the interval where I get the user id in, at least, the first event.

starkillerOG commented 2 years ago

@wmollenvanger you are probablly right. Unfortunately, lowering the update intervall to 3 seconds is not something I would like to do (due to battery live).

wmollenvanger commented 2 years ago

How hard is it to make the interval an UI configurable value? (default 5 and some text about battery life)

starkillerOG commented 2 years ago

@wmollenvanger well it is not so hard to make it configurable, but it is against the HomeAssistant standards, so a PR implementing that will not be accepted by the HomeAssistant core team.