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


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

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


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

示例1: handle_webhook

# 需要导入模块: from homeassistant.helpers.typing import HomeAssistantType [as 别名]
# 或者: from homeassistant.helpers.typing.HomeAssistantType import data[DOMAIN][entity_type][unique_store_key] [as 别名]

#.........这里部分代码省略.........

    if webhook_type == WEBHOOK_TYPE_UPDATE_REGISTRATION:
        new_registration = {**registration, **data}

        device_registry = await dr.async_get_registry(hass)

        device_registry.async_get_or_create(
            config_entry_id=config_entry.entry_id,
            identifiers={
                (ATTR_DEVICE_ID, registration[ATTR_DEVICE_ID]),
                (CONF_WEBHOOK_ID, registration[CONF_WEBHOOK_ID])
            },
            manufacturer=new_registration[ATTR_MANUFACTURER],
            model=new_registration[ATTR_MODEL],
            name=new_registration[ATTR_DEVICE_NAME],
            sw_version=new_registration[ATTR_OS_VERSION]
        )

        hass.config_entries.async_update_entry(config_entry,
                                               data=new_registration)

        return webhook_response(safe_registration(new_registration),
                                registration=registration, headers=headers)

    if webhook_type == WEBHOOK_TYPE_REGISTER_SENSOR:
        entity_type = data[ATTR_SENSOR_TYPE]

        unique_id = data[ATTR_SENSOR_UNIQUE_ID]

        unique_store_key = "{}_{}".format(webhook_id, unique_id)

        if unique_store_key in hass.data[DOMAIN][entity_type]:
            _LOGGER.error("Refusing to re-register existing sensor %s!",
                          unique_id)
            return error_response(ERR_SENSOR_DUPLICATE_UNIQUE_ID,
                                  "{} {} already exists!".format(entity_type,
                                                                 unique_id),
                                  status=409)

        data[CONF_WEBHOOK_ID] = webhook_id

        hass.data[DOMAIN][entity_type][unique_store_key] = data

        try:
            await hass.data[DOMAIN][DATA_STORE].async_save(savable_state(hass))
        except HomeAssistantError as ex:
            _LOGGER.error("Error registering sensor: %s", ex)
            return empty_okay_response()

        register_signal = '{}_{}_register'.format(DOMAIN,
                                                  data[ATTR_SENSOR_TYPE])
        async_dispatcher_send(hass, register_signal, data)

        return webhook_response({"status": "registered"},
                                registration=registration, status=HTTP_CREATED,
                                headers=headers)

    if webhook_type == WEBHOOK_TYPE_UPDATE_SENSOR_STATES:
        resp = {}
        for sensor in data:
            entity_type = sensor[ATTR_SENSOR_TYPE]

            unique_id = sensor[ATTR_SENSOR_UNIQUE_ID]

            unique_store_key = "{}_{}".format(webhook_id, unique_id)

            if unique_store_key not in hass.data[DOMAIN][entity_type]:
                _LOGGER.error("Refusing to update non-registered sensor: %s",
                              unique_store_key)
                err_msg = '{} {} is not registered'.format(entity_type,
                                                           unique_id)
                resp[unique_id] = {
                    'success': False,
                    'error': {
                        'code': ERR_SENSOR_NOT_REGISTERED,
                        'message': err_msg
                    }
                }
                continue

            entry = hass.data[DOMAIN][entity_type][unique_store_key]

            new_state = {**entry, **sensor}

            hass.data[DOMAIN][entity_type][unique_store_key] = new_state

            safe = savable_state(hass)

            try:
                await hass.data[DOMAIN][DATA_STORE].async_save(safe)
            except HomeAssistantError as ex:
                _LOGGER.error("Error updating mobile_app registration: %s", ex)
                return empty_okay_response()

            async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, new_state)

            resp[unique_id] = {"status": "okay"}

        return webhook_response(resp, registration=registration,
                                headers=headers)
开发者ID:boced66,项目名称:home-assistant,代码行数:104,代码来源:webhook.py


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