Admin service to automatically add empty schema (#22637)

* Admin service to automatically add empty schema

* Lint
This commit is contained in:
Paulus Schoutsen 2019-04-02 09:34:11 -07:00 committed by GitHub
parent b8b3f4e88f
commit e00ae35e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 8 deletions

View File

@ -187,11 +187,10 @@ async def async_setup(hass, config):
await cloud.remote.disconnect()
await prefs.async_update(remote_enabled=False)
empty_schema = vol.Schema({})
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler, empty_schema)
DOMAIN, SERVICE_REMOTE_CONNECT, _service_handler)
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler, empty_schema)
DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler)
await http_api.async_setup(hass)
hass.async_create_task(hass.helpers.discovery.async_load_platform(

View File

@ -333,9 +333,10 @@ async def _handle_service_platform_call(func, data, entities, context):
@bind_hass
@ha.callback
def async_register_admin_service(hass: typing.HomeAssistantType, domain: str,
def async_register_admin_service(
hass: typing.HomeAssistantType, domain: str,
service: str, service_func: Callable,
schema: vol.Schema) -> None:
schema: vol.Schema = vol.Schema({}, extra=vol.PREVENT_EXTRA)) -> None:
"""Register a service that requires admin access."""
@wraps(service_func)
async def admin_handler(call):

View File

@ -406,7 +406,11 @@ async def test_register_admin_service(hass, hass_read_only_user,
calls.append(call)
hass.helpers.service.async_register_admin_service(
'test', 'test', mock_service, vol.Schema({})
'test', 'test', mock_service
)
hass.helpers.service.async_register_admin_service(
'test', 'test2', mock_service,
vol.Schema({vol.Required('required'): cv.boolean})
)
with pytest.raises(exceptions.UnknownUser):
@ -423,8 +427,21 @@ async def test_register_admin_service(hass, hass_read_only_user,
))
assert len(calls) == 0
with pytest.raises(vol.Invalid):
await hass.services.async_call(
'test', 'test', {}, blocking=True, context=ha.Context(
'test', 'test', {'invalid': True}, blocking=True,
context=ha.Context(user_id=hass_admin_user.id))
assert len(calls) == 0
with pytest.raises(vol.Invalid):
await hass.services.async_call(
'test', 'test2', {}, blocking=True, context=ha.Context(
user_id=hass_admin_user.id
))
assert len(calls) == 0
await hass.services.async_call(
'test', 'test2', {'required': True}, blocking=True, context=ha.Context(
user_id=hass_admin_user.id
))
assert len(calls) == 1