core/homeassistant/components/__init__.py

47 lines
1.3 KiB
Python
Raw Normal View History

"""
This package contains components that can be plugged into Home Assistant.
2014-01-05 02:24:30 +00:00
Component design guidelines:
2016-03-08 16:55:57 +00:00
- Each component defines a constant DOMAIN that is equal to its filename.
- Each component that tracks states should create state entity names in the
format "<DOMAIN>.<OBJECT_ID>".
- Each component should publish services only under its own domain.
"""
from __future__ import annotations
import logging
from homeassistant.core import HomeAssistant, split_entity_id
2014-11-08 21:57:08 +00:00
_LOGGER = logging.getLogger(__name__)
def is_on(hass: HomeAssistant, entity_id: str | None = None) -> bool:
2016-03-08 16:55:57 +00:00
"""Load up the module to call the is_on method.
If there is no entity id given we will check all.
"""
2014-04-13 19:59:45 +00:00
if entity_id:
entity_ids = hass.components.group.expand_entity_ids([entity_id])
2014-04-13 19:59:45 +00:00
else:
2014-11-29 07:19:59 +00:00
entity_ids = hass.states.entity_ids()
for ent_id in entity_ids:
domain = split_entity_id(ent_id)[0]
try:
component = getattr(hass.components, domain)
except ImportError:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Failed to call %s.is_on: component not found", domain)
continue
2019-07-31 19:25:30 +00:00
if not hasattr(component, "is_on"):
_LOGGER.warning("Integration %s has no is_on method", domain)
continue
if component.is_on(ent_id):
return True
return False