diff --git a/homeassistant/components/schedule/__init__.py b/homeassistant/components/schedule/__init__.py index deaa69d05967..0b28c5f144d6 100644 --- a/homeassistant/components/schedule/__init__.py +++ b/homeassistant/components/schedule/__init__.py @@ -37,7 +37,6 @@ from .const import ( # noqa: F401 DATA_SCHEDULE, DOMAIN, LOGGER, - SERVICE_GET, WEEKDAY_TO_CONF, ScheduleEntityCapabilityAttribute, ScheduleEntityStateAttribute, diff --git a/homeassistant/components/schedule/const.py b/homeassistant/components/schedule/const.py index eca0fa949c10..9515ff6adfc8 100644 --- a/homeassistant/components/schedule/const.py +++ b/homeassistant/components/schedule/const.py @@ -58,5 +58,3 @@ WEEKDAY_TO_CONF: Final = { 5: CONF_SATURDAY, 6: CONF_SUNDAY, } - -SERVICE_GET: Final = "get_schedule" diff --git a/homeassistant/components/schedule/services.py b/homeassistant/components/schedule/services.py index 6dfdc6e41b2c..3ea735dca7aa 100644 --- a/homeassistant/components/schedule/services.py +++ b/homeassistant/components/schedule/services.py @@ -1,8 +1,9 @@ """Services for the schedule integration.""" +from enum import StrEnum from typing import TYPE_CHECKING -from homeassistant.const import CONF_ID, SERVICE_RELOAD +from homeassistant.const import CONF_ID from homeassistant.core import ( HomeAssistant, ServiceCall, @@ -12,12 +13,19 @@ from homeassistant.core import ( ) from homeassistant.helpers.service import async_register_admin_service -from .const import DATA_SCHEDULE, DOMAIN, SERVICE_GET +from .const import DATA_SCHEDULE, DOMAIN if TYPE_CHECKING: from .entity import Schedule +class ScheduleService(StrEnum): + """Store keys for schedule services.""" + + GET = "get_schedule" + RELOAD = "reload" + + async def _async_get_schedule_service( schedule: Schedule, service_call: ServiceCall ) -> ServiceResponse: @@ -41,12 +49,12 @@ def async_setup_services(hass: HomeAssistant) -> None: async_register_admin_service( hass, DOMAIN, - SERVICE_RELOAD, + ScheduleService.RELOAD, _async_reload_service, ) hass.data[DATA_SCHEDULE].component.async_register_entity_service( - SERVICE_GET, + ScheduleService.GET, {}, _async_get_schedule_service, supports_response=SupportsResponse.ONLY, diff --git a/homeassistant/components/utility_meter/const.py b/homeassistant/components/utility_meter/const.py index bb22c0f99048..cb046d0cbdf7 100644 --- a/homeassistant/components/utility_meter/const.py +++ b/homeassistant/components/utility_meter/const.py @@ -49,16 +49,12 @@ CONF_SENSOR_ALWAYS_AVAILABLE: Final = "always_available" ATTR_TARIFF = "tariff" ATTR_TARIFFS = "tariffs" -ATTR_VALUE = "value" ATTR_CRON_PATTERN = "cron pattern" ATTR_NEXT_RESET = "next_reset" SIGNAL_START_PAUSE_METER = "utility_meter_start_pause" SIGNAL_RESET_METER = "utility_meter_reset" -SERVICE_RESET = "reset" -SERVICE_CALIBRATE_METER = "calibrate" - class MeterInformation(TypedDict, total=False): """Meter information.""" diff --git a/homeassistant/components/utility_meter/services.py b/homeassistant/components/utility_meter/services.py index caa11280c8f9..755d20a02d47 100644 --- a/homeassistant/components/utility_meter/services.py +++ b/homeassistant/components/utility_meter/services.py @@ -1,5 +1,6 @@ """Support for tracking consumption over given periods of time.""" +from enum import StrEnum import logging import math @@ -13,17 +14,24 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.service import async_register_platform_entity_service -from .const import ( - ATTR_VALUE, - DOMAIN, - SERVICE_CALIBRATE_METER, - SERVICE_RESET, - SIGNAL_RESET_METER, -) +from .const import DOMAIN, SIGNAL_RESET_METER _LOGGER = logging.getLogger(__name__) +class UtilityMeterService(StrEnum): + """Store keys for utility meter services.""" + + CALIBRATE = "calibrate" + RESET = "reset" + + +class UtilityMeterServiceArgument(StrEnum): + """Store keys for utility meter service arguments.""" + + VALUE = "value" + + def _validate_is_number(value): """Validate value is a number.""" try: @@ -56,7 +64,7 @@ def async_setup_services(hass: HomeAssistant) -> None: hass.services.async_register( DOMAIN, - SERVICE_RESET, + UtilityMeterService.RESET, async_reset_meters, probatio.Schema( {ATTR_ENTITY_ID: probatio.All(probatio.EnsureList(), [cv.entity_id])} @@ -66,8 +74,10 @@ def async_setup_services(hass: HomeAssistant) -> None: async_register_platform_entity_service( hass, DOMAIN, - SERVICE_CALIBRATE_METER, + UtilityMeterService.CALIBRATE, entity_domain=SENSOR_DOMAIN, func="async_calibrate", - schema={probatio.Required(ATTR_VALUE): _validate_is_number}, + schema={ + probatio.Required(UtilityMeterServiceArgument.VALUE): _validate_is_number + }, ) diff --git a/tests/components/schedule/test_init.py b/tests/components/schedule/test_init.py index 803eb517835e..14f5f94776d5 100644 --- a/tests/components/schedule/test_init.py +++ b/tests/components/schedule/test_init.py @@ -28,8 +28,8 @@ from homeassistant.components.schedule.const import ( CONF_TUESDAY, CONF_WEDNESDAY, DOMAIN, - SERVICE_GET, ) +from homeassistant.components.schedule.services import ScheduleService from homeassistant.const import ( ATTR_EDITABLE, ATTR_FRIENDLY_NAME, @@ -754,7 +754,7 @@ async def test_service_get( # Test retrieving a single schedule via service call service_result = await hass.services.async_call( DOMAIN, - SERVICE_GET, + ScheduleService.GET, { CONF_ENTITY_ID: entity_id, }, @@ -790,7 +790,7 @@ async def test_service_get( # Test retrieving the schedule via service call after WS update service_result = await hass.services.async_call( DOMAIN, - SERVICE_GET, + ScheduleService.GET, { CONF_ENTITY_ID: entity_id, }, diff --git a/tests/components/utility_meter/test_init.py b/tests/components/utility_meter/test_init.py index ee27c1757c9b..2201e21690f0 100644 --- a/tests/components/utility_meter/test_init.py +++ b/tests/components/utility_meter/test_init.py @@ -23,7 +23,8 @@ from homeassistant.components.utility_meter import ( sensor as um_sensor, ) from homeassistant.components.utility_meter.config_flow import ConfigFlowHandler -from homeassistant.components.utility_meter.const import DOMAIN, SERVICE_RESET +from homeassistant.components.utility_meter.const import DOMAIN +from homeassistant.components.utility_meter.services import UtilityMeterService from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import ( ATTR_ENTITY_ID, @@ -274,7 +275,7 @@ async def test_services(hass: HomeAssistant, meter) -> None: # Reset meters data = {ATTR_ENTITY_ID: meter} - await hass.services.async_call(DOMAIN, SERVICE_RESET, data) + await hass.services.async_call(DOMAIN, UtilityMeterService.RESET, data) await hass.async_block_till_done() state = hass.states.get("sensor.energy_bill_peak") @@ -400,7 +401,7 @@ async def test_services_config_entry(hass: HomeAssistant) -> None: # Reset meters data = {ATTR_ENTITY_ID: "select.energy_bill"} - await hass.services.async_call(DOMAIN, SERVICE_RESET, data) + await hass.services.async_call(DOMAIN, UtilityMeterService.RESET, data) await hass.async_block_till_done() state = hass.states.get("sensor.energy_bill_peak") diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index 268d4eb73120..bf072f0af1ad 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -22,13 +22,10 @@ from homeassistant.components.sensor import ( ) from homeassistant.components.utility_meter import DEFAULT_OFFSET from homeassistant.components.utility_meter.const import ( - ATTR_VALUE, DAILY, DOMAIN, HOURLY, QUARTER_HOURLY, - SERVICE_CALIBRATE_METER, - SERVICE_RESET, ) from homeassistant.components.utility_meter.sensor import ( ATTR_LAST_RESET, @@ -38,6 +35,10 @@ from homeassistant.components.utility_meter.sensor import ( UtilityMeterSensor, UtilitySensorExtraStoredData, ) +from homeassistant.components.utility_meter.services import ( + UtilityMeterService, + UtilityMeterServiceArgument, +) from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, @@ -195,8 +196,11 @@ async def test_state(hass: HomeAssistant, yaml_config, config_entry_config) -> N await hass.services.async_call( DOMAIN, - SERVICE_CALIBRATE_METER, - {ATTR_ENTITY_ID: "sensor.energy_bill_midpeak", ATTR_VALUE: "100"}, + UtilityMeterService.CALIBRATE, + { + ATTR_ENTITY_ID: "sensor.energy_bill_midpeak", + UtilityMeterServiceArgument.VALUE: "100", + }, blocking=True, ) await hass.async_block_till_done() @@ -206,8 +210,11 @@ async def test_state(hass: HomeAssistant, yaml_config, config_entry_config) -> N await hass.services.async_call( DOMAIN, - SERVICE_CALIBRATE_METER, - {ATTR_ENTITY_ID: "sensor.energy_bill_midpeak", ATTR_VALUE: "0.123"}, + UtilityMeterService.CALIBRATE, + { + ATTR_ENTITY_ID: "sensor.energy_bill_midpeak", + UtilityMeterServiceArgument.VALUE: "0.123", + }, blocking=True, ) await hass.async_block_till_done() @@ -955,7 +962,7 @@ async def test_service_reset_no_tariffs( with freeze_time(now): await hass.services.async_call( domain=DOMAIN, - service=SERVICE_RESET, + service=UtilityMeterService.RESET, service_data={}, target={"entity_id": "sensor.energy_bill"}, blocking=True, @@ -1094,7 +1101,7 @@ async def test_service_reset_no_tariffs_correct_with_multi( with freeze_time(now): await hass.services.async_call( domain=DOMAIN, - service=SERVICE_RESET, + service=UtilityMeterService.RESET, service_data={}, target={"entity_id": "sensor.energy_bill"}, blocking=True,