test: add unit tests for ZaptecUpdateCoordinator and ZaptecBaseEntity - #394
test: add unit tests for ZaptecUpdateCoordinator and ZaptecBaseEntity#394rhammen wants to merge 12 commits into
Conversation
Covers _trigger_poll's per-object delay/refresh sequencing (charger vs installation), the installation-triggers-tracked-children fan-out and its untracked-child skip path, trigger_poll's no-op when there is no zaptec_object, and the cancel-in-flight-task-before-starting-new-one race. The final test needed two asyncio.sleep(0) yields rather than one: the first lets the replacement task run to completion, the second lets its add_done_callback (which clears _trigger_task) actually fire, since Task done-callbacks are scheduled via call_soon rather than invoked synchronously on completion. Verified deterministic across 45+ runs.
…n chaining, dead code, docstrings)
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Keep planning docs local-only rather than publishing them to the PR branch, matching the convention used on other in-flight PRs.
sveinse
left a comment
There was a problem hiding this comment.
See the comments for adjustments.
Making unittests for testing HA functionality like coordinator and entities is something they must have a lot of in HA. Have you checked what testing facilities exists in HA that can assist us? How does other components do this type of testing? (And I would look at gold or platinum components in HA core for inspiration.)
I'd also like @steinmn comments on the PR if that's possible. You have a different set of eyes than me.
| `async_create_background_task`). A real ConfigEntry pulls in HA's full | ||
| test-harness machinery, which cannot run on native Windows in this dev | ||
| environment - see CLAUDE.md's environment notes. |
There was a problem hiding this comment.
I'm not sure about this comment. First about mentioning Windows and second about CLAUDE.md which isn't part of this project.
| # NOTE: this sets _attr_available, but ZaptecBaseEntity does not override | ||
| # the `available` property inherited from HA's CoordinatorEntity (which | ||
| # returns coordinator.last_update_success instead), so this flag currently | ||
| # has no effect on the entity's actual reported availability. This test | ||
| # documents today's real behavior, not the intended one - see the "Known | ||
| # finding" note at the top of this plan. |
There was a problem hiding this comment.
Is this something that should be reported as a bug?
The text refers to "known finding" note at the top of this plan. What plan?
test: address review comments on conftest/entity docstrings - conftest.py: justify FakeConfigEntry on technical grounds (avoids pytest-homeassistant-custom-component) instead of referencing the native-Windows dev env and CLAUDE.md, which are not part of this repo. - test_entity.py: point the availability-finding NOTE at issue custom-components#410 instead of a removed plan doc. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> @
|
Thanks — fair point, and having dug into it I agree the current approach doesn't really match how gold/platinum HA integrations test. Those integrations lean on The original reason for the hand-rolled mocks was that the real harness didn't run on my native-Windows dev setup ( As a concrete example of why the harness is worth it: the So rather than merge this as-is, I'm going to hold this PR as a draft and open a replacement built on the |
I think I mentioned it on a different PR, not sure which: Have you tried working with the dev-container defined by .devcontainer.json? This is exactly the kind of dev-setup-issues this was created to handle. As long as you have docker running on WSL2 and your repo download location is on the wsl-filesystem (see https://endjin.com/blog/supercharge-dev-containers-on-windows for details on why), you can get just as good performance running in a dev-container as running locally. At my day-job, we recently even managed to reduce the build-time of a c-project by 80% by moving from a native Windows pipeline to a dev-container running on WSL2. |
Yes, I have setup the DEVcontainer this weekend and it is working. |
|
Closing this PR. |
…omponents#300) Standalone version of custom-components#396, rebased onto master without the custom-components#394/custom-components#395 test-coverage dependencies. Imports Zaptec's archived charge-session history (/api/sessions/archived) into Home Assistant long-term statistics as an external statistic (zaptec:energy_<id>), fixing the Energy Dashboard hour-misattribution described in custom-components#300 (and custom-components#162). Squashed from the 17 energy-statistics commits (a62dc20..b103473) of custom-components#396. Keeps the self-contained tests (bucket_sessions_hourly / _floor_hour and the archived-sessions API client); omits the ZaptecStatisticsCoordinator async tests and test_manager.py, which depend on fixtures introduced by the (separate, being-redesigned) custom-components#394/custom-components#395, plus the internal implementation-plan doc. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Summary
custom_components/zaptec/coordinator.py(ZaptecUpdateCoordinator) andcustom_components/zaptec/entity.py(ZaptecBaseEntity), raising coverage on these files from ~27-29% to 100% and 98% respectively (the one remaining line inentity.pyis a genuinely unreachable defensive fallback).ConfigEntryinternals), and the standardpytest-homeassistant-custom-componentharness is not viable on native Windows dev setups, tests use small hand-rolled test doubles instead: aMagicMock-based fakehasswith a real running event loop attached as.loop, and a minimalFakeConfigEntryclass (both intests/conftest.py). This keeps the tests fast, dependency-free, and runnable everywherepytestruns.entity.py:_handle_coordinator_updatesetsself._attr_available = Falseon aKeyUnavailableError, butZaptecBaseEntitynever overrides HA'sCoordinatorEntity.availableproperty, so that flag currently has no effect on the entity's actual reported availability. Left as a documented finding for a separate follow-up, out of scope here.Test plan
pytest tests/test_coordinator.py tests/test_entity.py -v— 35 passedpytest tests -q— 45 passed, 2 skipped, 22 errors (pre-existing, unrelated DNS-fixture gap intest_zconst.py/test_redact.py— see repo's dev notes)ruff format --diff/ruff checkclean on all touched filescustom_components/**production files modified — test-only change🤖 Generated with Claude Code