From f2d6817d8af8d6d113d1c96bc87289ba1880526c Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 30 May 2019 13:12:12 +0200 Subject: [PATCH] Register light conditions (#570) --- esphome/components/light/automation.h | 2 +- esphome/components/light/automation.py | 15 ++++++++++++++- esphome/components/light/types.py | 2 ++ tests/test1.yaml | 2 ++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/esphome/components/light/automation.h b/esphome/components/light/automation.h index af295df369..70c423af0a 100644 --- a/esphome/components/light/automation.h +++ b/esphome/components/light/automation.h @@ -89,7 +89,7 @@ template class LightIsOnCondition : public Condition { protected: LightState *state_; }; -template class LightIsOffCondition : public Condition { +template class LightIsOffCondition : public Condition { public: explicit LightIsOffCondition(LightState *state) : state_(state) {} bool check(Ts... x) override { return !this->state_->current_values.is_on(); } diff --git a/esphome/components/light/automation.py b/esphome/components/light/automation.py index 629073707e..41074c098d 100644 --- a/esphome/components/light/automation.py +++ b/esphome/components/light/automation.py @@ -5,7 +5,7 @@ from esphome.const import CONF_ID, CONF_TRANSITION_LENGTH, CONF_STATE, CONF_FLAS CONF_EFFECT, CONF_BRIGHTNESS, CONF_RED, CONF_GREEN, CONF_BLUE, CONF_WHITE, \ CONF_COLOR_TEMPERATURE, CONF_RANGE_FROM, CONF_RANGE_TO from .types import DimRelativeAction, ToggleAction, LightState, LightControlAction, \ - AddressableLightState, AddressableSet + AddressableLightState, AddressableSet, LightIsOnCondition, LightIsOffCondition @automation.register_action('light.toggle', ToggleAction, automation.maybe_simple_id({ @@ -145,3 +145,16 @@ def light_addressable_set_to_code(config, action_id, template_arg, args): templ = yield cg.templatable(config[CONF_WHITE], args, cg.uint8, to_exp=rgbw_to_exp) cg.add(var.set_white(templ)) yield var + + +@automation.register_condition('light.is_on', LightIsOnCondition, + automation.maybe_simple_id({ + cv.Required(CONF_ID): cv.use_id(LightState), + })) +@automation.register_condition('light.is_off', LightIsOffCondition, + automation.maybe_simple_id({ + cv.Required(CONF_ID): cv.use_id(LightState), + })) +def binary_sensor_is_on_to_code(config, condition_id, template_arg, args): + paren = yield cg.get_variable(config[CONF_ID]) + yield cg.new_Pvariable(condition_id, template_arg, paren) diff --git a/esphome/components/light/types.py b/esphome/components/light/types.py index 33ba759df0..9be6cc8319 100644 --- a/esphome/components/light/types.py +++ b/esphome/components/light/types.py @@ -16,6 +16,8 @@ ToggleAction = light_ns.class_('ToggleAction', automation.Action) LightControlAction = light_ns.class_('LightControlAction', automation.Action) DimRelativeAction = light_ns.class_('DimRelativeAction', automation.Action) AddressableSet = light_ns.class_('AddressableSet', automation.Action) +LightIsOnCondition = light_ns.class_('LightIsOnCondition', automation.Condition) +LightIsOffCondition = light_ns.class_('LightIsOffCondition', automation.Condition) # Effects LightEffect = light_ns.class_('LightEffect') diff --git a/tests/test1.yaml b/tests/test1.yaml index a9f49e789f..b5646762e8 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -84,6 +84,8 @@ mqtt: condition: - wifi.connected: - mqtt.connected: + - light.is_on: kitchen + - light.is_off: kitchen then: - lambda: |- int data = x["my_data"];