From 3a9febaf855f2c5bb770dab3b67d35d51b3b1f83 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 17 May 2018 17:20:43 +0200 Subject: [PATCH] Generic Switch inversion support fixes #14 --- esphomeyaml/components/switch/__init__.py | 5 ++++- esphomeyaml/components/switch/ir_transmitter.py | 3 ++- esphomeyaml/components/switch/restart.py | 5 ++++- esphomeyaml/components/switch/shutdown.py | 5 ++++- esphomeyaml/helpers.py | 14 +++++++------- esphomeyaml/writer.py | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/esphomeyaml/components/switch/__init__.py b/esphomeyaml/components/switch/__init__.py index 2ff6f13786..447c3dde42 100644 --- a/esphomeyaml/components/switch/__init__.py +++ b/esphomeyaml/components/switch/__init__.py @@ -1,7 +1,7 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.const import CONF_ICON, CONF_ID, CONF_NAME, CONF_MQTT_ID +from esphomeyaml.const import CONF_ICON, CONF_INVERTED, CONF_MQTT_ID from esphomeyaml.helpers import App, Pvariable, add, setup_mqtt_component PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -10,6 +10,7 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ MQTT_SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ vol.Optional(CONF_ICON): cv.icon, + vol.Optional(CONF_INVERTED): cv.boolean, }) MQTT_SWITCH_ID_SCHEMA = MQTT_SWITCH_SCHEMA.extend({ @@ -24,6 +25,8 @@ def setup_mqtt_switch(obj, config): def setup_switch(obj, config): if CONF_ICON in config: add(obj.set_icon(config[CONF_ICON])) + if CONF_INVERTED in config: + add(obj.set_inverted(config[CONF_INVERTED])) def register_switch(var, config): diff --git a/esphomeyaml/components/switch/ir_transmitter.py b/esphomeyaml/components/switch/ir_transmitter.py index 4614168c14..ac35498bce 100644 --- a/esphomeyaml/components/switch/ir_transmitter.py +++ b/esphomeyaml/components/switch/ir_transmitter.py @@ -5,7 +5,7 @@ from esphomeyaml.components import switch from esphomeyaml.components.ir_transmitter import IR_TRANSMITTER_COMPONENT_CLASS from esphomeyaml.const import CONF_ADDRESS, CONF_CARRIER_FREQUENCY, CONF_COMMAND, CONF_DATA, \ CONF_ID, CONF_IR_TRANSMITTER_ID, CONF_LG, CONF_NAME, CONF_NBITS, CONF_NEC, CONF_PANASONIC, \ - CONF_RAW, CONF_REPEAT, CONF_SONY, CONF_TIMES, CONF_WAIT_TIME + CONF_RAW, CONF_REPEAT, CONF_SONY, CONF_TIMES, CONF_WAIT_TIME, CONF_INVERTED from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.helpers import ArrayInitializer, HexIntLiteral, MockObj, Pvariable, get_variable @@ -45,6 +45,7 @@ PLATFORM_SCHEMA = vol.All(switch.PLATFORM_SCHEMA.extend({ vol.Optional('wait_time_us'): cv.invalid(WAIT_TIME_MESSAGE), })), vol.Optional(CONF_IR_TRANSMITTER_ID): cv.variable_id, + vol.Optional(CONF_INVERTED): cv.invalid("IR Transmitters do not support inverted mode!"), }).extend(switch.MQTT_SWITCH_ID_SCHEMA.schema), cv.has_at_least_one_key(*IR_KEYS)) # pylint: disable=invalid-name diff --git a/esphomeyaml/components/switch/restart.py b/esphomeyaml/components/switch/restart.py index 57f53a4e87..1eab632881 100644 --- a/esphomeyaml/components/switch/restart.py +++ b/esphomeyaml/components/switch/restart.py @@ -1,10 +1,13 @@ +import voluptuous as vol + import esphomeyaml.config_validation as cv from esphomeyaml.components import switch -from esphomeyaml.const import CONF_ID, CONF_NAME +from esphomeyaml.const import CONF_ID, CONF_NAME, CONF_INVERTED from esphomeyaml.helpers import App, variable PLATFORM_SCHEMA = switch.PLATFORM_SCHEMA.extend({ cv.GenerateID('restart_switch'): cv.register_variable_id, + vol.Optional(CONF_INVERTED): cv.invalid("Restart switches do not support inverted mode!"), }).extend(switch.MQTT_SWITCH_SCHEMA.schema) diff --git a/esphomeyaml/components/switch/shutdown.py b/esphomeyaml/components/switch/shutdown.py index 0f451ea6e8..e3b3f2641c 100644 --- a/esphomeyaml/components/switch/shutdown.py +++ b/esphomeyaml/components/switch/shutdown.py @@ -1,10 +1,13 @@ +import voluptuous as vol + import esphomeyaml.config_validation as cv from esphomeyaml.components import switch -from esphomeyaml.const import CONF_ID, CONF_NAME +from esphomeyaml.const import CONF_ID, CONF_NAME, CONF_INVERTED from esphomeyaml.helpers import App, variable PLATFORM_SCHEMA = switch.PLATFORM_SCHEMA.extend({ cv.GenerateID('shutdown_switch'): cv.register_variable_id, + vol.Optional(CONF_INVERTED): cv.invalid("Shutdown switches do not support inverted mode!"), }).extend(switch.MQTT_SWITCH_SCHEMA.schema) diff --git a/esphomeyaml/helpers.py b/esphomeyaml/helpers.py index 4f7d56443f..a116412493 100644 --- a/esphomeyaml/helpers.py +++ b/esphomeyaml/helpers.py @@ -186,15 +186,15 @@ class Literal(Expression): # From https://stackoverflow.com/a/14945195/8924614 -def cpp_string_escape(s, encoding='utf-8'): - if isinstance(s, unicode): - s = s.encode(encoding) +def cpp_string_escape(string, encoding='utf-8'): + if isinstance(string, unicode): + string = string.encode(encoding) result = '' - for c in s: - if not (32 <= ord(c) < 127) or c in ('\\', '"'): - result += '\\%03o' % ord(c) + for character in string: + if not (32 <= ord(character) < 127) or character in ('\\', '"'): + result += '\\%03o' % ord(character) else: - result += c + result += character return '"' + result + '"' diff --git a/esphomeyaml/writer.py b/esphomeyaml/writer.py index e9963f3f14..10394662e1 100644 --- a/esphomeyaml/writer.py +++ b/esphomeyaml/writer.py @@ -30,7 +30,7 @@ void setup() { void loop() { App.loop(); - delay(1); + delay(16); } """)