mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
component base: integrated get_module into is_on. Less global lookups = happier performance
This commit is contained in:
parent
77c56b704b
commit
70d2506e01
@ -33,38 +33,38 @@ SERVICE_TURN_OFF = 'turn_off'
|
|||||||
_LOADED_MOD = {}
|
_LOADED_MOD = {}
|
||||||
|
|
||||||
|
|
||||||
def _get_module(module):
|
|
||||||
""" Helper function to load a module. """
|
|
||||||
try:
|
|
||||||
return _LOADED_MOD[module]
|
|
||||||
|
|
||||||
except KeyError:
|
|
||||||
# if module key did not exist in loaded dict
|
|
||||||
try:
|
|
||||||
module = _LOADED_MOD[module] = importlib.import_module(
|
|
||||||
'homeassistant.components.'+module)
|
|
||||||
|
|
||||||
return module
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
# If module does not exist
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def is_on(statemachine, entity_id=None):
|
def is_on(statemachine, entity_id=None):
|
||||||
""" Loads up the module to call the is_on method.
|
""" Loads up the module to call the is_on method.
|
||||||
If there is no entity id given we will check all. """
|
If there is no entity id given we will check all. """
|
||||||
entity_ids = [entity_id] if entity_id else statemachine.entity_ids
|
entity_ids = [entity_id] if entity_id else statemachine.entity_ids
|
||||||
|
|
||||||
|
# Save reference locally because those lookups are way faster
|
||||||
|
modules = _LOADED_MOD
|
||||||
|
|
||||||
for entity_id in entity_ids:
|
for entity_id in entity_ids:
|
||||||
domain = util.split_entity_id(entity_id)[0]
|
domain = util.split_entity_id(entity_id)[0]
|
||||||
|
|
||||||
|
# See if we have the module locally cached, else import it
|
||||||
|
# Does this code look chaotic? Yes!
|
||||||
try:
|
try:
|
||||||
if _get_module(domain).is_on(statemachine, entity_id):
|
module = modules[domain]
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
# If modules[domain] does not exist, import module
|
||||||
|
try:
|
||||||
|
module = modules[domain] = importlib.import_module(
|
||||||
|
'homeassistant.components.'+domain)
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
# If we got a bogus domain the input will fail
|
||||||
|
module = modules[domain] = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
if module.is_on(statemachine, entity_id):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# method is_on does not exist within module
|
# module is None or method is_on does not exist
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user