Move counter service registration to services module (#184281)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
epenet
2026-10-05 09:52:25 +02:00
committed by GitHub
co-authored by Claude Opus 5
parent 39ce471443
commit 0ac3709d3b
3 changed files with 61 additions and 17 deletions
+15 -17
View File
@@ -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
+18
View File
@@ -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):
@@ -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",
)