Open DarwinsBuddy opened 1 week ago
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 86.22%. Comparing base (
119ba21
) to head (c038a71
).
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
🚨 Try these New Features:
Wow very nice work! :) Let me get on to testing this for a couple days, with all the different data quirks we know can happen and get back to you!
Nice! Unfortunately, I'm very short on time right now and probably cannot do a full review in the next weeks :( Just one question: Does it now handle states + statistics in a sane way, i.e., is this now supported by HA? Back then, when we decided to have two sensors, we had the problem that having a state would result in broken statistics.
@reox yes, I guess so. Since we do not necessarily need an explicit sensor to save something with recorder. We just have to use the domain of our integration (wnsm
) and update the stats there on update of our sensor.
This means that we have 2 histories produced.
WNSMSensor
(which is a) showing the last meter reading from the API and b) producing for each sucessfull update a datapoint in its respective recorder stream under sensor.atXXXXXXXX
)wnsm:atXXXXXXXXXX
So only one sensor, but two histories, hence no need for a separate sensor that shows no status whatsoever and would conflict with the dates imported (since they wrote into the same recorder stream)
@reox @tschoerk Any update on your findings? I'd like to release a new version containing a fix for #279 and considering adding all of this to it (iff there is no second thoughts from your side)
@DarwinsBuddy sorry, still hadn't had the time to look at the code properly :( But I guess it's fine? Not having the two entities sounds like a good idea!
Sorry for getting back so late. The API seemed to have a lot of problems in the last week, since I got a ton of error messages regarding server problems (Could not obtain API, No response, etc.).
What I did notice though is that the update of data takes a lot longer than the old method. I get the home assistant warning Update of sensor is taking over 10 seconds
every hour for each sensor. I looked through the code and noticed two things which probably could be improved upon:
Both could save a ton of API calls which would speed up the process and lead to fewer errors overall (maybe some server errors I got were due to rate limiting, but I can't test that).
Regarding the NACHTSTROM fix, good idea with the error message just showing the label, so we can fix it faster in the future.
For both updates the whole login process is done twice. So it logins for the meter readings and afterwards another login for the statistics update, although they happen basically in the same process.
I remember that the login gives a token and a refresh token. I believe it should be possible to store both and call the refresh every now and then, to not run the full login process again. I think last time we discussed that, the problem was that there was no good way to store such information in home assistant?
For both updates the whole login process is done twice. So it logins for the meter readings and afterwards another login for the statistics update, although they happen basically in the same process.
I remember that the login gives a token and a refresh token. I believe it should be possible to store both and call the refresh every now and then, to not run the full login process again. I think last time we discussed that, the problem was that there was no good way to store such information in home assistant?
Yeah I read that discussion and I think they are only valid for 30 minutes and would need a seperate process to refresh, which is not possible in HA (at least at the time of your discussion).
In this case I would just use the tokens which were already provided from the login in the meter reading update and pass the AsyncSmartmeter object to the importer. I will comment on the lines, so you know what I mean.
Thank you so much for your feedback, That's very insightful. I missed these points as I refactored. As soon as I have time I'll try to implement the mentioned improvements.
Hi @tschoerk and @reox
I invested some time to refactor the sensors into one:
WNSMSensor
It's basically the live sensor, but it's calling additionally animporter
, which bears the logic fromStatisticsSensor
. I used following integration (included in the core integrations) as a blueprint: ElviaThe statistics sensor I sunset and marked deprecated it. After releasing this I'd properly document this in the docs and the release notes and would suggest deleting the statistics sensor i.g. A later release (some time later) would then remove the
StatisticsSensor
from the code base eventually.The historical data is exposed to home assistant differently. Instead of
sensor.atXXXXXXXXXXXXXXX_statistics
it will now be offered underwnsm:atXXXXXXXXXXXXXXX
which is valid according to following linehttps://github.com/home-assistant/core/blob/6f947d2612374cb5870729ddec864e2fd59200ef/homeassistant/components/recorder/statistics.py#L326
Following things are to be considered:
I'd really appreciate your input on this. Be careful, you might have to do a data wipe if you want to switch between this branch and
main