当前位置: 首页>>代码示例>>Python>>正文


Python HomeAssistantType.data[DOMAIN][webhook_id]方法代码示例

本文整理汇总了Python中homeassistant.helpers.typing.HomeAssistantType.data[DOMAIN][webhook_id]方法的典型用法代码示例。如果您正苦于以下问题:Python HomeAssistantType.data[DOMAIN][webhook_id]方法的具体用法?Python HomeAssistantType.data[DOMAIN][webhook_id]怎么用?Python HomeAssistantType.data[DOMAIN][webhook_id]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在homeassistant.helpers.typing.HomeAssistantType的用法示例。


在下文中一共展示了HomeAssistantType.data[DOMAIN][webhook_id]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle_webhook

# 需要导入模块: from homeassistant.helpers.typing import HomeAssistantType [as 别名]
# 或者: from homeassistant.helpers.typing.HomeAssistantType import data[DOMAIN][webhook_id] [as 别名]
async def handle_webhook(store, hass: HomeAssistantType, webhook_id: str,
                         request):
    """Handle webhook callback."""
    device = hass.data[DOMAIN][webhook_id]

    try:
        req_data = await request.json()
    except ValueError:
        _LOGGER.warning('Received invalid JSON from mobile_app')
        return json_response([], status=HTTP_BAD_REQUEST)

    try:
        req_data = WEBHOOK_PAYLOAD_SCHEMA(req_data)
    except vol.Invalid as ex:
        err = vol.humanize.humanize_error(req_data, ex)
        _LOGGER.error('Received invalid webhook payload: %s', err)
        return Response(status=200)

    webhook_type = req_data[ATTR_WEBHOOK_TYPE]

    webhook_payload = req_data.get(ATTR_WEBHOOK_DATA, {})

    if req_data[ATTR_WEBHOOK_ENCRYPTED]:
        enc_data = req_data[ATTR_WEBHOOK_ENCRYPTED_DATA]
        webhook_payload = _decrypt_payload(device[CONF_SECRET], enc_data)

    try:
        data = WEBHOOK_SCHEMAS[webhook_type](webhook_payload)
    except vol.Invalid as ex:
        err = vol.humanize.humanize_error(webhook_payload, ex)
        _LOGGER.error('Received invalid webhook payload: %s', err)
        return Response(status=200)

    if webhook_type == WEBHOOK_TYPE_CALL_SERVICE:
        try:
            await hass.services.async_call(data[ATTR_DOMAIN],
                                           data[ATTR_SERVICE],
                                           data[ATTR_SERVICE_DATA],
                                           blocking=True,
                                           context=context(device))
        except (vol.Invalid, ServiceNotFound):
            raise HTTPBadRequest()

        return Response(status=200)

    if webhook_type == WEBHOOK_TYPE_FIRE_EVENT:
        event_type = data[ATTR_EVENT_TYPE]
        hass.bus.async_fire(event_type, data[ATTR_EVENT_DATA],
                            ha.EventOrigin.remote, context=context(device))
        return Response(status=200)

    if webhook_type == WEBHOOK_TYPE_RENDER_TEMPLATE:
        try:
            tpl = template.Template(data[ATTR_TEMPLATE], hass)
            rendered = tpl.async_render(data.get(ATTR_TEMPLATE_VARIABLES))
            return json_response({"rendered": rendered})
        except (ValueError, TemplateError) as ex:
            return json_response(({"error": ex}), status=HTTP_BAD_REQUEST)

    if webhook_type == WEBHOOK_TYPE_UPDATE_LOCATION:
        await hass.services.async_call(DEVICE_TRACKER_DOMAIN,
                                       DEVICE_TRACKER_SEE, data,
                                       blocking=True, context=context(device))
        return Response(status=200)

    if webhook_type == WEBHOOK_TYPE_UPDATE_REGISTRATION:
        data[ATTR_APP_ID] = device[ATTR_APP_ID]
        data[ATTR_APP_NAME] = device[ATTR_APP_NAME]
        data[ATTR_SUPPORTS_ENCRYPTION] = device[ATTR_SUPPORTS_ENCRYPTION]
        data[CONF_SECRET] = device[CONF_SECRET]
        data[CONF_USER_ID] = device[CONF_USER_ID]
        data[CONF_WEBHOOK_ID] = device[CONF_WEBHOOK_ID]

        hass.data[DOMAIN][webhook_id] = data

        try:
            await store.async_save(hass.data[DOMAIN])
        except HomeAssistantError as ex:
            _LOGGER.error("Error updating mobile_app registration: %s", ex)
            return Response(status=200)

        return json_response(safe_device(data))
开发者ID:chilicheech,项目名称:home-assistant,代码行数:84,代码来源:__init__.py


注:本文中的homeassistant.helpers.typing.HomeAssistantType.data[DOMAIN][webhook_id]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。