Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -7,8 +7,11 @@ import logging
|
||||
from homeassistant import config as conf_util
|
||||
from homeassistant.setup import async_prepare_setup_platform
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, CONF_SCAN_INTERVAL, CONF_ENTITY_NAMESPACE,
|
||||
ENTITY_MATCH_ALL)
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_ENTITY_NAMESPACE,
|
||||
ENTITY_MATCH_ALL,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_per_platform, discovery
|
||||
@@ -21,25 +24,27 @@ from .entity_platform import EntityPlatform
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=15)
|
||||
DATA_INSTANCES = 'entity_components'
|
||||
DATA_INSTANCES = "entity_components"
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_update_entity(hass, entity_id):
|
||||
"""Trigger an update for an entity."""
|
||||
domain = entity_id.split('.', 1)[0]
|
||||
domain = entity_id.split(".", 1)[0]
|
||||
entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain)
|
||||
|
||||
if entity_comp is None:
|
||||
logging.getLogger(__name__).warning(
|
||||
'Forced update failed. Component for %s not loaded.', entity_id)
|
||||
"Forced update failed. Component for %s not loaded.", entity_id
|
||||
)
|
||||
return
|
||||
|
||||
entity = entity_comp.get_entity(entity_id)
|
||||
|
||||
if entity is None:
|
||||
logging.getLogger(__name__).warning(
|
||||
'Forced update failed. Entity %s not found.', entity_id)
|
||||
"Forced update failed. Entity %s not found.", entity_id
|
||||
)
|
||||
return
|
||||
|
||||
await entity.async_update_ha_state(True)
|
||||
@@ -56,8 +61,9 @@ class EntityComponent:
|
||||
- Listen for discovery events for platforms related to the domain.
|
||||
"""
|
||||
|
||||
def __init__(self, logger, domain, hass,
|
||||
scan_interval=DEFAULT_SCAN_INTERVAL, group_name=None):
|
||||
def __init__(
|
||||
self, logger, domain, hass, scan_interval=DEFAULT_SCAN_INTERVAL, group_name=None
|
||||
):
|
||||
"""Initialize an entity component."""
|
||||
self.logger = logger
|
||||
self.hass = hass
|
||||
@@ -67,9 +73,7 @@ class EntityComponent:
|
||||
|
||||
self.config = None
|
||||
|
||||
self._platforms = {
|
||||
domain: self._async_init_entity_platform(domain, None)
|
||||
}
|
||||
self._platforms = {domain: self._async_init_entity_platform(domain, None)}
|
||||
self.async_add_entities = self._platforms[domain].async_add_entities
|
||||
self.add_entities = self._platforms[domain].add_entities
|
||||
|
||||
@@ -78,8 +82,9 @@ class EntityComponent:
|
||||
@property
|
||||
def entities(self):
|
||||
"""Return an iterable that returns all entities."""
|
||||
return chain.from_iterable(platform.entities.values() for platform
|
||||
in self._platforms.values())
|
||||
return chain.from_iterable(
|
||||
platform.entities.values() for platform in self._platforms.values()
|
||||
)
|
||||
|
||||
def get_entity(self, entity_id):
|
||||
"""Get an entity."""
|
||||
@@ -121,7 +126,8 @@ class EntityComponent:
|
||||
await self._async_setup_platform(platform, {}, info)
|
||||
|
||||
discovery.async_listen_platform(
|
||||
self.hass, self.domain, component_platform_discovered)
|
||||
self.hass, self.domain, component_platform_discovered
|
||||
)
|
||||
|
||||
async def async_setup_entry(self, config_entry):
|
||||
"""Set up a config entry."""
|
||||
@@ -131,7 +137,9 @@ class EntityComponent:
|
||||
# In future PR we should make hass_config part of the constructor
|
||||
# params.
|
||||
self.config or {},
|
||||
self.domain, platform_type)
|
||||
self.domain,
|
||||
platform_type,
|
||||
)
|
||||
|
||||
if platform is None:
|
||||
return False
|
||||
@@ -139,11 +147,12 @@ class EntityComponent:
|
||||
key = config_entry.entry_id
|
||||
|
||||
if key in self._platforms:
|
||||
raise ValueError('Config entry has already been setup!')
|
||||
raise ValueError("Config entry has already been setup!")
|
||||
|
||||
self._platforms[key] = self._async_init_entity_platform(
|
||||
platform_type, platform,
|
||||
scan_interval=getattr(platform, 'SCAN_INTERVAL', None),
|
||||
platform_type,
|
||||
platform,
|
||||
scan_interval=getattr(platform, "SCAN_INTERVAL", None),
|
||||
)
|
||||
|
||||
return await self._platforms[key].async_setup_entry(config_entry)
|
||||
@@ -155,7 +164,7 @@ class EntityComponent:
|
||||
platform = self._platforms.pop(key, None)
|
||||
|
||||
if platform is None:
|
||||
raise ValueError('Config entry was never loaded!')
|
||||
raise ValueError("Config entry was never loaded!")
|
||||
|
||||
await platform.async_reset()
|
||||
return True
|
||||
@@ -173,45 +182,51 @@ class EntityComponent:
|
||||
if data_ent_id in (None, ENTITY_MATCH_ALL):
|
||||
if data_ent_id is None:
|
||||
self.logger.warning(
|
||||
'Not passing an entity ID to a service to target all '
|
||||
'entities is deprecated. Update your call to %s.%s to be '
|
||||
'instead: entity_id: %s', service.domain, service.service,
|
||||
ENTITY_MATCH_ALL)
|
||||
"Not passing an entity ID to a service to target all "
|
||||
"entities is deprecated. Update your call to %s.%s to be "
|
||||
"instead: entity_id: %s",
|
||||
service.domain,
|
||||
service.service,
|
||||
ENTITY_MATCH_ALL,
|
||||
)
|
||||
|
||||
return [entity for entity in self.entities if entity.available]
|
||||
|
||||
entity_ids = await async_extract_entity_ids(
|
||||
self.hass, service, expand_group)
|
||||
return [entity for entity in self.entities
|
||||
if entity.available and entity.entity_id in entity_ids]
|
||||
entity_ids = await async_extract_entity_ids(self.hass, service, expand_group)
|
||||
return [
|
||||
entity
|
||||
for entity in self.entities
|
||||
if entity.available and entity.entity_id in entity_ids
|
||||
]
|
||||
|
||||
@callback
|
||||
def async_register_entity_service(self, name, schema, func,
|
||||
required_features=None):
|
||||
def async_register_entity_service(self, name, schema, func, required_features=None):
|
||||
"""Register an entity service."""
|
||||
|
||||
async def handle_service(call):
|
||||
"""Handle the service."""
|
||||
service_name = "{}.{}".format(self.domain, name)
|
||||
await self.hass.helpers.service.entity_service_call(
|
||||
self._platforms.values(), func, call, service_name,
|
||||
required_features
|
||||
self._platforms.values(), func, call, service_name, required_features
|
||||
)
|
||||
|
||||
self.hass.services.async_register(
|
||||
self.domain, name, handle_service, schema)
|
||||
self.hass.services.async_register(self.domain, name, handle_service, schema)
|
||||
|
||||
async def _async_setup_platform(self, platform_type, platform_config,
|
||||
discovery_info=None):
|
||||
async def _async_setup_platform(
|
||||
self, platform_type, platform_config, discovery_info=None
|
||||
):
|
||||
"""Set up a platform for this component."""
|
||||
platform = await async_prepare_setup_platform(
|
||||
self.hass, self.config, self.domain, platform_type)
|
||||
self.hass, self.config, self.domain, platform_type
|
||||
)
|
||||
|
||||
if platform is None:
|
||||
return
|
||||
|
||||
# Use config scan interval, fallback to platform if none set
|
||||
scan_interval = platform_config.get(
|
||||
CONF_SCAN_INTERVAL, getattr(platform, 'SCAN_INTERVAL', None))
|
||||
CONF_SCAN_INTERVAL, getattr(platform, "SCAN_INTERVAL", None)
|
||||
)
|
||||
entity_namespace = platform_config.get(CONF_ENTITY_NAMESPACE)
|
||||
|
||||
key = (platform_type, scan_interval, entity_namespace)
|
||||
@@ -232,38 +247,43 @@ class EntityComponent:
|
||||
if self.group_name is None:
|
||||
return
|
||||
|
||||
ids = [entity.entity_id for entity in
|
||||
sorted(self.entities,
|
||||
key=lambda entity: entity.name or entity.entity_id)]
|
||||
ids = [
|
||||
entity.entity_id
|
||||
for entity in sorted(
|
||||
self.entities, key=lambda entity: entity.name or entity.entity_id
|
||||
)
|
||||
]
|
||||
|
||||
self.hass.async_create_task(
|
||||
self.hass.services.async_call(
|
||||
'group', 'set', dict(
|
||||
"group",
|
||||
"set",
|
||||
dict(
|
||||
object_id=slugify(self.group_name),
|
||||
name=self.group_name,
|
||||
visible=False,
|
||||
entities=ids)))
|
||||
entities=ids,
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
async def _async_reset(self):
|
||||
"""Remove entities and reset the entity component to initial values.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
tasks = [platform.async_reset() for platform
|
||||
in self._platforms.values()]
|
||||
tasks = [platform.async_reset() for platform in self._platforms.values()]
|
||||
|
||||
if tasks:
|
||||
await asyncio.wait(tasks)
|
||||
|
||||
self._platforms = {
|
||||
self.domain: self._platforms[self.domain]
|
||||
}
|
||||
self._platforms = {self.domain: self._platforms[self.domain]}
|
||||
self.config = None
|
||||
|
||||
if self.group_name is not None:
|
||||
await self.hass.services.async_call(
|
||||
'group', 'remove', dict(
|
||||
object_id=slugify(self.group_name)))
|
||||
"group", "remove", dict(object_id=slugify(self.group_name))
|
||||
)
|
||||
|
||||
async def async_remove_entity(self, entity_id):
|
||||
"""Remove an entity managed by one of the platforms."""
|
||||
@@ -277,8 +297,7 @@ class EntityComponent:
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
try:
|
||||
conf = await \
|
||||
conf_util.async_hass_config_yaml(self.hass)
|
||||
conf = await conf_util.async_hass_config_yaml(self.hass)
|
||||
except HomeAssistantError as err:
|
||||
self.logger.error(err)
|
||||
return None
|
||||
@@ -286,7 +305,8 @@ class EntityComponent:
|
||||
integration = await async_get_integration(self.hass, self.domain)
|
||||
|
||||
conf = await conf_util.async_process_component_config(
|
||||
self.hass, conf, integration)
|
||||
self.hass, conf, integration
|
||||
)
|
||||
|
||||
if conf is None:
|
||||
return None
|
||||
@@ -294,8 +314,9 @@ class EntityComponent:
|
||||
await self._async_reset()
|
||||
return conf
|
||||
|
||||
def _async_init_entity_platform(self, platform_type, platform,
|
||||
scan_interval=None, entity_namespace=None):
|
||||
def _async_init_entity_platform(
|
||||
self, platform_type, platform, scan_interval=None, entity_namespace=None
|
||||
):
|
||||
"""Initialize an entity platform."""
|
||||
if scan_interval is None:
|
||||
scan_interval = self.scan_interval
|
||||
|
||||
Reference in new issue
Block a user