Skip to content

Commit

Permalink
Power on device when changing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Zaorski authored and Sebastian Zaorski committed Aug 20, 2019
1 parent 1a5511b commit c813bc7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
2 changes: 1 addition & 1 deletion custom_components/climate_ip/connection_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
60 changes: 18 additions & 42 deletions custom_components/climate_ip/samsung_2878.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -275,23 +249,25 @@ 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:
message = template.render(**params) + '\n'
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)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/climate_ip/samsung_2878.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ device:
host: "__CLIMATE_IP_HOST__"
token: '__CLIMATE_IP_TOKEN__'
connection_template: '<Request Type="AuthToken"><User Token="{{token}}" /></Request>'
power_template: '{% if value != none %}{% for key, value in device_state.items() %}{% if key == "AC_FUN_POWER" %}{% if value == "Off" %}<Request Type="DeviceControl"><Control CommandID="AC_FUN_POWER" DUID="{{duid}}"><Attr ID="AC_FUN_POWER" Value="On" /></Control></Request>{% endif %}{% endif %}{% endfor %}{% endif %}'
status:
type: 'json_status'
connection_template: '<Request Type="DeviceState" DUID="{{duid}}"></Request>'
Expand All @@ -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: '<Request Type="DeviceControl"><Control CommandID="{% if value == "Off" %}AC_FUN_POWER{% else %}AC_FUN_OPMODE{% endif %}" DUID="{{duid}}"><Attr ID="{% if value == "Off" %}AC_FUN_POWER{% else %}AC_FUN_OPMODE{% endif %}" Value="{{value}}" /></Control></Request>'
# connection_template: '<Request Type="DeviceControl"><Control CommandID="{% if value == "Off" %}AC_FUN_POWER{% else %}AC_FUN_OPMODE{% endif %}" DUID="{{duid}}"><Attr ID="{% if value == "Off" %}AC_FUN_POWER{% else %}AC_FUN_OPMODE{% endif %}" Value="{{value}}" /></Control><Control CommandID="AC_FUN_POWER" DUID="{{duid}}"><Attr ID="AC_FUN_POWER" Value="{% if value == "Off" %}Off{% else %}On{% endif %}" /></Control></Request>'
preset: # preset_mode
type: modes
values:
Expand Down
1 change: 1 addition & 0 deletions custom_components/climate_ip/yaml_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit c813bc7

Please sign in to comment.