esphome/esphomeyaml/helpers.py

634 lines
18 KiB
Python
Raw Normal View History

2018-04-07 01:23:03 +02:00
from __future__ import print_function
2018-06-02 22:22:20 +02:00
import inspect
2018-04-07 01:23:03 +02:00
import logging
import re
2018-06-03 07:11:11 +02:00
from collections import OrderedDict, deque
2018-04-07 01:23:03 +02:00
from esphomeyaml import core
2018-04-07 01:23:03 +02:00
from esphomeyaml.const import CONF_AVAILABILITY, CONF_COMMAND_TOPIC, CONF_DISCOVERY, \
CONF_INVERTED, \
2018-05-15 11:09:27 +02:00
CONF_MODE, CONF_NUMBER, CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_PCF8574, \
CONF_RETAIN, CONF_STATE_TOPIC, CONF_TOPIC
2018-06-02 22:22:20 +02:00
from esphomeyaml.core import ESPHomeYAMLError, HexInt, Lambda, TimePeriodMicroseconds, \
TimePeriodMilliseconds, TimePeriodSeconds
2018-04-07 01:23:03 +02:00
_LOGGER = logging.getLogger(__name__)
def ensure_unique_string(preferred_string, current_strings):
test_string = preferred_string
current_strings_set = set(current_strings)
tries = 1
while test_string in current_strings_set:
tries += 1
test_string = u"{}_{}".format(preferred_string, tries)
return test_string
def indent_all_but_first_and_last(text, padding=u' '):
lines = text.splitlines(True)
if len(lines) <= 2:
return text
return lines[0] + u''.join(padding + line for line in lines[1:-1]) + lines[-1]
def indent_list(text, padding=u' '):
return [padding + line for line in text.splitlines()]
def indent(text, padding=u' '):
return u'\n'.join(indent_list(text, padding))
class Expression(object):
def __init__(self):
self.requires = []
self.required = False
2018-04-07 01:23:03 +02:00
def __str__(self):
raise NotImplementedError
2018-04-07 01:23:03 +02:00
def require(self):
self.required = True
for require in self.requires:
if require.required:
continue
require.require()
2018-05-20 12:41:52 +02:00
def has_side_effects(self):
return self.required
2018-04-07 01:23:03 +02:00
class RawExpression(Expression):
def __init__(self, text):
super(RawExpression, self).__init__()
self.text = text
def __str__(self):
2018-05-20 12:41:52 +02:00
return str(self.text)
2018-04-07 01:23:03 +02:00
# pylint: disable=redefined-builtin
2018-04-07 01:23:03 +02:00
class AssignmentExpression(Expression):
def __init__(self, type, modifier, name, rhs, obj):
2018-04-07 01:23:03 +02:00
super(AssignmentExpression, self).__init__()
self.type = type
self.modifier = modifier
self.name = name
2018-04-07 01:23:03 +02:00
self.rhs = safe_exp(rhs)
self.requires.append(self.rhs)
self.obj = obj
2018-04-07 01:23:03 +02:00
def __str__(self):
type_ = self.type
if core.SIMPLIFY:
type_ = u'auto'
return u"{} {}{} = {}".format(type_, self.modifier, self.name, self.rhs)
2018-04-07 01:23:03 +02:00
2018-05-20 12:41:52 +02:00
def has_side_effects(self):
return self.rhs.has_side_effects()
2018-04-07 01:23:03 +02:00
class ExpressionList(Expression):
def __init__(self, *args):
super(ExpressionList, self).__init__()
# Remove every None on end
args = list(args)
while args and args[-1] is None:
args.pop()
self.args = []
for arg in args:
exp = safe_exp(arg)
self.requires.append(exp)
self.args.append(exp)
2018-04-07 01:23:03 +02:00
def __str__(self):
2018-05-20 12:41:52 +02:00
text = u", ".join(str(x) for x in self.args)
2018-04-07 01:23:03 +02:00
return indent_all_but_first_and_last(text)
2018-05-14 11:50:56 +02:00
class TemplateArguments(Expression):
def __init__(self, *args):
super(TemplateArguments, self).__init__()
self.args = ExpressionList(*args)
self.requires.append(self.args)
def __str__(self):
return u'<{}>'.format(self.args)
2018-04-07 01:23:03 +02:00
class CallExpression(Expression):
def __init__(self, base, *args):
super(CallExpression, self).__init__()
self.base = base
2018-05-14 11:50:56 +02:00
if args and isinstance(args[0], TemplateArguments):
self.template_args = args[0]
self.requires.append(self.template_args)
args = args[1:]
else:
self.template_args = None
2018-04-07 01:23:03 +02:00
self.args = ExpressionList(*args)
self.requires.append(self.args)
2018-04-07 01:23:03 +02:00
def __str__(self):
2018-05-14 11:50:56 +02:00
if self.template_args is not None:
return u'{}{}({})'.format(self.base, self.template_args, self.args)
2018-04-07 01:23:03 +02:00
return u'{}({})'.format(self.base, self.args)
class StructInitializer(Expression):
def __init__(self, base, *args):
super(StructInitializer, self).__init__()
self.base = base
2018-05-20 12:41:52 +02:00
if isinstance(base, Expression):
self.requires.append(base)
2018-04-07 01:23:03 +02:00
if not isinstance(args, OrderedDict):
args = OrderedDict(args)
self.args = OrderedDict()
for key, value in args.iteritems():
if value is None:
continue
exp = safe_exp(value)
self.args[key] = exp
self.requires.append(exp)
2018-04-07 01:23:03 +02:00
def __str__(self):
cpp = u'{}{{\n'.format(self.base)
2018-04-07 01:23:03 +02:00
for key, value in self.args.iteritems():
cpp += u' .{} = {},\n'.format(key, value)
cpp += u'}'
return cpp
2018-04-07 01:23:03 +02:00
class ArrayInitializer(Expression):
def __init__(self, *args, **kwargs):
2018-04-07 01:23:03 +02:00
super(ArrayInitializer, self).__init__()
self.multiline = kwargs.get('multiline', True)
self.args = []
for arg in args:
if arg is None:
continue
exp = safe_exp(arg)
self.args.append(exp)
self.requires.append(exp)
2018-04-07 01:23:03 +02:00
def __str__(self):
if not self.args:
return u'{}'
if self.multiline:
cpp = u'{\n'
for arg in self.args:
cpp += u' {},\n'.format(arg)
cpp += u'}'
else:
cpp = u'{' + u', '.join(str(arg) for arg in self.args) + u'}'
return cpp
2018-04-07 01:23:03 +02:00
2018-05-20 12:41:52 +02:00
# pylint: disable=invalid-name
class ParameterExpression(Expression):
def __init__(self, type, id):
super(ParameterExpression, self).__init__()
self.type = type
self.id = id
def __str__(self):
return u"{} {}".format(self.type, self.id)
class ParameterListExpression(Expression):
def __init__(self, *parameters):
super(ParameterListExpression, self).__init__()
self.parameters = []
for parameter in parameters:
if not isinstance(parameter, ParameterExpression):
parameter = ParameterExpression(*parameter)
self.parameters.append(parameter)
self.requires.append(parameter)
def __str__(self):
return u", ".join(unicode(x) for x in self.parameters)
class LambdaExpression(Expression):
def __init__(self, parts, parameters, capture='=', return_type=None):
super(LambdaExpression, self).__init__()
self.parts = parts
if not isinstance(parameters, ParameterListExpression):
parameters = ParameterListExpression(*parameters)
self.parameters = parameters
self.requires.append(self.parameters)
self.capture = capture
self.return_type = return_type
if return_type is not None:
self.requires.append(return_type)
for i in range(1, len(parts), 2):
self.requires.append(parts[i])
def __str__(self):
cpp = u'[{}]({})'.format(self.capture, self.parameters)
if self.return_type is not None:
cpp += u' -> {}'.format(self.return_type)
cpp += u' {\n'
for part in self.parts:
cpp += unicode(part)
cpp += u'\n}'
return indent_all_but_first_and_last(cpp)
2018-04-07 01:23:03 +02:00
class Literal(Expression):
def __str__(self):
raise NotImplementedError
2018-04-07 01:23:03 +02:00
2018-05-15 11:09:27 +02:00
# From https://stackoverflow.com/a/14945195/8924614
2018-05-17 19:57:55 +02:00
def cpp_string_escape(string, encoding='utf-8'):
if isinstance(string, unicode):
string = string.encode(encoding)
2018-05-15 11:09:27 +02:00
result = ''
2018-05-17 19:57:55 +02:00
for character in string:
if not (32 <= ord(character) < 127) or character in ('\\', '"'):
result += '\\%03o' % ord(character)
2018-05-15 11:09:27 +02:00
else:
2018-05-17 19:57:55 +02:00
result += character
2018-05-15 11:09:27 +02:00
return '"' + result + '"'
2018-04-07 01:23:03 +02:00
class StringLiteral(Literal):
def __init__(self, string):
2018-04-07 01:23:03 +02:00
super(StringLiteral, self).__init__()
self.string = string
2018-04-07 01:23:03 +02:00
def __str__(self):
2018-05-15 11:09:27 +02:00
return u'{}'.format(cpp_string_escape(self.string))
2018-04-07 01:23:03 +02:00
class IntLiteral(Literal):
def __init__(self, i):
super(IntLiteral, self).__init__()
self.i = i
def __str__(self):
return unicode(self.i)
class BoolLiteral(Literal):
def __init__(self, binary):
2018-04-07 01:23:03 +02:00
super(BoolLiteral, self).__init__()
self.binary = binary
2018-04-07 01:23:03 +02:00
def __str__(self):
return u"true" if self.binary else u"false"
2018-04-07 01:23:03 +02:00
class HexIntLiteral(Literal):
def __init__(self, i):
super(HexIntLiteral, self).__init__()
self.i = HexInt(i)
def __str__(self):
return str(self.i)
class FloatLiteral(Literal):
2018-05-20 12:41:52 +02:00
def __init__(self, value):
2018-04-07 01:23:03 +02:00
super(FloatLiteral, self).__init__()
2018-05-20 12:41:52 +02:00
self.float_ = value
2018-04-07 01:23:03 +02:00
def __str__(self):
return u"{:f}f".format(self.float_)
2018-04-07 01:23:03 +02:00
def safe_exp(obj):
if isinstance(obj, Expression):
return obj
elif isinstance(obj, bool):
return BoolLiteral(obj)
elif isinstance(obj, (str, unicode)):
2018-04-07 01:23:03 +02:00
return StringLiteral(obj)
2018-05-14 11:50:56 +02:00
elif isinstance(obj, HexInt):
return HexIntLiteral(obj)
2018-04-07 01:23:03 +02:00
elif isinstance(obj, (int, long)):
return IntLiteral(obj)
elif isinstance(obj, float):
return FloatLiteral(obj)
2018-05-14 11:50:56 +02:00
elif isinstance(obj, TimePeriodMicroseconds):
return IntLiteral(int(obj.total_microseconds))
elif isinstance(obj, TimePeriodMilliseconds):
return IntLiteral(int(obj.total_milliseconds))
elif isinstance(obj, TimePeriodSeconds):
return IntLiteral(int(obj.total_seconds))
2018-04-07 01:23:03 +02:00
raise ValueError(u"Object is not an expression", obj)
class Statement(object):
def __init__(self):
pass
def __str__(self):
raise NotImplementedError
2018-04-07 01:23:03 +02:00
class RawStatement(Statement):
def __init__(self, text):
super(RawStatement, self).__init__()
self.text = text
def __str__(self):
return self.text
class ExpressionStatement(Statement):
def __init__(self, expression):
super(ExpressionStatement, self).__init__()
self.expression = safe_exp(expression)
def __str__(self):
return u"{};".format(self.expression)
def statement(expression):
if isinstance(expression, Statement):
return expression
return ExpressionStatement(expression)
2018-06-02 22:22:20 +02:00
def register_variable(id, obj):
_LOGGER.debug("Registered variable %s of type %s", id.id, id.type)
_VARIABLES[id] = obj
2018-05-20 12:41:52 +02:00
# pylint: disable=redefined-builtin, invalid-name
2018-06-02 22:22:20 +02:00
def variable(id, rhs, type=None):
2018-04-07 01:23:03 +02:00
rhs = safe_exp(rhs)
obj = MockObj(id, u'.')
2018-06-02 22:22:20 +02:00
id.type = type or id.type
assignment = AssignmentExpression(id.type, '', id, rhs, obj)
add(assignment)
2018-06-02 22:22:20 +02:00
register_variable(id, obj)
obj.requires.append(assignment)
2018-04-07 01:23:03 +02:00
return obj
2018-06-02 22:22:20 +02:00
def Pvariable(id, rhs, has_side_effects=True, type=None):
2018-04-07 01:23:03 +02:00
rhs = safe_exp(rhs)
2018-05-20 12:41:52 +02:00
if not has_side_effects and hasattr(rhs, '_has_side_effects'):
# pylint: disable=attribute-defined-outside-init, protected-access
rhs._has_side_effects = False
obj = MockObj(id, u'->', has_side_effects=has_side_effects)
2018-06-02 22:22:20 +02:00
id.type = type or id.type
assignment = AssignmentExpression(id.type, '*', id, rhs, obj)
add(assignment)
2018-06-02 22:22:20 +02:00
register_variable(id, obj)
obj.requires.append(assignment)
2018-04-07 01:23:03 +02:00
return obj
2018-06-03 07:11:11 +02:00
_TASKS = deque()
2018-04-07 01:23:03 +02:00
_VARIABLES = {}
_EXPRESSIONS = []
2018-06-02 22:22:20 +02:00
def get_variable(id):
while True:
if id in _VARIABLES:
yield _VARIABLES[id]
return
_LOGGER.debug("Waiting for variable %s", id)
yield None
2018-04-07 01:23:03 +02:00
2018-05-20 12:41:52 +02:00
def process_lambda(value, parameters, capture='=', return_type=None):
2018-05-21 17:01:47 +02:00
if value is None:
2018-06-02 22:22:20 +02:00
yield
return
parts = value.parts[:]
2018-06-03 19:22:25 +02:00
for i, id in enumerate(value.requires_ids):
2018-06-02 22:22:20 +02:00
var = None
2018-06-03 19:22:25 +02:00
for var in get_variable(id):
2018-06-02 22:22:20 +02:00
yield
2018-06-03 19:22:25 +02:00
parts[i*2 + 1] = var._
2018-06-02 22:22:20 +02:00
yield LambdaExpression(parts, parameters, capture, return_type)
return
2018-05-20 12:41:52 +02:00
def templatable(value, input_type, output_type):
if isinstance(value, Lambda):
2018-06-02 22:22:20 +02:00
lambda_ = None
for lambda_ in process_lambda(value, [(input_type, 'x')], return_type=output_type):
yield None
yield lambda_
else:
yield value
2018-06-03 07:11:11 +02:00
def add_job(func, *args, **kwargs):
domain = kwargs.get('domain')
2018-06-02 22:22:20 +02:00
if inspect.isgeneratorfunction(func):
def func_():
yield
2018-06-03 07:11:11 +02:00
for _ in func(*args):
2018-06-02 22:22:20 +02:00
yield
else:
def func_():
yield
2018-06-03 07:11:11 +02:00
func(*args)
gen = func_()
_TASKS.append((gen, domain))
return gen
2018-06-02 22:22:20 +02:00
2018-06-03 07:11:11 +02:00
def flush_tasks():
i = 0
while _TASKS:
i += 1
if i > 1000000:
raise ESPHomeYAMLError("Circular dependency detected!")
2018-06-02 22:22:20 +02:00
2018-06-03 07:11:11 +02:00
task, domain = _TASKS.popleft()
2018-06-03 11:18:53 +02:00
_LOGGER.debug("Executing task for domain=%s", domain)
2018-06-02 22:22:20 +02:00
try:
task.next()
2018-06-03 07:11:11 +02:00
_TASKS.append((task, domain))
2018-06-02 22:22:20 +02:00
except StopIteration:
2018-06-03 11:18:53 +02:00
_LOGGER.debug(" -> %s finished", domain)
2018-06-02 22:22:20 +02:00
pass
2018-04-07 01:23:03 +02:00
def add(expression, require=True):
if require and isinstance(expression, Expression):
expression.require()
2018-04-07 01:23:03 +02:00
_EXPRESSIONS.append(expression)
2018-06-03 11:18:53 +02:00
_LOGGER.debug("Adding: %s", statement(expression))
2018-04-07 01:23:03 +02:00
return expression
class MockObj(Expression):
2018-05-20 12:41:52 +02:00
def __init__(self, base, op=u'.', has_side_effects=True):
2018-04-07 01:23:03 +02:00
self.base = base
self.op = op
2018-05-20 12:41:52 +02:00
self._has_side_effects = has_side_effects
2018-04-07 01:23:03 +02:00
super(MockObj, self).__init__()
def __getattr__(self, attr):
2018-05-20 12:41:52 +02:00
if attr == u'_':
obj = MockObj(u'{}{}'.format(self.base, self.op))
obj.requires.append(self)
return obj
if attr == u'new':
obj = MockObj(u'new {}'.format(self.base), u'->')
obj.requires.append(self)
return obj
2018-04-07 01:23:03 +02:00
next_op = u'.'
2018-05-20 12:41:52 +02:00
if attr.startswith(u'P') and self.op != '::':
2018-04-07 01:23:03 +02:00
attr = attr[1:]
next_op = u'->'
2018-05-20 12:41:52 +02:00
if attr.startswith(u'_'):
attr = attr[1:]
obj = MockObj(u'{}{}{}'.format(self.base, self.op, attr), next_op)
obj.requires.append(self)
return obj
2018-04-07 01:23:03 +02:00
def __call__(self, *args, **kwargs):
call = CallExpression(self.base, *args)
obj = MockObj(call, self.op)
obj.requires.append(self)
obj.requires.append(call)
return obj
2018-04-07 01:23:03 +02:00
def __str__(self):
return unicode(self.base)
def require(self):
self.required = True
for require in self.requires:
if require.required:
continue
require.require()
2018-04-07 01:23:03 +02:00
2018-05-20 12:41:52 +02:00
def template(self, args):
if not isinstance(args, TemplateArguments):
args = TemplateArguments(args)
obj = MockObj(u'{}{}'.format(self.base, args))
obj.requires.append(self)
obj.requires.append(args)
return obj
def namespace(self, name):
obj = MockObj(u'{}{}{}'.format(self.base, self.op, name), u'::')
obj.requires.append(self)
return obj
def has_side_effects(self):
return self._has_side_effects
2018-04-07 01:23:03 +02:00
2018-05-20 12:41:52 +02:00
global_ns = MockObj('', '')
float_ = global_ns.namespace('float')
bool_ = global_ns.namespace('bool')
std_ns = global_ns.namespace('std')
std_string = std_ns.string
uint8 = global_ns.namespace('uint8_t')
uint16 = global_ns.namespace('uint16_t')
uint32 = global_ns.namespace('uint32_t')
NAN = global_ns.namespace('NAN')
esphomelib_ns = global_ns # using namespace esphomelib;
NoArg = esphomelib_ns.NoArg
App = esphomelib_ns.App
Application = esphomelib_ns.namespace('Application')
optional = esphomelib_ns.optional
GPIOPin = esphomelib_ns.GPIOPin
GPIOOutputPin = esphomelib_ns.GPIOOutputPin
GPIOInputPin = esphomelib_ns.GPIOInputPin
2018-04-07 01:23:03 +02:00
def get_gpio_pin_number(conf):
if isinstance(conf, int):
return conf
return conf[CONF_NUMBER]
2018-05-17 19:57:55 +02:00
def generic_gpio_pin_expression_(conf, mock_obj, default_mode):
if conf is None:
2018-06-02 22:22:20 +02:00
return
2018-05-17 19:57:55 +02:00
number = conf[CONF_NUMBER]
inverted = conf.get(CONF_INVERTED)
2018-05-14 11:50:56 +02:00
if CONF_PCF8574 in conf:
2018-06-02 22:22:20 +02:00
hub = None
for hub in get_variable(conf[CONF_PCF8574]):
yield None
2018-05-06 15:56:12 +02:00
if default_mode == u'INPUT':
2018-05-17 19:57:55 +02:00
mode = conf.get(CONF_MODE, u'INPUT')
2018-06-02 22:22:20 +02:00
yield hub.make_input_pin(number,
RawExpression('PCF8574_' + mode),
inverted)
return
2018-05-06 15:56:12 +02:00
elif default_mode == u'OUTPUT':
2018-06-02 22:22:20 +02:00
yield hub.make_output_pin(number, inverted)
return
2018-05-06 15:56:12 +02:00
else:
raise ESPHomeYAMLError(u"Unknown default mode {}".format(default_mode))
2018-05-17 19:57:55 +02:00
if len(conf) == 1:
2018-06-02 22:22:20 +02:00
yield IntLiteral(number)
return
2018-05-17 19:57:55 +02:00
mode = RawExpression(conf.get(CONF_MODE, default_mode))
2018-06-02 22:22:20 +02:00
yield mock_obj(number, mode, inverted)
2018-04-07 01:23:03 +02:00
2018-05-17 19:57:55 +02:00
def gpio_output_pin_expression(conf):
2018-06-02 22:22:20 +02:00
exp = None
for exp in generic_gpio_pin_expression_(conf, GPIOOutputPin, 'OUTPUT'):
yield None
yield exp
2018-04-07 01:23:03 +02:00
2018-05-17 19:57:55 +02:00
def gpio_input_pin_expression(conf):
2018-06-02 22:22:20 +02:00
exp = None
for exp in generic_gpio_pin_expression_(conf, GPIOInputPin, 'INPUT'):
yield None
yield exp
2018-04-07 01:23:03 +02:00
def setup_mqtt_component(obj, config):
if CONF_RETAIN in config:
add(obj.set_retain(config[CONF_RETAIN]))
if not config.get(CONF_DISCOVERY, True):
add(obj.disable_discovery())
if CONF_STATE_TOPIC in config:
add(obj.set_custom_state_topic(config[CONF_STATE_TOPIC]))
if CONF_COMMAND_TOPIC in config:
add(obj.set_custom_command_topic(config[CONF_COMMAND_TOPIC]))
if CONF_AVAILABILITY in config:
availability = config[CONF_AVAILABILITY]
add(obj.set_availability(availability[CONF_TOPIC], availability[CONF_PAYLOAD_AVAILABLE],
availability[CONF_PAYLOAD_NOT_AVAILABLE]))
2018-04-07 01:23:03 +02:00
# shlex's quote for Python 2.7
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
def quote(s):
"""Return a shell-escaped version of the string *s*."""
if not s:
return u"''"
if _find_unsafe(s) is None:
return s
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return u"'" + s.replace(u"'", u"'\"'\"'") + u"'"
def color(the_color, message='', reset=None):
2018-04-07 01:23:03 +02:00
"""Color helper."""
from colorlog.escape_codes import escape_codes, parse_colors
if not message:
return parse_colors(the_color)
return parse_colors(the_color) + message + escape_codes[reset or 'reset']