From 0ac3709d3bac32f4afdbc35ed97c74c33d17cb2c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 5 Oct 2026 09:52:25 +0200 Subject: [PATCH] Move counter service registration to services module (#184281) Co-authored-by: Claude Opus 5 --- homeassistant/components/counter/__init__.py | 32 +++++++++----------- homeassistant/components/counter/const.py | 18 +++++++++++ homeassistant/components/counter/services.py | 28 +++++++++++++++++ 3 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 homeassistant/components/counter/services.py diff --git a/homeassistant/components/counter/__init__.py b/homeassistant/components/counter/__init__.py index 1b26a32a3de6..16e307c526ee 100644 --- a/homeassistant/components/counter/__init__.py +++ b/homeassistant/components/counter/__init__.py @@ -19,7 +19,17 @@ from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType, VolDictType -from .const import CounterEntityStateAttribute +from .const import ( # noqa: F401 + DATA_COMPONENT, + DOMAIN, + SERVICE_DECREMENT, + SERVICE_INCREMENT, + SERVICE_RESET, + SERVICE_SET_VALUE, + VALUE, + CounterEntityStateAttribute, +) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -27,7 +37,6 @@ ATTR_INITIAL = "initial" ATTR_STEP = "step" ATTR_MINIMUM = "minimum" ATTR_MAXIMUM = "maximum" -VALUE = "value" CONF_INITIAL = "initial" CONF_RESTORE = "restore" @@ -35,14 +44,9 @@ CONF_STEP = "step" DEFAULT_INITIAL = 0 DEFAULT_STEP = 1 -DOMAIN = "counter" ENTITY_ID_FORMAT = DOMAIN + ".{}" -SERVICE_DECREMENT = "decrement" -SERVICE_INCREMENT = "increment" -SERVICE_RESET = "reset" -SERVICE_SET_VALUE = "set_value" STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 @@ -97,7 +101,9 @@ CONFIG_SCHEMA = probatio.Schema( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the counters.""" - component = EntityComponent[Counter](_LOGGER, DOMAIN, hass) + component = hass.data[DATA_COMPONENT] = EntityComponent[Counter]( + _LOGGER, DOMAIN, hass + ) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( @@ -124,15 +130,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS ).async_setup(hass) - component.async_register_entity_service(SERVICE_INCREMENT, None, "async_increment") - component.async_register_entity_service(SERVICE_DECREMENT, None, "async_decrement") - component.async_register_entity_service(SERVICE_RESET, None, "async_reset") - component.async_register_entity_service( - SERVICE_SET_VALUE, - {probatio.Required(VALUE): probatio.Coerce(int)}, - "async_set_value", - ) - + async_setup_services(hass) return True diff --git a/homeassistant/components/counter/const.py b/homeassistant/components/counter/const.py index 5d7b14e91fdf..b7d2ba9f83ec 100644 --- a/homeassistant/components/counter/const.py +++ b/homeassistant/components/counter/const.py @@ -1,6 +1,24 @@ """Constants for the counter integration.""" from enum import StrEnum +from typing import TYPE_CHECKING, Final + +from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from . import Counter + +DOMAIN: Final = "counter" + +DATA_COMPONENT: HassKey[EntityComponent[Counter]] = HassKey(DOMAIN) + +VALUE: Final = "value" + +SERVICE_DECREMENT: Final = "decrement" +SERVICE_INCREMENT: Final = "increment" +SERVICE_RESET: Final = "reset" +SERVICE_SET_VALUE: Final = "set_value" class CounterEntityStateAttribute(StrEnum): diff --git a/homeassistant/components/counter/services.py b/homeassistant/components/counter/services.py new file mode 100644 index 000000000000..1e1a1a8375e9 --- /dev/null +++ b/homeassistant/components/counter/services.py @@ -0,0 +1,28 @@ +"""Services for the counter integration.""" + +import probatio + +from homeassistant.core import HomeAssistant, callback + +from .const import ( + DATA_COMPONENT, + SERVICE_DECREMENT, + SERVICE_INCREMENT, + SERVICE_RESET, + SERVICE_SET_VALUE, + VALUE, +) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the counter services.""" + component = hass.data[DATA_COMPONENT] + component.async_register_entity_service(SERVICE_INCREMENT, None, "async_increment") + component.async_register_entity_service(SERVICE_DECREMENT, None, "async_decrement") + component.async_register_entity_service(SERVICE_RESET, None, "async_reset") + component.async_register_entity_service( + SERVICE_SET_VALUE, + {probatio.Required(VALUE): probatio.Coerce(int)}, + "async_set_value", + )