I have started experimenting with this script and love it. I have everything figured out and working but just wanted to find out a little about the Home Automation part of the addon.
I know very little about Python but do know basic programming in C++ and Python doesn't seem much different. I have had a look at 2 different HA script files in this thread and the first (which I assume was an original) has comments to say "Add Code Here" where you could add the code you wanted to run. But the home_automation(2).py script which has been "cleaned up" does not have these pointers. In any event, I wanted to ask the following:
I have an ELK M1 automation system and the full SDK software for the system. It controls everything in my home (lights, fish tanks, media devices etc) and is simple to control from ASCII commands. I have created the following test script that will trigger the MESSAGE I send just fine:
Code:
# PYTHON SCRIPT TO SEND COMMANDS TO ELK M1
# VERSION: 1.0 (30 APRIL 2013)
#
# Use the M1 SDK to generate ASCII commands
# Copy the ASCII command to the MESSAGE and replace the ^ character with \r
#
import socket
M1IP = "192.168.0.251"
M1PORT = 2101
MESSAGE=b'0Ecn0030000300D4\r'
#Code Start
srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srvsock.settimeout(3) # 3 second timeout on commands
srvsock.connect((M1IP, M1PORT))
srvsock.sendall(MESSAGE)
data = srvsock.recv(4096)
#returns result from panel (for troubleshooting)
print ("received message:", data.decode("UTF-8"))
srvsock.close()
The Message simply pulses an output for 3 seconds as a test and works perfectly. I assume I should be able to slot the code into the automation script where I would like to trigger something and it should be able to send the command to my system?
Code:
# This module's future home should be inside userdata/addon_data/script.cinema.experience/ha_scripts
# to make sure it does not get over written when updating the script
import xbmc, xbmcaddon
import socket
M1IP = "192.168.0.251"
M1PORT = 2101
MESSAGE=b'0Ecn0030000300D4\r'
_A_ = xbmcaddon.Addon('script.cinema.experience')
_L_ = _A_.getLocalizedString
_S_ = _A_.getSetting
def activate_on( trigger = "None" ):
"""
Scripting to trigger almost anything(HA, other scripts, etc...) when videos start.
Usage:
activate_on( "Movie" )
will trigger code that is set under the Movie heading.
"""
if trigger == "None":
xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - No Trigger Sent, Returning", level=xbmc.LOGNOTICE )
return
xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - activate_on( %s ) Triggered" % trigger, level=xbmc.LOGNOTICE )
# Script Start
if trigger == _L_( 32613 ) and _S_( "ha_script_start" ) == "true":
xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - %s Triggered" % _L_( 32613 ), level=xbmc.LOGNOTICE )
# place code below this line
xbmc.executehttpapi( "Broadcast(<b>CE_Automate<li>script_start</b>)" )
# Trivia Intro
elif trigger == _L_( 32609 ) and _S_( "ha_trivia_intro" ) == "true":
xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - %s Triggered" % _L_( 32609 ), level=xbmc.LOGNOTICE )
# place code below this line
########################################################
srvsock.settimeout(3) # 3 second timeout on commands
srvsock.connect((M1IP, M1PORT))
srvsock.sendall(MESSAGE)
########################################################
xbmc.executehttpapi( "Broadcast(<b>CE_Automate<li>trivia_intro</b>)" )
The example above with my code between the comment lines, would that work if I wanted the event to happen on the trivia intro? I just want to be sure that what I am trying would be possible before investing time on it.