mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
Global variables
This commit is contained in:
parent
75f566a7ab
commit
d61a4fba63
35
esphomeyaml/components/globals.py
Normal file
35
esphomeyaml/components/globals.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from esphomeyaml import config_validation as cv
|
||||||
|
from esphomeyaml.const import CONF_ID, CONF_INITIAL_VALUE, CONF_RESTORE_VALUE, CONF_TYPE
|
||||||
|
from esphomeyaml.helpers import App, Component, Pvariable, RawExpression, TemplateArguments, add, \
|
||||||
|
esphomelib_ns, setup_component
|
||||||
|
|
||||||
|
GlobalVariableComponent = esphomelib_ns.class_('GlobalVariableComponent', Component)
|
||||||
|
|
||||||
|
GLOBAL_VAR_SCHEMA = vol.Schema({
|
||||||
|
vol.Required(CONF_ID): cv.declare_variable_id(GlobalVariableComponent),
|
||||||
|
vol.Required(CONF_TYPE): cv.string,
|
||||||
|
vol.Optional(CONF_INITIAL_VALUE): cv.string,
|
||||||
|
vol.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
||||||
|
}).extend(cv.COMPONENT_SCHEMA.schema)
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = vol.All(cv.ensure_list, [GLOBAL_VAR_SCHEMA])
|
||||||
|
|
||||||
|
|
||||||
|
def to_code(config):
|
||||||
|
for conf in config:
|
||||||
|
type_ = RawExpression(conf[CONF_TYPE])
|
||||||
|
template_args = TemplateArguments(type_)
|
||||||
|
res_type = GlobalVariableComponent.template(template_args)
|
||||||
|
initial_value = None
|
||||||
|
if CONF_INITIAL_VALUE in conf:
|
||||||
|
initial_value = RawExpression(conf[CONF_INITIAL_VALUE])
|
||||||
|
rhs = App.Pmake_global_variable(template_args, initial_value)
|
||||||
|
glob = Pvariable(conf[CONF_ID], rhs, type=res_type)
|
||||||
|
|
||||||
|
if conf.get(CONF_RESTORE_VALUE, False):
|
||||||
|
hash_ = hash(conf[CONF_ID].id) % 2**32
|
||||||
|
add(glob.set_restore_value(hash_))
|
||||||
|
|
||||||
|
setup_component(glob, conf)
|
@ -365,6 +365,8 @@ CONF_TIME_ID = 'time_id'
|
|||||||
CONF_RESTORE_STATE = 'restore_state'
|
CONF_RESTORE_STATE = 'restore_state'
|
||||||
CONF_TIMING = 'timing'
|
CONF_TIMING = 'timing'
|
||||||
CONF_INVALID_COOLDOWN = 'invalid_cooldown'
|
CONF_INVALID_COOLDOWN = 'invalid_cooldown'
|
||||||
|
CONF_INITIAL_VALUE = 'initial_value'
|
||||||
|
CONF_RESTORE_VALUE = 'restore_value'
|
||||||
|
|
||||||
|
|
||||||
ALLOWED_NAME_CHARS = u'abcdefghijklmnopqrstuvwxyz0123456789_'
|
ALLOWED_NAME_CHARS = u'abcdefghijklmnopqrstuvwxyz0123456789_'
|
||||||
|
@ -407,15 +407,31 @@ def get_variable(id):
|
|||||||
yield None
|
yield None
|
||||||
|
|
||||||
|
|
||||||
|
def get_variable_with_full_id(id):
|
||||||
|
while True:
|
||||||
|
for k, v in _VARIABLES.iteritems():
|
||||||
|
if k == id:
|
||||||
|
yield (k, v)
|
||||||
|
return
|
||||||
|
_LOGGER.debug("Waiting for variable %s", id)
|
||||||
|
yield None, None
|
||||||
|
|
||||||
|
|
||||||
def process_lambda(value, parameters, capture='=', return_type=None):
|
def process_lambda(value, parameters, capture='=', return_type=None):
|
||||||
|
from esphomeyaml.components.globals import GlobalVariableComponent
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
yield
|
yield
|
||||||
return
|
return
|
||||||
parts = value.parts[:]
|
parts = value.parts[:]
|
||||||
for i, id in enumerate(value.requires_ids):
|
for i, id in enumerate(value.requires_ids):
|
||||||
var = None
|
for full_id, var in get_variable_with_full_id(id):
|
||||||
for var in get_variable(id):
|
|
||||||
yield
|
yield
|
||||||
|
if full_id is not None and isinstance(full_id.type, MockObjClass) and \
|
||||||
|
full_id.type.inherits_from(GlobalVariableComponent):
|
||||||
|
parts[i * 3 + 1] = var.value()
|
||||||
|
continue
|
||||||
|
|
||||||
if parts[i * 3 + 2] == '.':
|
if parts[i * 3 + 2] == '.':
|
||||||
parts[i * 3 + 1] = var._
|
parts[i * 3 + 1] = var._
|
||||||
else:
|
else:
|
||||||
@ -592,7 +608,9 @@ class MockObjClass(MockObj):
|
|||||||
def template(self, args):
|
def template(self, args):
|
||||||
if not isinstance(args, TemplateArguments):
|
if not isinstance(args, TemplateArguments):
|
||||||
args = TemplateArguments(args)
|
args = TemplateArguments(args)
|
||||||
obj = MockObjClass(u'{}{}'.format(self.base, args), parents=self._parents)
|
new_parents = self._parents[:]
|
||||||
|
new_parents.append(self)
|
||||||
|
obj = MockObjClass(u'{}{}'.format(self.base, args), parents=new_parents)
|
||||||
obj.requires.append(self)
|
obj.requires.append(self)
|
||||||
obj.requires.append(args)
|
obj.requires.append(args)
|
||||||
return obj
|
return obj
|
||||||
|
Loading…
x
Reference in New Issue
Block a user