diff --git a/custom_components/climate_ip/connection_request.py b/custom_components/climate_ip/connection_request.py index d0916a1..2c78548 100755 --- a/custom_components/climate_ip/connection_request.py +++ b/custom_components/climate_ip/connection_request.py @@ -118,7 +118,7 @@ def execute(self, template, value, device_state): self.embedded_command.execute(template, value, device_state) if not self.check_execute_condition(device_state): - self.logger.warning("Execute condition not met, skipping command") + self.logger.info("Execute condition not met, skipping command") return ({}, True, 200) self.logger.info("Executing command...") diff --git a/custom_components/climate_ip/samsung_2878.py b/custom_components/climate_ip/samsung_2878.py index 6278d36..1221d33 100644 --- a/custom_components/climate_ip/samsung_2878.py +++ b/custom_components/climate_ip/samsung_2878.py @@ -1,5 +1,5 @@ from .connection import (register_connection, Connection) -from .yaml_const import (CONFIG_DEVICE_CONNECTION_PARAMS, CONFIG_DEVICE_CONDITION_TEMPLATE, +from .yaml_const import (CONFIG_DEVICE_CONNECTION_PARAMS, CONFIG_DEVICE_POWER_TEMPLATE, CONFIG_DEVICE_CONNECTION_TEMPLATE, CONF_CERT, CONFIG_DEVICE_CONNECTION, ) from homeassistant.const import (CONF_PORT, CONF_TOKEN, CONF_MAC, CONF_IP_ADDRESS) @@ -39,16 +39,7 @@ def __init__(self, hass_config, logger): self._device_status = {} self._socket_timeout = 1 # in seconds self.update_configuration_from_hass(hass_config) - self._embedded_command = None - self._condition_template = None - - @property - def embedded_command(self): - return self._embedded_command - - @property - def condition_template(self): - return self._condition_template + self._power_template = None def update_configuration_from_hass(self, hass_config): if hass_config is not None: @@ -89,11 +80,8 @@ def load_from_yaml(self, node, connection_base): self.logger.error("ERROR: missing 'connection_template' parameter in connection section") return False - if CONFIG_DEVICE_CONNECTION in node: - self._embedded_command = self.create_updated(node[CONFIG_DEVICE_CONNECTION]) - - if CONFIG_DEVICE_CONDITION_TEMPLATE in node: - self._condition_template = Template(node[CONFIG_DEVICE_CONDITION_TEMPLATE]) + if CONFIG_DEVICE_POWER_TEMPLATE in params_node: + self._power_template = Template(params_node[CONFIG_DEVICE_POWER_TEMPLATE]) if connection_base is None: if self._cfg.host is None: @@ -111,36 +99,22 @@ def load_from_yaml(self, node, connection_base): self.logger.info("Configuration, token: {}".format(self._cfg.token)) self.logger.info("Configuration, duid: {}".format(self._cfg.duid)) self.logger.info("Configuration, cert: {}".format(self._cfg.cert)) - self._params.update(node.get(CONFIG_DEVICE_CONNECTION_PARAMS, {})) + + self._params.update(params_node) return True return False - def check_execute_condition(self, device_state): - do_execute = True - self.logger.info("Checking execute condition") - if self.condition_template is not None: - self.logger.info("Execute condition found, evaluating") - try: - rendered_condition = self.condition_template.render(device_state = device_state) - self.logger.info("Execute condition evaluated: {0}".format(rendered_condition)) - do_execute = rendered_condition == '1' - except: - self.logger.error("Execute condition found, error while evaluating, executing command") - do_execute = True - else: - self.logger.warning("Execute condition not found, executing") - - return do_execute - @staticmethod def match_type(type): return type == CONNECTION_TYPE_S2878 def create_updated(self, node): + from jinja2 import Template c = ConnectionSamsung2878(None, self.logger) c._cfg = self._cfg c._connection_init_template = self._connection_init_template + c._power_template = self._power_template c.load_from_yaml(node, self) return c @@ -275,16 +249,9 @@ def socket(self): return sslSocket def execute(self, template, v, device_state): - if self.embedded_command: - self.logger.info("Embedded command found, executing...") - self.embedded_command.execute(template, value, device_state) - - if not self.check_execute_condition(device_state): - self.logger.warning("Execute condition not met, skipping command") - return self._device_status - params = self._params params.update({ 'value' : v }) + params.update({ 'device_state' : device_state }) self.logger.info("Executing params: {}".format(params)) message = v if template is not None: @@ -292,6 +259,15 @@ def execute(self, template, v, device_state): elif CONFIG_DEVICE_CONNECTION_TEMPLATE in params: message = params[CONFIG_DEVICE_CONNECTION_TEMPLATE] + self.logger.info("Checking power on template: {}".format(self._power_template)) + if self._power_template: + self.logger.info("Power on template found, rendering") + power_message = self._power_template.render(**params) + self.logger.info("Power on message: {}".format(power_message)) + if power_message and power_message != '': + self.logger.info("Executing power command") + self.send_socket_command(power_message, 1) + self.logger.info("Executing command: {}".format(message)) self.send_socket_command(message, 1) #self.handle_response_device_state(None, xml_test) diff --git a/custom_components/climate_ip/samsung_2878.yaml b/custom_components/climate_ip/samsung_2878.yaml index f3a5bbd..35f9c5f 100755 --- a/custom_components/climate_ip/samsung_2878.yaml +++ b/custom_components/climate_ip/samsung_2878.yaml @@ -8,6 +8,7 @@ device: host: "__CLIMATE_IP_HOST__" token: '__CLIMATE_IP_TOKEN__' connection_template: '' + power_template: '{% if value != none %}{% for key, value in device_state.items() %}{% if key == "AC_FUN_POWER" %}{% if value == "Off" %}{% endif %}{% endif %}{% endfor %}{% endif %}' status: type: 'json_status' connection_template: '' @@ -23,7 +24,6 @@ device: 'off': { value : 'Off' } status_template: '{% for key, value in device_state.items() %}{% if key == "AC_FUN_POWER" %}{% if value == "On" %}{% for key, value in device_state.items() %}{% if key == "AC_FUN_OPMODE" %}{{value}}{% endif %}{% endfor %}{% else %}{{value}}{% endif %}{% endif %}{% endfor %}' connection_template: '' -# connection_template: '' preset: # preset_mode type: modes values: diff --git a/custom_components/climate_ip/yaml_const.py b/custom_components/climate_ip/yaml_const.py index d56e19c..d4f139a 100755 --- a/custom_components/climate_ip/yaml_const.py +++ b/custom_components/climate_ip/yaml_const.py @@ -18,6 +18,7 @@ CONFIG_DEVICE_CONNECTION_TEMPLATE = 'connection_template' CONFIG_DEVICE_VALIDATION_TEMPLATE = 'validation_template' CONFIG_DEVICE_CONDITION_TEMPLATE = 'condition_template' +CONFIG_DEVICE_POWER_TEMPLATE = 'power_template' CONF_CERT = 'cert' CONF_DEBUG = 'debug'