refactor: introduce typed SceneAccessor and TimerAccessor#85
Merged
Conversation
- Add `accessor_cls` field to `DomainSpec` so domains can declare a typed `DomainAccessor` subclass instead of relying on dynamic `setattr`-based operation binding. - Expose `DomainAccessor.factory` as a public property so subclasses can access services/state without reaching into `_factory` private attribute. - `HAClient` now instantiates `spec.accessor_cls` when set, falling back to the base `DomainAccessor` for domains without one. - Replace scene domain's dynamic `_create`/`_apply` functions with a `SceneAccessor` subclass that exposes properly typed `create()` and `apply()` methods. - Replace timer domain's dynamic `_create` function with a `TimerAccessor` subclass that exposes a properly typed `create()` method. - Remove all `# type: ignore[assignment]` comments from domain code; mypy now passes cleanly with no suppressions. - Add six new tests covering the `factory` property, typed accessor instantiation, and the `accessor_cls` dispatch path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #76
Issue validity
Valid and actionable. The issue correctly identifies that:
DomainSpec.operations(dict[str, Callable[..., Any]]) and dynamicsetattrbinding make collection-level domain operations invisible to type checkers.scene.pyandtimer.pyworked around this by accessing the privateaccessor._factorywith# type: ignore[assignment], bypassing encapsulation.py.typedand runsmypyin strict mode, so this is a genuine gap.Fix
core/plugins.pyaccessor_cls: type[DomainAccessor[Any]] | None = Nonefield toDomainSpec. Domains with collection-level operations declare their typed accessor subclass here instead of populatingoperations.DomainAccessor.factoryas a public property returning theEntityFactoryProtocol. Subclasses use this to reachfactory.servicesandfactory.statewithout touching_factory.api.pyHAClient.__init__now instantiatesspec.accessor_clswhen set, falling back to the baseDomainAccessorfor domains that don't need collection-level operations.domains/scene.py_create/_apply+operations={...}with aSceneAccessor(DomainAccessor[Scene])subclass exposing typedasync def create(...) -> Sceneandasync def apply(...) -> Nonemethods.DomainSpecnow usesaccessor_cls=SceneAccessor.domains/timer.py_create+operations={...}with aTimerAccessor(DomainAccessor[Timer])subclass exposing a typedasync def create(...) -> Timermethod.DomainSpecnow usesaccessor_cls=TimerAccessor.# type: ignore[assignment]comments removed.Tests added (
tests/test_plugins.py)test_domain_accessor_factory_propertyDomainAccessor.factoryreturns anEntityFactoryinstancetest_scene_accessor_is_typed_subclassha.sceneis aSceneAccessorwith callablecreateandapplytest_timer_accessor_is_typed_subclassha.timeris aTimerAccessorwith a callablecreatetest_accessor_cls_in_spec_is_usedaccessor_clson a spec is correctly instantiatedtest_accessor_cls_none_falls_back_to_baseaccessor_clsget the baseDomainAccessorValidation
pytest tests/ --cov=haclient --cov-fail-under=95: 316 passed, coverage 97.11%ruff check src tests: All checks passedruff format --check src tests: 57 files already formattedmypy src: Success: no issues found in 38 source files