-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cover fake position persistent #213
Conversation
@postlund , I tried to implement what we agreed in issue #175. I need your review of the code, in particular for 2 aspects:
|
@rospogrigio I don't have time to do a full review now, can do that later. But doing I/O like that is a problem. Home Assistant can already save previous state for you using https://aarongodfrey.dev/programming/restoring-an-entity-in-home-assistant/ |
@postlund thank you, I read the link you posted but I can't even inherit the RestoreEntity class, so I'll wait for your support. Also, I don't know whether we should inherit LocaltuyaCover or the LocaltuyaEntity... will wait for your advice when you have time, thank you |
Just to make sure I test correctly. Do I need to completely UNinstall, REinstall (or even change the repo in Hacs?) local_tuya? Is it still needed to half the opening time in config (my cover opening is 30 secs, but in previous config I had to input 15)? |
It should be fine to just replace the folder (actually, only cover.py has changed so you could just replace that one). |
@postlund OK I think I have understood how it works, get ready for a review... 😉 |
@postlund OK I tried it and it seems to work partially. It is working if I restart HA, but if I reboot the Raspberry it loses the stored status.
if this is the case, then we'd better make asyncio-safe the storing on a file, since for example I reboot the host once a day (I actually leave it off overnight). Your thoughts? Thank you |
Hi @postlund I just saw you were back, hope everything is fine... do you have any comment/suggestion about this? Thank you... |
It's not working correctly. -- Clean Hass start --- Status: Slider is at 0% 2 problems: Only when Clicking STOP manually, the slider moves to 0%. Hope this helps, logs available if needed. |
Actually this behavior is correct.
|
Thnx for the explanation, but one thing I don't understand.
But It does know how long it takes to fully open, it is set in the cfg "Full opening time in secs (fake mode only)"?! So why wait for a long 2 minutes timeout, if the max timeout is allready known in de config settings? ps. Not all covers have a timeout, mine does not. It just stops the motor because there's a adjustable microswitch inside. |
I'm surprised: I always tend to think that all devices behave like mine, but I am usually proven to be wrong, as in this case. 😆 |
…out equal to full opening time
@TheDukeSr I've done it, can you please give it a go and report feedback? PLEASE NOTE: I changed the name of the positioning from "fake" to "timed", since now I like this naming more: so please update your YAML file if you are using it, or recreate the Integration if you went with the config flow. Thank you! |
Mmm, I noticed that now you can't stop the movement until it has reached the timeout, need to fix this so please wait... |
@TheDukeSr now it should work as expected, give it a go and report feedback, tranks! |
@rospogrigio Hi, I'll test it tomorow (15-12-2020) and report back here asap. |
@postlund may I ask you a review on this? Also, since as I reported the RestoreEntity is not effective in case the HA server is rebooted, would the I/O operation being invoked with async_create_task (so it is parallelized) be a solution? |
@rospogrigio I'm sorry to say but there's still much going wrong. Isn't there a way to commonucate directly like discord, teamspeak..? (Dutch or English). |
You can send me a private message on https://community.home-assistant.io/ 😉 |
@rospogrigio Sorry, forgot about this one! I don't really accept the fact that there's a difference between a restart of Home Assistant and the system itself. Home Assistant (core) does not make any distinction of that. I had to take a look at the code to see how it works. The states are stored in Creating a new task does not make anything run in parallel, like you would expect with regular multithreading. In asyncio there's only one thread and one thing running at once. So you are never allowed to do potentially blocking I/O in async context as the blocks everything. You need to put that kind of work in an executor ( I however would like to not do this at all, but rather rely on
|
Thank you! Well, to answer your question: I just switch off the system (the smart plug that provides power has a switch-off timing). But still the problem might occur if there is a power cut and the status was not saved... is there any way to force updating |
Nope ;-( New user, so my rights there are limited and can't send messages.... OMG.. |
@rospogrigio Do you turn off Home Assistant by just cutting power?! That is not a good idea. Things like this can certainly happen because of that and it's also a big risk breaking your storage medium. Shut down properly instead. You can always lose data due to an accidental power cut. That's an edge case that we should not solve, it's a problem for Hone Assistant itself. If everyone runs around trying to fix every little detail, it just becomes a mess. We should restore if possible and be robust, providing as good fallback if we can't. But we should not mess with the internals of Home Assistant. |
@postlund Yeah, I was not scared for the storage medium since I only use solid state devices but I agree with you and I moved to a proper shutdown, |
|
||
async def async_added_to_hass(self): | ||
"""Subscribe localtuya events.""" | ||
await super().async_added_to_hass() | ||
|
||
self.debug("Adding %s with configuration: %s", self.entity_id, self._config) | ||
|
||
state = await self.async_get_last_state() | ||
if state: | ||
self._stored_state = state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really have to store it in self._stored_state
, can't we just pass the value as an argument to status_restored
?
custom_components/localtuya/cover.py
Outdated
# for timed positioning, stop the cover after a full opening timespan | ||
# instead of waiting the internal timeout | ||
self.hass.async_create_task( | ||
self.async_stop_after_timeout(self._config[CONF_SPAN_TIME] + 5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic constant: what does 5 mean? Create a constant for it.
custom_components/localtuya/cover.py
Outdated
# for timed positioning, stop the cover after a full opening timespan | ||
# instead of waiting the internal timeout | ||
self.hass.async_create_task( | ||
self.async_stop_after_timeout(self._config[CONF_SPAN_TIME] + 5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
custom_components/localtuya/cover.py
Outdated
stored_pos = int( | ||
self._stored_state.attributes.get("current_position", "-1") | ||
) | ||
if stored_pos != -1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You return the string "-1" if the variable does not exist but compare with an integer here, is that intentional? Maybe just check if not None
instead and skip default value?
@TheDukeSr can you give me some feedbacks about the current implementation? Let me know... |
Hi. I've send you a email.
Op ma 21 dec. 2020 12:41 schreef rospogrigio <notifications@github.com>:
… @TheDukeSr <https://github.com/TheDukeSr> can you give me some feedbacks
about the current implementation? You can write me at
***@***.*** if you don't feel comfortable with the PR
conversation. Let me know...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#213 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJTZSOGY2RBQ4BTELWSVDWDSV4YALANCNFSM4UMAJWAA>
.
|
@postlund I fixed your remarks, can you review this? Then we can merge it and publish a New Year release 😄 , please also send me a summary of the last changes because I lost track... thank you! |
@rospogrigio Will fix later tonight 👍 |
I guess these are the commits of interest:
Simple summary:
There are a few other things related to light and stuff, but I know to little about that. |
* Introduced read/store to JSON file of cover's last position for fake positioning * renamed "fake" positioning to "timed"; introduced timed movement timeout equal to full opening time * Made waiting for timeout for stopping non-blocking * Introduced status storing using RestoreEntity * Fixed postlund's remarks * Updated cover samples in README and info.md
Introduced storing and retrieving of cover last position, in order to make it persistent after rebooting/restarting HA.
It assumes the cover to be fully closed at the first startup, however after a full opening it will be coherent with the actual cover position.
This allows to get rid of the "half range" weird behavior.
Implemented for issue #175, but it should fix also issue #186, and probably #207.