本文整理汇总了Python中homeassistant.helpers.typing.HomeAssistantType.data[DATA_CONFIG_ENTRY_LOCK]方法的典型用法代码示例。如果您正苦于以下问题:Python HomeAssistantType.data[DATA_CONFIG_ENTRY_LOCK]方法的具体用法?Python HomeAssistantType.data[DATA_CONFIG_ENTRY_LOCK]怎么用?Python HomeAssistantType.data[DATA_CONFIG_ENTRY_LOCK]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类homeassistant.helpers.typing.HomeAssistantType
的用法示例。
在下文中一共展示了HomeAssistantType.data[DATA_CONFIG_ENTRY_LOCK]方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: async_setup_entry
# 需要导入模块: from homeassistant.helpers.typing import HomeAssistantType [as 别名]
# 或者: from homeassistant.helpers.typing.HomeAssistantType import data[DATA_CONFIG_ENTRY_LOCK] [as 别名]
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Set up Point from a config entry."""
from pypoint import PointSession
def token_saver(token):
_LOGGER.debug('Saving updated token')
entry.data[CONF_TOKEN] = token
hass.config_entries.async_update_entry(entry, data={**entry.data})
# Force token update.
entry.data[CONF_TOKEN]['expires_in'] = -1
session = PointSession(
entry.data['refresh_args']['client_id'],
token=entry.data[CONF_TOKEN],
auto_refresh_kwargs=entry.data['refresh_args'],
token_saver=token_saver,
)
if not session.is_authorized:
_LOGGER.error('Authentication Error')
return False
hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
hass.data[CONFIG_ENTRY_IS_SETUP] = set()
await async_setup_webhook(hass, entry, session)
client = MinutPointClient(hass, entry, session)
hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: client})
await client.update()
return True
示例2: async_start
# 需要导入模块: from homeassistant.helpers.typing import HomeAssistantType [as 别名]
# 或者: from homeassistant.helpers.typing.HomeAssistantType import data[DATA_CONFIG_ENTRY_LOCK] [as 别名]
async def async_start(hass: HomeAssistantType, discovery_topic, hass_config,
config_entry=None) -> bool:
"""Initialize of MQTT Discovery."""
async def async_device_message_received(topic, payload, qos):
"""Process the received message."""
match = TOPIC_MATCHER.match(topic)
if not match:
return
_prefix_topic, component, node_id, object_id = match.groups()
if component not in SUPPORTED_COMPONENTS:
_LOGGER.warning("Component %s is not supported", component)
return
# If present, the node_id will be included in the discovered object id
discovery_id = '_'.join((node_id, object_id)) if node_id else object_id
if ALREADY_DISCOVERED not in hass.data:
hass.data[ALREADY_DISCOVERED] = {}
discovery_hash = (component, discovery_id)
if discovery_hash in hass.data[ALREADY_DISCOVERED]:
_LOGGER.info(
"Component has already been discovered: %s %s, sending update",
component, discovery_id)
async_dispatcher_send(
hass, MQTT_DISCOVERY_UPDATED.format(discovery_hash), payload)
elif payload:
# Add component
try:
payload = json.loads(payload)
except ValueError:
_LOGGER.warning("Unable to parse JSON %s: '%s'",
object_id, payload)
return
payload = dict(payload)
platform = payload.get(CONF_PLATFORM, 'mqtt')
if platform not in ALLOWED_PLATFORMS.get(component, []):
_LOGGER.warning("Platform %s (component %s) is not allowed",
platform, component)
return
payload[CONF_PLATFORM] = platform
if CONF_STATE_TOPIC not in payload:
payload[CONF_STATE_TOPIC] = '{}/{}/{}{}/state'.format(
discovery_topic, component,
'%s/' % node_id if node_id else '', object_id)
hass.data[ALREADY_DISCOVERED][discovery_hash] = None
payload[ATTR_DISCOVERY_HASH] = discovery_hash
_LOGGER.info("Found new component: %s %s", component, discovery_id)
if platform not in CONFIG_ENTRY_PLATFORMS.get(component, []):
await async_load_platform(
hass, component, platform, payload, hass_config)
return
config_entries_key = '{}.{}'.format(component, platform)
async with hass.data[DATA_CONFIG_ENTRY_LOCK]:
if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]:
await hass.config_entries.async_forward_entry_setup(
config_entry, component)
hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key)
async_dispatcher_send(hass, MQTT_DISCOVERY_NEW.format(
component, platform), payload)
hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
hass.data[CONFIG_ENTRY_IS_SETUP] = set()
await mqtt.async_subscribe(
hass, discovery_topic + '/#', async_device_message_received, 0)
return True
示例3: async_start
# 需要导入模块: from homeassistant.helpers.typing import HomeAssistantType [as 别名]
# 或者: from homeassistant.helpers.typing.HomeAssistantType import data[DATA_CONFIG_ENTRY_LOCK] [as 别名]
async def async_start(hass: HomeAssistantType, discovery_topic, hass_config,
config_entry=None) -> bool:
"""Initialize of MQTT Discovery."""
async def async_device_message_received(topic, payload, qos):
"""Process the received message."""
match = TOPIC_MATCHER.match(topic)
if not match:
return
_prefix_topic, component, node_id, object_id = match.groups()
if component not in SUPPORTED_COMPONENTS:
_LOGGER.warning("Component %s is not supported", component)
return
if payload:
try:
payload = json.loads(payload)
except ValueError:
_LOGGER.warning("Unable to parse JSON %s: '%s'",
object_id, payload)
return
payload = dict(payload)
for key in list(payload.keys()):
abbreviated_key = key
key = ABBREVIATIONS.get(key, key)
payload[key] = payload.pop(abbreviated_key)
base = payload.pop(TOPIC_BASE, None)
if base:
for key, value in payload.items():
if isinstance(value, str) and value:
if value[0] == TOPIC_BASE and key.endswith('_topic'):
payload[key] = "{}{}".format(base, value[1:])
if value[-1] == TOPIC_BASE and key.endswith('_topic'):
payload[key] = "{}{}".format(value[:-1], base)
# If present, the node_id will be included in the discovered object id
discovery_id = ' '.join((node_id, object_id)) if node_id else object_id
discovery_hash = (component, discovery_id)
if payload:
if CONF_PLATFORM in payload and 'schema' not in payload:
platform = payload[CONF_PLATFORM]
if (component in DEPRECATED_PLATFORM_TO_SCHEMA and
platform in DEPRECATED_PLATFORM_TO_SCHEMA[component]):
schema = DEPRECATED_PLATFORM_TO_SCHEMA[component][platform]
payload['schema'] = schema
_LOGGER.warning('"platform": "%s" is deprecated, '
'replace with "schema":"%s"',
platform, schema)
payload[CONF_PLATFORM] = 'mqtt'
if CONF_STATE_TOPIC not in payload:
payload[CONF_STATE_TOPIC] = '{}/{}/{}{}/state'.format(
discovery_topic, component,
'%s/' % node_id if node_id else '', object_id)
payload[ATTR_DISCOVERY_HASH] = discovery_hash
if ALREADY_DISCOVERED not in hass.data:
hass.data[ALREADY_DISCOVERED] = {}
if discovery_hash in hass.data[ALREADY_DISCOVERED]:
# Dispatch update
_LOGGER.info(
"Component has already been discovered: %s %s, sending update",
component, discovery_id)
async_dispatcher_send(
hass, MQTT_DISCOVERY_UPDATED.format(discovery_hash), payload)
elif payload:
# Add component
_LOGGER.info("Found new component: %s %s", component, discovery_id)
hass.data[ALREADY_DISCOVERED][discovery_hash] = None
if component not in CONFIG_ENTRY_COMPONENTS:
await async_load_platform(
hass, component, 'mqtt', payload, hass_config)
return
config_entries_key = '{}.{}'.format(component, 'mqtt')
async with hass.data[DATA_CONFIG_ENTRY_LOCK]:
if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]:
await hass.config_entries.async_forward_entry_setup(
config_entry, component)
hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key)
async_dispatcher_send(hass, MQTT_DISCOVERY_NEW.format(
component, 'mqtt'), payload)
hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
hass.data[CONFIG_ENTRY_IS_SETUP] = set()
await mqtt.async_subscribe(
hass, discovery_topic + '/#', async_device_message_received, 0)
return True