From 9ec6a003e60fa635829bcc776639e20878be83ae Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 7 Oct 2026 08:24:31 +0200 Subject: [PATCH] Move osoenergy service registration to services module (#184628) Co-authored-by: Claude Opus 5.5 --- .../components/osoenergy/__init__.py | 15 ++- .../components/osoenergy/services.py | 95 +++++++++++++++++++ .../components/osoenergy/water_heater.py | 69 +------------- .../components/osoenergy/test_water_heater.py | 2 +- 4 files changed, 112 insertions(+), 69 deletions(-) create mode 100644 homeassistant/components/osoenergy/services.py diff --git a/homeassistant/components/osoenergy/__init__.py b/homeassistant/components/osoenergy/__init__.py index d4e4c881d8d5..c78113b04299 100644 --- a/homeassistant/components/osoenergy/__init__.py +++ b/homeassistant/components/osoenergy/__init__.py @@ -10,7 +10,11 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import aiohttp_client +from homeassistant.helpers import aiohttp_client, config_validation as cv +from homeassistant.helpers.typing import ConfigType + +from .const import DOMAIN +from .services import async_setup_services type OSOEnergyConfigEntry = ConfigEntry[OSOEnergy] @@ -26,6 +30,15 @@ PLATFORM_LOOKUP = { } +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the OSO Energy integration.""" + async_setup_services(hass) + return True + + async def async_setup_entry(hass: HomeAssistant, entry: OSOEnergyConfigEntry) -> bool: """Set up OSO Energy from a config entry.""" subscription_key = entry.data[CONF_API_KEY] diff --git a/homeassistant/components/osoenergy/services.py b/homeassistant/components/osoenergy/services.py new file mode 100644 index 000000000000..121eda03cdac --- /dev/null +++ b/homeassistant/components/osoenergy/services.py @@ -0,0 +1,95 @@ +"""Services for the OSO Energy integration.""" + +import probatio + +from homeassistant.components.water_heater import DOMAIN as WATER_HEATER_DOMAIN +from homeassistant.const import SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.core import HomeAssistant, SupportsResponse, callback +from homeassistant.helpers import config_validation as cv, service + +from .const import DOMAIN + +ATTR_DURATION_DAYS = "duration_days" +ATTR_UNTIL_TEMP_LIMIT = "until_temp_limit" +ATTR_V40MIN = "v40_min" +SERVICE_GET_PROFILE = "get_profile" +SERVICE_SET_PROFILE = "set_profile" +SERVICE_SET_V40MIN = "set_v40_min" +SERVICE_TURN_AWAY_MODE_ON = "turn_away_mode_on" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the OSO Energy integration.""" + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_GET_PROFILE, + entity_domain=WATER_HEATER_DOMAIN, + schema={}, + func="async_get_profile", + supports_response=SupportsResponse.ONLY, + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_TURN_AWAY_MODE_ON, + entity_domain=WATER_HEATER_DOMAIN, + schema={ + probatio.Required(ATTR_DURATION_DAYS): probatio.All( + probatio.Coerce(int), probatio.Range(min=1, max=365) + ), + }, + func="async_oso_turn_away_mode_on", + ) + + service_set_profile_schema = cv.make_entity_service_schema( + { + probatio.Optional(f"hour_{hour:02d}"): probatio.All( + probatio.Coerce(int), probatio.Range(min=10, max=75) + ) + for hour in range(24) + } + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_PROFILE, + entity_domain=WATER_HEATER_DOMAIN, + schema=service_set_profile_schema, + func="async_set_profile", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_V40MIN, + entity_domain=WATER_HEATER_DOMAIN, + schema={ + probatio.Required(ATTR_V40MIN): probatio.All( + probatio.Coerce(float), probatio.Range(min=200, max=550) + ), + }, + func="async_set_v40_min", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_TURN_OFF, + entity_domain=WATER_HEATER_DOMAIN, + schema={probatio.Required(ATTR_UNTIL_TEMP_LIMIT): probatio.All(cv.boolean)}, + func="async_oso_turn_off", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_TURN_ON, + entity_domain=WATER_HEATER_DOMAIN, + schema={probatio.Required(ATTR_UNTIL_TEMP_LIMIT): probatio.All(cv.boolean)}, + func="async_oso_turn_on", + ) diff --git a/homeassistant/components/osoenergy/water_heater.py b/homeassistant/components/osoenergy/water_heater.py index 56af861b0e81..db6157e85db8 100644 --- a/homeassistant/components/osoenergy/water_heater.py +++ b/homeassistant/components/osoenergy/water_heater.py @@ -5,7 +5,6 @@ from typing import Any, override from apyosoenergyapi import OSOEnergy from apyosoenergyapi.helper.const import OSOEnergyWaterHeaterData -import probatio from homeassistant.components.water_heater import ( STATE_ECO, @@ -15,9 +14,8 @@ from homeassistant.components.water_heater import ( WaterHeaterEntity, WaterHeaterEntityFeature, ) -from homeassistant.const import SERVICE_TURN_OFF, SERVICE_TURN_ON, UnitOfTemperature -from homeassistant.core import HomeAssistant, ServiceResponse, SupportsResponse -from homeassistant.helpers import config_validation as cv, entity_platform +from homeassistant.const import UnitOfTemperature +from homeassistant.core import HomeAssistant, ServiceResponse from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util import dt as dt_util from homeassistant.util.json import JsonValueType @@ -25,9 +23,6 @@ from homeassistant.util.json import JsonValueType from . import OSOEnergyConfigEntry from .entity import OSOEnergyEntity -ATTR_DURATION_DAYS = "duration_days" -ATTR_UNTIL_TEMP_LIMIT = "until_temp_limit" -ATTR_V40MIN = "v40_min" CURRENT_OPERATION_MAP: dict[str, Any] = { "default": { "off": STATE_OFF, @@ -41,10 +36,6 @@ CURRENT_OPERATION_MAP: dict[str, Any] = { "extraenergy": STATE_HIGH_DEMAND, }, } -SERVICE_GET_PROFILE = "get_profile" -SERVICE_SET_PROFILE = "set_profile" -SERVICE_SET_V40MIN = "set_v40_min" -SERVICE_TURN_AWAY_MODE_ON = "turn_away_mode_on" async def async_setup_entry( @@ -59,62 +50,6 @@ async def async_setup_entry( return async_add_entities((OSOEnergyWaterHeater(osoenergy, dev) for dev in devices), True) - platform = entity_platform.async_get_current_platform() - - platform.async_register_entity_service( - SERVICE_GET_PROFILE, - {}, - OSOEnergyWaterHeater.async_get_profile.__name__, - supports_response=SupportsResponse.ONLY, - ) - - platform.async_register_entity_service( - SERVICE_TURN_AWAY_MODE_ON, - { - probatio.Required(ATTR_DURATION_DAYS): probatio.All( - probatio.Coerce(int), probatio.Range(min=1, max=365) - ), - }, - OSOEnergyWaterHeater.async_oso_turn_away_mode_on.__name__, - ) - - service_set_profile_schema = cv.make_entity_service_schema( - { - probatio.Optional(f"hour_{hour:02d}"): probatio.All( - probatio.Coerce(int), probatio.Range(min=10, max=75) - ) - for hour in range(24) - } - ) - - platform.async_register_entity_service( - SERVICE_SET_PROFILE, - service_set_profile_schema, - OSOEnergyWaterHeater.async_set_profile.__name__, - ) - - platform.async_register_entity_service( - SERVICE_SET_V40MIN, - { - probatio.Required(ATTR_V40MIN): probatio.All( - probatio.Coerce(float), probatio.Range(min=200, max=550) - ), - }, - OSOEnergyWaterHeater.async_set_v40_min.__name__, - ) - - platform.async_register_entity_service( - SERVICE_TURN_OFF, - {probatio.Required(ATTR_UNTIL_TEMP_LIMIT): probatio.All(cv.boolean)}, - OSOEnergyWaterHeater.async_oso_turn_off.__name__, - ) - - platform.async_register_entity_service( - SERVICE_TURN_ON, - {probatio.Required(ATTR_UNTIL_TEMP_LIMIT): probatio.All(cv.boolean)}, - OSOEnergyWaterHeater.async_oso_turn_on.__name__, - ) - def _get_utc_hour(local_hour: int) -> dt.datetime: """Convert the requested local hour to a utc hour for the day. diff --git a/tests/components/osoenergy/test_water_heater.py b/tests/components/osoenergy/test_water_heater.py index 772f95bfcc97..60e9ba12f6a6 100644 --- a/tests/components/osoenergy/test_water_heater.py +++ b/tests/components/osoenergy/test_water_heater.py @@ -6,7 +6,7 @@ import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.osoenergy.const import DOMAIN -from homeassistant.components.osoenergy.water_heater import ( +from homeassistant.components.osoenergy.services import ( ATTR_DURATION_DAYS, ATTR_UNTIL_TEMP_LIMIT, ATTR_V40MIN,