Jython: harmony to dummy for use with alexa

Hello everyone, I’d like to share what I have done to my harmony using the jython scripting engine.
The basic motivation for the this is to expose the activities to alexa.

Benefits

The main benefit of this is, that there is no need to touch the rule if you want to add another activity, all you have to do is add another dummy item.

Requirements

Items

// Groups
Group:Switch gHarmonyDummy

// Harmony Hub
String harmonyLivingActivity_current "Harmony current activity" (gHistory) {channel="harmonyhub:hub:HarmonyHub:currentActivity"}

// Dummy Items for Alexa
Switch dummyLivingActivity_Kodi "Heimkino" (gHarmonyDummy, gHistory) ["Switchable"]
Switch dummyLivingActivity_Playstation "Playstation" (gHarmonyDummy, gHistory) ["Switchable"]

The dummy items need to meet a few restrictions:

  • they have to be groupmembers of the same group.
  • they have to contain only one underscore _ , followed by the exact activity name (thus the activityname may not contain spaces, I might think about something to change that in the future)

Jython Script

scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")
from openhab.log import logging
from openhab.triggers import item_group_triggered, item_triggered
from openhab import osgi

log = logging.getLogger("org.eclipse.smarthome.automation")
item_registry = osgi.get_service("org.eclipse.smarthome.core.items.ItemRegistry")


@item_group_triggered("gHarmonyDummy", event_types=["ItemCommandEvent"])
def dummy2Harmony(event):
    desired_activity = event.itemName.split('_')[1]
    log.info("dummy2Harmony: " + event.itemName + " received command: " + event.itemCommand.toString() + ";  desired_activity = " + desired_activity)
    if event.itemCommand.toString() == "OFF":
        events.sendCommand("harmonyLivingActivity_current", "PowerOff")
    else:
        events.sendCommand("harmonyLivingActivity_current", desired_activity)


@item_triggered("harmonyLivingActivity_current")
def harmony2dummy(event):
    current_activity = event.itemState.toString()
    log.info("harmony2dummy:  Harmony activity update detected, now: " + current_activity)
    for item in item_registry.getItem("gHarmonyDummy").getMembers():
        if current_activity == item.getName().split('_')[1]:
            events.postUpdate(item.getName(), "ON")
        else:
            events.postUpdate(item.getName(), "OFF")

Maybe it is interesting for someone maybe not, in any way it is more jython stuff in this forum, so thats nice i guess :smiley:
I am open for any improvements btw, since I am pretty new to the whole JSR223 scripting engine.

2 Likes