Add foundation for integration services (#30813)

* Add foundation for integration services

* Fix tests

* Remove async_get_platform

* Migrate Sonos partially to EntityPlatform.async_register_entity_service

* Tweaks

* Move other Sonos services to media player domain

* Move other Sonos services to media player domain

* Address comments

* Remove lock

* Fix typos

* Use make_entity_service_schema

* Add area extraction to async_extract_entities

Co-authored-by: Anders Melchiorsen <amelchio@nogoto.net>
This commit is contained in:
Paulus Schoutsen 2020-01-19 17:55:18 -08:00 committed by GitHub
parent f20b3515f2
commit 0c3ffbe282
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 339 additions and 289 deletions

View file

@ -7,6 +7,7 @@ from typing import Optional
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import callback, split_entity_id, valid_entity_id
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
from homeassistant.helpers import config_validation as cv, service
from homeassistant.util.async_ import run_callback_threadsafe
from .entity_registry import DISABLED_INTEGRATION
@ -194,7 +195,11 @@ class EntityPlatform:
)
return False
except Exception: # pylint: disable=broad-except
logger.exception("Error while setting up platform %s", self.platform_name)
logger.exception(
"Error while setting up %s platform for %s",
self.platform_name,
self.domain,
)
return False
finally:
warn_task.cancel()
@ -449,6 +454,33 @@ class EntityPlatform:
self._async_unsub_polling()
self._async_unsub_polling = None
async def async_extract_from_service(self, service_call, expand_group=True):
"""Extract all known and available entities from a service call.
Will return an empty list if entities specified but unknown.
This method must be run in the event loop.
"""
return await service.async_extract_entities(
self.hass, self.entities.values(), service_call, expand_group
)
@callback
def async_register_entity_service(self, name, schema, func, required_features=None):
"""Register an entity service."""
if isinstance(schema, dict):
schema = cv.make_entity_service_schema(schema)
async def handle_service(call):
"""Handle the service."""
await service.entity_service_call(
self.hass, [self], func, call, required_features
)
self.hass.services.async_register(
self.platform_name, name, handle_service, schema
)
async def _update_entity_states(self, now: datetime) -> None:
"""Update the states of all the polling entities.