Profiles via Jython

Hi,

Profiles are great way to reduce rules writing but currently it only support device to device but not item to item. As I am using proxy items, profiles in it’s current support is not helpful to me.
Using item metadata and Jython, I have added profile support that can follow item state, until, I hope, in the future, will be replaced by builtin support.

Some notes:

  • This is initial version with only follow profile
  • I will probably add more profiles in the future.
  • The current version support only single profile, planing to have the ability to define multiple profiles
  • The script uses the metadata class (see here)

Item should have the following metadata:

Switch Test_Power { profile="follow" [item="Sonoff_Power"] }

Available follow options:

  • item (Mandatory) - The followee (the other item to follow)
  • offset (Optional, default 0) - Not supported yet (will be added to Number items in the future)
  • forward (Optional, default “command”) - sendCommand (command) or postUpdate (update)

And the Jython script (002_initialize_profiles.py) is:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Init profiles
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from personal.metadata import Metadata

from core.log import logging, LOG_PREFIX

from core.triggers import ItemCommandTrigger, ItemStateUpdateTrigger

scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")

# Module logge r
log = logging.getLogger(LOG_PREFIX + ".profiles")

# Total profiles (for debugging)
profiles = 0

'''
This item follow other item state
'''
class followRule(SimpleRule):
  type = "follow"

  def __init__(self, this, other, offset = None, forward = "command"):
    self.name = "Profile: {} follow {}".format(this, other)
    self.this = this
    self.other = other
    self.forward = forward
    self.triggers = [
      ItemCommandTrigger(other).trigger,
      ItemStateUpdateTrigger(other).trigger
    ]

  def execute(self, module, input):
    if self.forward == "command" or self.forward == None:
      events.sendCommand(self.this, str(items[self.other]))
    elif self.forward == "update":
      events.postUpdate(self.this, str(items[self.other]))
    else:
      log.info("Unsupported forward type: {}".format(self.forward))

'''
Iterate all items and initialize profiles:
  - Follow  { profile="follow" [item =<name>, offset=<value>, forward=update/command] }
  - ...
'''
for item in ir.getItems():
  m = Metadata(item.name, "profile")
  if m.exist() == True:
    # Add follow profile
    if m.get_value() == followRule.type:
      # Profile configuration
      other = m.get_configuration_value_for_key("item")
      offset = m.get_configuration_value_for_key("offset")
      forward = m.get_configuration_value_for_key("forward")
      # Validate mandatory parameters
      if other != None:
        automationManager.addRule(followRule(item.name, other, offset, forward))
        profiles += 1
    # Unsupported profile
    else:
      log.info("Profile {} is not supported yet".format(m.get_value()))

log.info("Loaded {} profiles".format(profiles))

Comments are welcome

2 Likes