2023-03-03 10:26:13 +00:00
|
|
|
"""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.
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2021-04-27 16:13:11 +00:00
|
|
|
from __future__ import annotations
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2021-04-27 16:13:11 +00:00
|
|
|
import logging
|
2014-03-12 05:45:05 +00:00
|
|
|
|
2021-04-27 16:13:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant, split_entity_id
|
2019-08-12 03:38:18 +00:00
|
|
|
|
2014-11-08 21:57:08 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2014-08-13 12:28:45 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2021-04-27 16:13:11 +00:00
|
|
|
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:
|
2017-07-16 19:39:38 +00:00
|
|
|
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()
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2017-07-06 03:02:16 +00:00
|
|
|
for ent_id in entity_ids:
|
2019-03-26 12:38:33 +00:00
|
|
|
domain = split_entity_id(ent_id)[0]
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
try:
|
2017-07-16 19:39:38 +00:00
|
|
|
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)
|
2017-07-16 19:39:38 +00:00
|
|
|
continue
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if not hasattr(component, "is_on"):
|
2020-07-05 21:04:19 +00:00
|
|
|
_LOGGER.warning("Integration %s has no is_on method", domain)
|
2017-07-16 19:39:38 +00:00
|
|
|
continue
|
2014-01-24 07:26:00 +00:00
|
|
|
|
2017-07-16 19:39:38 +00:00
|
|
|
if component.is_on(ent_id):
|
|
|
|
return True
|
2014-01-24 07:26:00 +00:00
|
|
|
|
|
|
|
return False
|