示例#1
0
async def _handle_entity_call(
    hass: HomeAssistant,
    entity: Entity,
    func: str | Callable[..., Any],
    data: dict | ServiceCall,
    context: Context,
) -> None:
    """Handle calling service method."""
    entity.async_set_context(context)

    if isinstance(func, str):
        result = hass.async_run_job(partial(getattr(entity, func),
                                            **data))  # type: ignore
    else:
        result = hass.async_run_job(func, entity, data)

    # Guard because callback functions do not return a task when passed to async_run_job.
    if result is not None:
        await result

    if asyncio.iscoroutine(result):
        _LOGGER.error(
            "Service %s for %s incorrectly returns a coroutine object. Await result instead in service handler. Report bug to integration author",
            func,
            entity.entity_id,
        )
        await result
示例#2
0
def _async_dispatch_domain_event(hass: HomeAssistant, event: Event,
                                 callbacks: Dict[str, List]) -> None:
    domain = split_entity_id(event.data["entity_id"])[0]

    if domain not in callbacks and MATCH_ALL not in callbacks:
        return

    listeners = callbacks.get(domain, []) + callbacks.get(MATCH_ALL, [])

    for action in listeners:
        try:
            hass.async_run_job(action, event)
        except Exception:  # pylint: disable=broad-except
            _LOGGER.exception("Error while processing event %s for domain %s",
                              event, domain)