Skip to content
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

fix: Charge time remaining, account for time passing between updates #419

Merged
merged 3 commits into from
Dec 27, 2022

Conversation

InTheDaylight14
Copy link
Contributor

Saves the time when the value changes, subtracts the time that has passed since the last update.

Without this the value grows by 1 minute for every minute since an api update. Then, on api call, the time jumps back down to the proper time.

@alandtse
Copy link
Owner

For this type of nuanced issue, do you think you can write a test so we don't get a regression? Basically, the idea is the test will fail if this fix's behavior is undone.

@InTheDaylight14
Copy link
Contributor Author

Hmm, my first thought on how to test would be to put a delay of 1 minute in and make sure the state has not changed. I'd hate to hold up the all the tests for 1 minute though.

I included this behavior in the Destination arrival time PR. So that could end up with 2 minute delays in the tests.

Is there a way to programmatically change the system time? Or run the tests with delays in a separate thread?

@alandtse
Copy link
Owner

Yes, use libraries like https://pypi.org/project/time-machine/. I don't remember what HA has standardized on though.

@InTheDaylight14 InTheDaylight14 marked this pull request as draft December 12, 2022 18:34
@InTheDaylight14
Copy link
Contributor Author

Moving to a draft for now. This might not be needed if the polling is increased to every 60 seconds while charging. The charge time completion will always be within 60 seconds.

#426

@@ -261,6 +261,15 @@ async def test_time_charge_complete_charging(
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert state.attributes.get(ATTR_STATE_CLASS) is None

mock_timetravel = mock_now + timedelta(minutes=2)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little out of my element here, I want to draw attention to this to make sure this works for testing. Trying to change the time 2 minutes in the future and make sure that the state has stayed the same.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain what the issue is. Is the test failing? I haven't mocked timestamps before so I'd just be reading the documentation like you are and reviewing the HA examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No the test is passing just fine. Maybe @carleeno can review the mock_timetravel. I based it off of their recent PR to shore up time tests. I just want to make sure that the state is actually being evaluated two minutes later by using that monkeypatch.setattr...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@InTheDaylight14 the mock looks correct, however you're not asserting anything afterwards so it's not actually testing anything.

Add an assert state.state == charge_complete_str at the end and see what happens, this will reveal if your logic is good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for an explanation of what the monkeypatch is doing is it's replacing the homeassistant.util.dt.utcnow that HA uses with our mocked time. So when dt.utcnow is called from sensor.py it will actually get your mocked value.

@InTheDaylight14
Copy link
Contributor Author

Oh and it looks like the tests are being skipped in the checks. Not sure what is going on there.

@alandtse alandtse merged commit 02751a2 into alandtse:dev Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants