2013-12-11 00:07:30 -08:00
|
|
|
"""
|
|
|
|
This package contains components that can be plugged into Home Assistant.
|
2014-01-04 18:24:30 -08:00
|
|
|
|
|
|
|
Component design guidelines:
|
2016-03-08 17:55:57 +01: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 00:07:30 -08:00
|
|
|
"""
|
2014-08-13 14:28:45 +02:00
|
|
|
import logging
|
2014-01-23 23:26:00 -08:00
|
|
|
|
2019-03-26 05:38:33 -07:00
|
|
|
from homeassistant.core import split_entity_id
|
2014-03-11 22:45:05 -07:00
|
|
|
|
2019-08-12 06:38:18 +03:00
|
|
|
# mypy: allow-untyped-defs
|
|
|
|
|
2014-11-08 13:57:08 -08:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2014-08-13 14:28:45 +02:00
|
|
|
|
2014-01-23 23:26:00 -08:00
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
def is_on(hass, entity_id=None):
|
2016-03-08 17:55:57 +01: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 12:59:45 -07:00
|
|
|
if entity_id:
|
2017-07-16 12:39:38 -07:00
|
|
|
entity_ids = hass.components.group.expand_entity_ids([entity_id])
|
2014-04-13 12:59:45 -07:00
|
|
|
else:
|
2014-11-28 23:19:59 -08:00
|
|
|
entity_ids = hass.states.entity_ids()
|
2014-01-23 23:26:00 -08:00
|
|
|
|
2017-07-05 20:02:16 -07:00
|
|
|
for ent_id in entity_ids:
|
2019-03-26 05:38:33 -07:00
|
|
|
domain = split_entity_id(ent_id)[0]
|
2014-01-23 23:26:00 -08:00
|
|
|
|
|
|
|
try:
|
2017-07-16 12:39:38 -07:00
|
|
|
component = getattr(hass.components, domain)
|
|
|
|
|
|
|
|
except ImportError:
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER.error("Failed to call %s.is_on: component not found", domain)
|
2017-07-16 12:39:38 -07:00
|
|
|
continue
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if not hasattr(component, "is_on"):
|
2019-07-06 00:24:26 +02:00
|
|
|
_LOGGER.warning("Integration %s has no is_on method.", domain)
|
2017-07-16 12:39:38 -07:00
|
|
|
continue
|
2014-01-23 23:26:00 -08:00
|
|
|
|
2017-07-16 12:39:38 -07:00
|
|
|
if component.is_on(ent_id):
|
|
|
|
return True
|
2014-01-23 23:26:00 -08:00
|
|
|
|
|
|
|
return False
|