From a99aad86a82951f6944d0ec6dc2fc65a5742dfef Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 9 Apr 2024 18:25:03 +0200 Subject: [PATCH] Make AbstractSpookEntityService and AbstractSpookEntityComponentService generic --- .../ectoplasms/input_number/services/decrement.py | 4 +++- .../ectoplasms/input_number/services/increment.py | 4 +++- .../spook/ectoplasms/input_number/services/max.py | 8 ++++++-- .../spook/ectoplasms/input_number/services/min.py | 8 ++++++-- .../ectoplasms/input_select/services/shuffle.py | 4 ++-- .../ectoplasms/input_select/services/sort.py | 4 ++-- .../spook/ectoplasms/number/services/decrement.py | 2 +- .../spook/ectoplasms/number/services/increment.py | 2 +- .../spook/ectoplasms/number/services/max.py | 8 ++++++-- .../spook/ectoplasms/number/services/min.py | 8 ++++++-- .../spook/ectoplasms/select/services/random.py | 2 +- .../ectoplasms/timer/services/set_duration.py | 2 +- custom_components/spook/services.py | 15 +++++++++------ 13 files changed, 47 insertions(+), 24 deletions(-) diff --git a/custom_components/spook/ectoplasms/input_number/services/decrement.py b/custom_components/spook/ectoplasms/input_number/services/decrement.py index 99d2154e..215f52fe 100644 --- a/custom_components/spook/ectoplasms/input_number/services/decrement.py +++ b/custom_components/spook/ectoplasms/input_number/services/decrement.py @@ -14,7 +14,9 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService, ReplaceExistingService): +class SpookService( + AbstractSpookEntityComponentService[InputNumber], ReplaceExistingService +): """Input number entity service, decrease value by a single step. It override the built-in increment service to allow for a custom amount. diff --git a/custom_components/spook/ectoplasms/input_number/services/increment.py b/custom_components/spook/ectoplasms/input_number/services/increment.py index 9a8b271d..3aee71b7 100644 --- a/custom_components/spook/ectoplasms/input_number/services/increment.py +++ b/custom_components/spook/ectoplasms/input_number/services/increment.py @@ -14,7 +14,9 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService, ReplaceExistingService): +class SpookService( + AbstractSpookEntityComponentService[InputNumber], ReplaceExistingService +): """Input number entity service, increase value by a single step. It override the built-in increment service to allow for a custom amount. diff --git a/custom_components/spook/ectoplasms/input_number/services/max.py b/custom_components/spook/ectoplasms/input_number/services/max.py index b844dcc0..6cd8bab1 100644 --- a/custom_components/spook/ectoplasms/input_number/services/max.py +++ b/custom_components/spook/ectoplasms/input_number/services/max.py @@ -12,13 +12,17 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[InputNumber]): """Input number entity service, set the max value.""" domain = DOMAIN service = "max" - async def async_handle_service(self, entity: InputNumber, _: ServiceCall) -> None: + async def async_handle_service( + self, + entity: InputNumber, + call: ServiceCall, # noqa: ARG002 + ) -> None: """Handle the service call.""" # pylint: disable-next=protected-access await entity.async_set_value(entity._maximum) # noqa: SLF001 diff --git a/custom_components/spook/ectoplasms/input_number/services/min.py b/custom_components/spook/ectoplasms/input_number/services/min.py index 2fcc415b..4d323790 100644 --- a/custom_components/spook/ectoplasms/input_number/services/min.py +++ b/custom_components/spook/ectoplasms/input_number/services/min.py @@ -12,13 +12,17 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[InputNumber]): """Input number entity service, set the min value.""" domain = DOMAIN service = "min" - async def async_handle_service(self, entity: InputNumber, _: ServiceCall) -> None: + async def async_handle_service( + self, + entity: InputNumber, + call: ServiceCall, # noqa: ARG002 + ) -> None: """Handle the service call.""" # pylint: disable-next=protected-access await entity.async_set_value(entity._minimum) # noqa: SLF001 diff --git a/custom_components/spook/ectoplasms/input_select/services/shuffle.py b/custom_components/spook/ectoplasms/input_select/services/shuffle.py index 47b0b0a2..d39ff103 100644 --- a/custom_components/spook/ectoplasms/input_select/services/shuffle.py +++ b/custom_components/spook/ectoplasms/input_select/services/shuffle.py @@ -13,7 +13,7 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[InputSelect]): """Input select entity service, shuffling the positions. These changes are not permanent, and will be lost when input select entities @@ -26,7 +26,7 @@ class SpookService(AbstractSpookEntityComponentService): async def async_handle_service( self, entity: InputSelect, - _call: ServiceCall, + call: ServiceCall, # noqa: ARG002 ) -> None: """Handle the service call.""" # pylint: disable-next=protected-access diff --git a/custom_components/spook/ectoplasms/input_select/services/sort.py b/custom_components/spook/ectoplasms/input_select/services/sort.py index 7b7e2daa..9d7e31e6 100644 --- a/custom_components/spook/ectoplasms/input_select/services/sort.py +++ b/custom_components/spook/ectoplasms/input_select/services/sort.py @@ -12,7 +12,7 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[InputSelect]): """Input select entity service, sorting the positions. These changes are not permanent, and will be lost when input select entities @@ -25,7 +25,7 @@ class SpookService(AbstractSpookEntityComponentService): async def async_handle_service( self, entity: InputSelect, - _call: ServiceCall, + call: ServiceCall, # noqa: ARG002 ) -> None: """Handle the service call.""" # pylint: disable-next=protected-access diff --git a/custom_components/spook/ectoplasms/number/services/decrement.py b/custom_components/spook/ectoplasms/number/services/decrement.py index 327dedb1..9fc57115 100644 --- a/custom_components/spook/ectoplasms/number/services/decrement.py +++ b/custom_components/spook/ectoplasms/number/services/decrement.py @@ -14,7 +14,7 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[NumberEntity]): """Number entity service, decrease value by a single step.""" domain = DOMAIN diff --git a/custom_components/spook/ectoplasms/number/services/increment.py b/custom_components/spook/ectoplasms/number/services/increment.py index 73138d94..6a7aa2b4 100644 --- a/custom_components/spook/ectoplasms/number/services/increment.py +++ b/custom_components/spook/ectoplasms/number/services/increment.py @@ -14,7 +14,7 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[NumberEntity]): """Number entity service, increase value by a single step.""" domain = DOMAIN diff --git a/custom_components/spook/ectoplasms/number/services/max.py b/custom_components/spook/ectoplasms/number/services/max.py index 8d4f32da..cfa1fcea 100644 --- a/custom_components/spook/ectoplasms/number/services/max.py +++ b/custom_components/spook/ectoplasms/number/services/max.py @@ -13,13 +13,17 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[NumberEntity]): """Number entity service, set the max value.""" domain = DOMAIN service = "max" - async def async_handle_service(self, entity: NumberEntity, _: ServiceCall) -> None: + async def async_handle_service( + self, + entity: NumberEntity, + call: ServiceCall, # noqa: ARG002 + ) -> None: """Handle the service call.""" if entity.max_value is None: msg = f"Entity {entity.entity_id} has no max value" diff --git a/custom_components/spook/ectoplasms/number/services/min.py b/custom_components/spook/ectoplasms/number/services/min.py index 01a4fcc0..f3398d74 100644 --- a/custom_components/spook/ectoplasms/number/services/min.py +++ b/custom_components/spook/ectoplasms/number/services/min.py @@ -13,13 +13,17 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[NumberEntity]): """Number entity service, set the min value.""" domain = DOMAIN service = "min" - async def async_handle_service(self, entity: NumberEntity, _: ServiceCall) -> None: + async def async_handle_service( + self, + entity: NumberEntity, + call: ServiceCall, # noqa: ARG002 + ) -> None: """Handle the service call.""" if entity.min_value is None: msg = f"Entity {entity.entity_id} has no min value" diff --git a/custom_components/spook/ectoplasms/select/services/random.py b/custom_components/spook/ectoplasms/select/services/random.py index bc290ada..9eb24701 100644 --- a/custom_components/spook/ectoplasms/select/services/random.py +++ b/custom_components/spook/ectoplasms/select/services/random.py @@ -16,7 +16,7 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityComponentService): +class SpookService(AbstractSpookEntityComponentService[SelectEntity]): """Select entity service, select a random option.""" domain = DOMAIN diff --git a/custom_components/spook/ectoplasms/timer/services/set_duration.py b/custom_components/spook/ectoplasms/timer/services/set_duration.py index 9b8b3a20..45953c28 100644 --- a/custom_components/spook/ectoplasms/timer/services/set_duration.py +++ b/custom_components/spook/ectoplasms/timer/services/set_duration.py @@ -23,7 +23,7 @@ from homeassistant.core import ServiceCall -class SpookService(AbstractSpookEntityService): +class SpookService(AbstractSpookEntityService[Timer]): """Home Assistant service to set duration for a timer.""" domain = DOMAIN diff --git a/custom_components/spook/services.py b/custom_components/spook/services.py index 9099585e..dbd3352f 100644 --- a/custom_components/spook/services.py +++ b/custom_components/spook/services.py @@ -6,8 +6,9 @@ from dataclasses import dataclass, field import importlib from pathlib import Path -from typing import TYPE_CHECKING, Any, cast, final +from typing import TYPE_CHECKING, Any, Generic, cast, final +from typing_extensions import TypeVar import voluptuous as vol from homeassistant.core import ( @@ -18,6 +19,7 @@ SupportsResponse, callback, ) +from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_component import DATA_INSTANCES, EntityComponent from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM from homeassistant.helpers.service import ( @@ -33,7 +35,8 @@ if TYPE_CHECKING: from types import ModuleType - from homeassistant.helpers.entity import Entity + +_EntityT = TypeVar("_EntityT", bound=Entity, default=Entity) class AbstractSpookServiceBase(ABC): @@ -147,7 +150,7 @@ async def async_handle_service(self, call: ServiceCall) -> None: raise NotImplementedError -class AbstractSpookEntityService(AbstractSpookServiceBase): +class AbstractSpookEntityService(AbstractSpookServiceBase, Generic[_EntityT]): """Abstract class to hold a Spook entity service.""" platform: str @@ -190,14 +193,14 @@ def async_register(self) -> None: @abstractmethod async def async_handle_service( self, - entity: Entity, + entity: _EntityT, call: ServiceCall, ) -> ServiceResponse: """Handle the service call.""" raise NotImplementedError -class AbstractSpookEntityComponentService(AbstractSpookServiceBase): +class AbstractSpookEntityComponentService(AbstractSpookServiceBase, Generic[_EntityT]): """Abstract class to hold a Spook entity component service.""" required_features: list[int] | None = None @@ -233,7 +236,7 @@ def async_register(self) -> None: @abstractmethod async def async_handle_service( self, - entity: Entity, + entity: _EntityT, call: ServiceCall, ) -> ServiceResponse: """Handle the service call."""