Clean up accessing service helpers via hass (#72013)

This commit is contained in:
Franck Nijhof 2022-05-17 19:56:57 +02:00 committed by GitHub
parent 5433c0a535
commit c0da97b038
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 55 additions and 45 deletions

View File

@ -9,6 +9,7 @@ from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, dispatcher from homeassistant.helpers import config_validation as cv, dispatcher
from homeassistant.helpers.network import NoURLAvailableError, get_url from homeassistant.helpers.network import NoURLAvailableError, get_url
from homeassistant.helpers.service import async_register_admin_service
from .const import DOMAIN, SIGNAL_HASS_CAST_SHOW_VIEW from .const import DOMAIN, SIGNAL_HASS_CAST_SHOW_VIEW
@ -65,7 +66,8 @@ async def async_setup_ha_cast(
call.data.get(ATTR_URL_PATH), call.data.get(ATTR_URL_PATH),
) )
hass.helpers.service.async_register_admin_service( async_register_admin_service(
hass,
DOMAIN, DOMAIN,
SERVICE_SHOW_VIEW, SERVICE_SHOW_VIEW,
handle_show_view, handle_show_view,

View File

@ -26,6 +26,7 @@ from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
async_dispatcher_send, async_dispatcher_send,
) )
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.util.aiohttp import MockRequest from homeassistant.util.aiohttp import MockRequest
@ -250,11 +251,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
elif service.service == SERVICE_REMOTE_DISCONNECT: elif service.service == SERVICE_REMOTE_DISCONNECT:
await prefs.async_update(remote_enabled=False) await prefs.async_update(remote_enabled=False)
hass.helpers.service.async_register_admin_service( async_register_admin_service(hass, DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler)
DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler async_register_admin_service(
) hass, DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler
) )
loaded = False loaded = False

View File

@ -27,6 +27,7 @@ from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.service import ( from homeassistant.helpers.service import (
async_extract_config_entry_ids, async_extract_config_entry_ids,
async_extract_referenced_entity_ids, async_extract_referenced_entity_ids,
async_register_admin_service,
) )
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -206,14 +207,14 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
if tasks: if tasks:
await asyncio.wait(tasks) await asyncio.wait(tasks)
hass.helpers.service.async_register_admin_service( async_register_admin_service(
ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service hass, ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service
) )
hass.helpers.service.async_register_admin_service( async_register_admin_service(
ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART, async_handle_core_service hass, ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART, async_handle_core_service
) )
hass.helpers.service.async_register_admin_service( async_register_admin_service(
ha.DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service hass, ha.DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service
) )
hass.services.async_register( hass.services.async_register(
ha.DOMAIN, ha.DOMAIN,
@ -233,8 +234,8 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
# auth only processed during startup # auth only processed during startup
await conf_util.async_process_ha_core_config(hass, conf.get(ha.DOMAIN) or {}) await conf_util.async_process_ha_core_config(hass, conf.get(ha.DOMAIN) or {})
hass.helpers.service.async_register_admin_service( async_register_admin_service(
ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config hass, ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config
) )
async def async_set_location(call: ha.ServiceCall) -> None: async def async_set_location(call: ha.ServiceCall) -> None:
@ -243,7 +244,8 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
latitude=call.data[ATTR_LATITUDE], longitude=call.data[ATTR_LONGITUDE] latitude=call.data[ATTR_LATITUDE], longitude=call.data[ATTR_LONGITUDE]
) )
hass.helpers.service.async_register_admin_service( async_register_admin_service(
hass,
ha.DOMAIN, ha.DOMAIN,
SERVICE_SET_LOCATION, SERVICE_SET_LOCATION,
async_set_location, async_set_location,
@ -265,7 +267,8 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
) )
) )
hass.helpers.service.async_register_admin_service( async_register_admin_service(
hass,
ha.DOMAIN, ha.DOMAIN,
SERVICE_RELOAD_CONFIG_ENTRY, SERVICE_RELOAD_CONFIG_ENTRY,
async_handle_reload_config_entry, async_handle_reload_config_entry,

View File

@ -35,6 +35,7 @@ from homeassistant.helpers import (
entity_platform, entity_platform,
) )
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.state import async_reproduce_state from homeassistant.helpers.state import async_reproduce_state
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.loader import async_get_integration from homeassistant.loader import async_get_integration
@ -206,9 +207,7 @@ async def async_setup_platform(
hass.bus.async_fire(EVENT_SCENE_RELOADED, context=call.context) hass.bus.async_fire(EVENT_SCENE_RELOADED, context=call.context)
hass.helpers.service.async_register_admin_service( async_register_admin_service(hass, SCENE_DOMAIN, SERVICE_RELOAD, reload_config)
SCENE_DOMAIN, SERVICE_RELOAD, reload_config
)
async def apply_service(call: ServiceCall) -> None: async def apply_service(call: ServiceCall) -> None:
"""Apply a scene.""" """Apply a scene."""

View File

@ -54,7 +54,10 @@ from homeassistant.helpers.entityfilter import (
EntityFilter, EntityFilter,
) )
from homeassistant.helpers.reload import async_integration_yaml_config from homeassistant.helpers.reload import async_integration_yaml_config
from homeassistant.helpers.service import async_extract_referenced_entity_ids from homeassistant.helpers.service import (
async_extract_referenced_entity_ids,
async_register_admin_service,
)
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import IntegrationNotFound, async_get_integration from homeassistant.loader import IntegrationNotFound, async_get_integration
@ -461,7 +464,8 @@ def _async_register_events_and_services(hass: HomeAssistant) -> None:
await asyncio.gather(*reload_tasks) await asyncio.gather(*reload_tasks)
hass.helpers.service.async_register_admin_service( async_register_admin_service(
hass,
DOMAIN, DOMAIN,
SERVICE_RELOAD, SERVICE_RELOAD,
_handle_homekit_reload, _handle_homekit_reload,

View File

@ -47,6 +47,7 @@ from homeassistant.helpers import (
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import (
@ -531,7 +532,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
_LOGGER.error("%s: unsupported service", service.service) _LOGGER.error("%s: unsupported service", service.service)
for service in ADMIN_SERVICES: for service in ADMIN_SERVICES:
hass.helpers.service.async_register_admin_service( async_register_admin_service(
hass,
DOMAIN, DOMAIN,
service, service,
service_handler, service_handler,

View File

@ -19,6 +19,7 @@ from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import async_get_platforms from homeassistant.helpers.entity_platform import async_get_platforms
import homeassistant.helpers.entity_registry as er import homeassistant.helpers.entity_registry as er
from homeassistant.helpers.service import entity_service_call
from .const import ( from .const import (
_LOGGER, _LOGGER,
@ -373,8 +374,8 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
) )
async def _async_send_raw_node_command(call: ServiceCall) -> None: async def _async_send_raw_node_command(call: ServiceCall) -> None:
await hass.helpers.service.entity_service_call( await entity_service_call(
async_get_platforms(hass, DOMAIN), "async_send_raw_node_command", call hass, async_get_platforms(hass, DOMAIN), "async_send_raw_node_command", call
) )
hass.services.async_register( hass.services.async_register(
@ -385,8 +386,8 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
) )
async def _async_send_node_command(call: ServiceCall) -> None: async def _async_send_node_command(call: ServiceCall) -> None:
await hass.helpers.service.entity_service_call( await entity_service_call(
async_get_platforms(hass, DOMAIN), "async_send_node_command", call hass, async_get_platforms(hass, DOMAIN), "async_send_node_command", call
) )
hass.services.async_register( hass.services.async_register(
@ -397,8 +398,8 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
) )
async def _async_get_zwave_parameter(call: ServiceCall) -> None: async def _async_get_zwave_parameter(call: ServiceCall) -> None:
await hass.helpers.service.entity_service_call( await entity_service_call(
async_get_platforms(hass, DOMAIN), "async_get_zwave_parameter", call hass, async_get_platforms(hass, DOMAIN), "async_get_zwave_parameter", call
) )
hass.services.async_register( hass.services.async_register(
@ -409,8 +410,8 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
) )
async def _async_set_zwave_parameter(call: ServiceCall) -> None: async def _async_set_zwave_parameter(call: ServiceCall) -> None:
await hass.helpers.service.entity_service_call( await entity_service_call(
async_get_platforms(hass, DOMAIN), "async_set_zwave_parameter", call hass, async_get_platforms(hass, DOMAIN), "async_set_zwave_parameter", call
) )
hass.services.async_register( hass.services.async_register(
@ -421,8 +422,8 @@ def async_setup_services(hass: HomeAssistant) -> None: # noqa: C901
) )
async def _async_rename_node(call: ServiceCall) -> None: async def _async_rename_node(call: ServiceCall) -> None:
await hass.helpers.service.entity_service_call( await entity_service_call(
async_get_platforms(hass, DOMAIN), "async_rename_node", call hass, async_get_platforms(hass, DOMAIN), "async_rename_node", call
) )
hass.services.async_register( hass.services.async_register(

View File

@ -19,6 +19,7 @@ from homeassistant.helpers import (
update_coordinator, update_coordinator,
) )
from homeassistant.helpers.reload import async_reload_integration_platforms from homeassistant.helpers.reload import async_reload_integration_platforms
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import async_get_integration from homeassistant.loader import async_get_integration
@ -54,9 +55,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context)
hass.helpers.service.async_register_admin_service( async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config)
DOMAIN, SERVICE_RELOAD, _reload_config
)
return True return True

View File

@ -377,7 +377,7 @@ async def test_async_get_descriptions_script(hass):
} }
await async_setup_component(hass, DOMAIN, script_config) await async_setup_component(hass, DOMAIN, script_config)
descriptions = await hass.helpers.service.async_get_all_descriptions() descriptions = await async_get_all_descriptions(hass)
assert descriptions[DOMAIN]["test1"]["description"] == "" assert descriptions[DOMAIN]["test1"]["description"] == ""
assert not descriptions[DOMAIN]["test1"]["fields"] assert not descriptions[DOMAIN]["test1"]["fields"]

View File

@ -819,8 +819,9 @@ async def test_register_admin_service(hass, hass_read_only_user, hass_admin_user
async def mock_service(call): async def mock_service(call):
calls.append(call) calls.append(call)
hass.helpers.service.async_register_admin_service("test", "test", mock_service) service.async_register_admin_service(hass, "test", "test", mock_service)
hass.helpers.service.async_register_admin_service( service.async_register_admin_service(
hass,
"test", "test",
"test2", "test2",
mock_service, mock_service,
@ -887,7 +888,7 @@ async def test_domain_control_not_async(hass, mock_entities):
calls.append(call) calls.append(call)
with pytest.raises(exceptions.HomeAssistantError): with pytest.raises(exceptions.HomeAssistantError):
hass.helpers.service.verify_domain_control("test_domain")(mock_service_log) service.verify_domain_control(hass, "test_domain")(mock_service_log)
async def test_domain_control_unknown(hass, mock_entities): async def test_domain_control_unknown(hass, mock_entities):
@ -902,9 +903,9 @@ async def test_domain_control_unknown(hass, mock_entities):
"homeassistant.helpers.entity_registry.async_get_registry", "homeassistant.helpers.entity_registry.async_get_registry",
return_value=Mock(entities=mock_entities), return_value=Mock(entities=mock_entities),
): ):
protected_mock_service = hass.helpers.service.verify_domain_control( protected_mock_service = service.verify_domain_control(hass, "test_domain")(
"test_domain" mock_service_log
)(mock_service_log) )
hass.services.async_register( hass.services.async_register(
"test_domain", "test_service", protected_mock_service, schema=None "test_domain", "test_service", protected_mock_service, schema=None
@ -940,7 +941,7 @@ async def test_domain_control_unauthorized(hass, hass_read_only_user):
"""Define a protected service.""" """Define a protected service."""
calls.append(call) calls.append(call)
protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")( protected_mock_service = service.verify_domain_control(hass, "test_domain")(
mock_service_log mock_service_log
) )
@ -979,7 +980,7 @@ async def test_domain_control_admin(hass, hass_admin_user):
"""Define a protected service.""" """Define a protected service."""
calls.append(call) calls.append(call)
protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")( protected_mock_service = service.verify_domain_control(hass, "test_domain")(
mock_service_log mock_service_log
) )
@ -1017,7 +1018,7 @@ async def test_domain_control_no_user(hass):
"""Define a protected service.""" """Define a protected service."""
calls.append(call) calls.append(call)
protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")( protected_mock_service = service.verify_domain_control(hass, "test_domain")(
mock_service_log mock_service_log
) )