eventghost homeseer python

Source Homeseer plugin for EventGhost

For quite a while now I make use of EventGhost to control MediaPortal with my remote-control. Why shouldn’t I make use of the same remote to execute HomeSeer events?

Unfortunately there wasn’t any existing plugin for EventGhost doing this. That’s why I tried to quickly build something in C# (see here). However, the result wasn’t what I expected it to be, so I decided to create an EventGhost (Python) plugin myself; I must admit, Python was completely new to me.

Requisites:

Hello HomeSeer!

When EventGhost is installed on the same machine running HomeSeer, nothing extra needs to be installed. However, when a remote machine is used to control HomeSeer, then you’ll need to download and install the HomeSeer Speaker Client. As such, you can call the necessary HomeSeer COM objects from Python.

The source for the plugin is a .py file, which needs to be copied into /plugins/Homeseer located in the EventGhost folder.

Necessary imports to use the COM objects:

import win32com.client

We create a simple Homeseer class to call the HomeSeer functions:

class Homeseer():
    hsi = win32com.client.Dispatch("HomeSeer2.application")
    hs = win32com.client.Dispatch("Scheduler.hsapplication")

    ...

    def connect(self):
        print "Trying to connect to Homeseer-host " + self.hostname + " using user " + self.username + "."
        self.hsi.SetHost(self.hostname)
        rval = self.hsi.Connect(self.username, self.password)
        if rval == "":
            print "Successfully connected to Homeseer " + self.hostname + " using user " + self.username + "."
            self.connected = True
        else:
            print "Error: " + rval
            self.hsi.Disconnect()
            self.connected = False

        if self.connected:
            self.hs = self.hsi.GetHSRef

    def disconnect(self):
        if self.connected:
            self.hsi.Disconnect()
            print "Disconnected from Homeseer."
            self.connected = False

A hs.speak test function is added to easily test our setup:

def doSpeak(self, speech):
        if self.connected:
            print "Speaking " + speech
            self.hs.speak(speech)
        else:
            print "Not connected to Homeseer."

The actual function that, depending on the status of a device, sends a On or Off command to HomeSeer:

def doOnOffCommand(self, deviceCode):
        if self.connected:
            if self.hs.IsOn(deviceCode):
                command = "Off"
            else:
                command = "On"

            print "Sending command " + command + " to " + deviceCode
            self.hs.ExecX10(deviceCode, command, 0, 0, False);

        else:
            print "Not connected to Homeseer."

The actual EventGhost plugin which will call the HomeSeer class:

class HomeseerPlugin(eg.PluginBase):

    ...

    def __start__(self, hostname, username, password):
        global HOMESEERINSTANCE
        HOMESEERINSTANCE = Homeseer(hostname, username, password)
        HOMESEERINSTANCE.connect()

    def __stop__(self):
        HOMESEERINSTANCE.disconnect()  

    ...

The actual EventGhost Actions which will call HomeSeer commands:

class OnOffCommand(eg.ActionBase):

    def __call__(self, deviceCode):
        HOMESEERINSTANCE.doOnOffCommand(deviceCode)
    ...

class Speak(eg.ActionBase):

    def __call__(self, speech):
        HOMESEERINSTANCE.doSpeak(speech)
    ...

Download the full source code here.

References: