diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index c05264bd9773..9270f42a1024 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -60,6 +60,7 @@ __all__ = [ "SERVICE_RELOAD_CONFIG_ENTRY", "SERVICE_RELOAD_CORE_CONFIG", "SERVICE_RELOAD_CUSTOM_TEMPLATES", + "HomeAssistantService", "async_set_stop_handler", "async_should_expose", ] diff --git a/homeassistant/components/homeassistant/const.py b/homeassistant/components/homeassistant/const.py index ffdbed44a44c..d2e98a1e8785 100644 --- a/homeassistant/components/homeassistant/const.py +++ b/homeassistant/components/homeassistant/const.py @@ -33,6 +33,16 @@ class HomeAssistantService(StrEnum): UPDATE_ENTITY = "update_entity" +class HomeAssistantServiceArgument(StrEnum): + """Store keys for Home Assistant service arguments.""" + + ELEVATION = "elevation" + ENTRY_ID = "entry_id" + LATITUDE = "latitude" + LONGITUDE = "longitude" + SAFE_MODE = "safe_mode" + + # To be deprecated at a later stage, replaced by HomeAssistantService SERVICE_HOMEASSISTANT_STOP: Final = HomeAssistantService.STOP.value SERVICE_HOMEASSISTANT_RESTART: Final = HomeAssistantService.RESTART.value diff --git a/homeassistant/components/homeassistant/services.py b/homeassistant/components/homeassistant/services.py index c43d5af22f74..e6c08e85d0b3 100644 --- a/homeassistant/components/homeassistant/services.py +++ b/homeassistant/components/homeassistant/services.py @@ -12,14 +12,7 @@ from homeassistant import config as conf_util, core_config from homeassistant.auth.permissions.const import CAT_ENTITIES, POLICY_CONTROL from homeassistant.components import persistent_notification from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN -from homeassistant.const import ( - ATTR_ELEVATION, - ATTR_ENTITY_ID, - ATTR_LATITUDE, - ATTR_LONGITUDE, - CONF_EXCLUDE, - SERVICE_RELOAD, -) +from homeassistant.const import ATTR_ENTITY_ID, CONF_EXCLUDE, SERVICE_RELOAD from homeassistant.core import ( HomeAssistant, ServiceCall, @@ -40,10 +33,12 @@ from homeassistant.helpers.target import ( ) from homeassistant.helpers.template import async_load_custom_templates -from .const import DATA_STOP_HANDLER, DOMAIN, HomeAssistantService - -ATTR_ENTRY_ID = "entry_id" -ATTR_SAFE_MODE = "safe_mode" +from .const import ( + DATA_STOP_HANDLER, + DOMAIN, + HomeAssistantService, + HomeAssistantServiceArgument, +) _LOGGER = logging.getLogger(__name__) @@ -51,14 +46,14 @@ SCHEMA_UPDATE_ENTITY = probatio.Schema({ATTR_ENTITY_ID: cv.entity_ids}) SCHEMA_RELOAD_CONFIG_ENTRY = probatio.All( probatio.Schema( { - probatio.Optional(ATTR_ENTRY_ID): str, + probatio.Optional(HomeAssistantServiceArgument.ENTRY_ID): str, **cv.ENTITY_SERVICE_FIELDS, }, ), - probatio.AtLeastOne(ATTR_ENTRY_ID, *cv.TARGET_ID_KEYS), + probatio.AtLeastOne(HomeAssistantServiceArgument.ENTRY_ID, *cv.TARGET_ID_KEYS), ) SCHEMA_RESTART = probatio.Schema( - {probatio.Optional(ATTR_SAFE_MODE, default=False): bool} + {probatio.Optional(HomeAssistantServiceArgument.SAFE_MODE, default=False): bool} ) SHUTDOWN_SERVICES = (HomeAssistantService.STOP, HomeAssistantService.RESTART) @@ -171,7 +166,7 @@ async def _async_handle_core_service(call: ServiceCall) -> None: ) if call.service == HomeAssistantService.RESTART: - if call.data[ATTR_SAFE_MODE]: + if call.data[HomeAssistantServiceArgument.SAFE_MODE]: await conf_util.async_enable_safe_mode(hass) stop_handler = hass.data[DATA_STOP_HANDLER] await stop_handler(hass, True) @@ -225,11 +220,11 @@ async def _async_set_location(call: ServiceCall) -> None: """Service handler to set location.""" hass = call.hass service_data = { - "latitude": call.data[ATTR_LATITUDE], - "longitude": call.data[ATTR_LONGITUDE], + "latitude": call.data[HomeAssistantServiceArgument.LATITUDE], + "longitude": call.data[HomeAssistantServiceArgument.LONGITUDE], } - if (elevation := call.data.get(ATTR_ELEVATION)) is not None: + if (elevation := call.data.get(HomeAssistantServiceArgument.ELEVATION)) is not None: service_data["elevation"] = elevation await hass.config.async_update(**service_data) @@ -245,8 +240,8 @@ async def _async_handle_reload_config_entry(call: ServiceCall) -> None: """Service handler for reloading a config entry.""" hass = call.hass reload_entries: set[str] = set() - if ATTR_ENTRY_ID in call.data: - reload_entries.add(call.data[ATTR_ENTRY_ID]) + if HomeAssistantServiceArgument.ENTRY_ID in call.data: + reload_entries.add(call.data[HomeAssistantServiceArgument.ENTRY_ID]) if TargetSelection(call.data).has_any_target: _LOGGER.warning( "Reloading a config entry by target is deprecated and will stop " @@ -375,9 +370,11 @@ def async_setup_services(hass: HomeAssistant) -> None: _async_set_location, probatio.Schema( { - probatio.Required(ATTR_LATITUDE): cv.latitude, - probatio.Required(ATTR_LONGITUDE): cv.longitude, - probatio.Optional(ATTR_ELEVATION): probatio.Coerce(int), + probatio.Required(HomeAssistantServiceArgument.LATITUDE): cv.latitude, + probatio.Required(HomeAssistantServiceArgument.LONGITUDE): cv.longitude, + probatio.Optional( + HomeAssistantServiceArgument.ELEVATION + ): probatio.Coerce(int), } ), ) diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 5209953d3fb4..8ed428d0b223 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -26,10 +26,7 @@ from homeassistant.components.homeassistant import ( SERVICE_RELOAD_CUSTOM_TEMPLATES, SERVICE_SET_LOCATION, ) -from homeassistant.components.homeassistant.services import ( - ATTR_ENTRY_ID, - ATTR_SAFE_MODE, -) +from homeassistant.components.homeassistant.const import HomeAssistantServiceArgument from homeassistant.const import ( ATTR_ENTITY_ID, CONF_EXCLUDE, @@ -273,7 +270,10 @@ async def test_setting_location(hass: HomeAssistant) -> None: await hass.services.async_call( DOMAIN, SERVICE_SET_LOCATION, - {"latitude": 30, "longitude": 40}, + { + HomeAssistantServiceArgument.LATITUDE: 30, + HomeAssistantServiceArgument.LONGITUDE: 40, + }, blocking=True, ) assert len(events) == 1 @@ -284,7 +284,11 @@ async def test_setting_location(hass: HomeAssistant) -> None: await hass.services.async_call( DOMAIN, SERVICE_SET_LOCATION, - {"latitude": 30, "longitude": 40, "elevation": 50}, + { + HomeAssistantServiceArgument.LATITUDE: 30, + HomeAssistantServiceArgument.LONGITUDE: 40, + HomeAssistantServiceArgument.ELEVATION: 50, + }, blocking=True, ) assert hass.config.latitude == 30 @@ -294,7 +298,11 @@ async def test_setting_location(hass: HomeAssistant) -> None: await hass.services.async_call( DOMAIN, SERVICE_SET_LOCATION, - {"latitude": 30, "longitude": 40, "elevation": 0.6}, + { + HomeAssistantServiceArgument.LATITUDE: 30, + HomeAssistantServiceArgument.LONGITUDE: 40, + HomeAssistantServiceArgument.ELEVATION: 0.6, + }, blocking=True, ) assert hass.config.latitude == 30 @@ -327,7 +335,10 @@ async def test_require_admin( await hass.services.async_call( ha.DOMAIN, SERVICE_SET_LOCATION, - {"latitude": 0, "longitude": 0}, + { + HomeAssistantServiceArgument.LATITUDE: 0, + HomeAssistantServiceArgument.LONGITUDE: 0, + }, context=ha.Context(user_id=hass_read_only_user.id), blocking=True, ) @@ -476,7 +487,7 @@ async def test_reload_config_entry_by_entry_id( await hass.services.async_call( DOMAIN, "reload_config_entry", - {ATTR_ENTRY_ID: "8955375327824e14ba89e4b29cc3ec9a"}, + {HomeAssistantServiceArgument.ENTRY_ID: "8955375327824e14ba89e4b29cc3ec9a"}, blocking=True, ) @@ -575,7 +586,11 @@ async def test_raises_when_config_is_invalid( @pytest.mark.parametrize( ("service_data", "safe_mode_enabled"), - [({}, False), ({ATTR_SAFE_MODE: False}, False), ({ATTR_SAFE_MODE: True}, True)], + [ + ({}, False), + ({HomeAssistantServiceArgument.SAFE_MODE: False}, False), + ({HomeAssistantServiceArgument.SAFE_MODE: True}, True), + ], ) async def test_restart_homeassistant( hass: HomeAssistant, service_data: dict, safe_mode_enabled: bool