Repository navigation
Expand file tree
/
Copy pathimportexample.py
More file actions
133 lines (113 loc) · 5.52 KB
/
Copy pathimportexample.py
File metadata and controls
133 lines (113 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
"""
importexample.py
This script:
* demonstrates how to import the CM19a driver module and use it to send and receive commands via the CM19a; and
* provides an example of a simple macro that runs when a particular command is received from an RF remote
Version 3.0
Sept 2011
Andrew Cuddon
www.cuddon.net
"""
# *************** CONFIGURATION ***************
POLLFREQ = 1 # Polling Frequency - Check for inbound commands every 1 second
# *************** CODE ***************
VERSION = "3.00 (Beta)"
# Std Python modules
import sys, time, re
# CM19a module
import cm19aDriver
# Start logging
log = cm19adriver.startLogging() # log is an instance of the logger class
# Configure the CM19a and start polling for inbound commands from a remote
# Initialise device. Note: auto polling/receiving in a thread is turned ON
cm19a = cm19adriver.CM19aDevice(POLLFREQ, log, polling = True) # cm19a is an instance of the CM19aDevice class
if cm19a.initialised:
# Firstly some simple send commands
# Send the command 'ON' to X10 device A1 wirelessly via the CM19a (and a receiver of course)
print "Turning ON A1..."
result = cm19a.send("A", "1", "ON") # True if the command was sent OK
if result:
print "...Success"
else:
print >> sys.stderr, "Command failed"
print "Waiting 5 seconds (so we can check that the device has turned on)..."
time.sleep(5)
print "Now turning OFF A1..."
result = cm19a.send("a", "1", "OFf") # notice that this is case insensitive
if result:
print "...Success"
else:
print >> sys.stderr, "Command failed"
# Now a simple macro
print '\n\n---- Basic Macros initiated by a key press on an X10 remote control ----'
print 'Press any button on the remote. A4ON will run a macro, A4OFF will exit this programme'
alive = True
lasthouse = ''
lastunit = ''
while alive:
# Check for any button presses from an X10 remote
buttonpresses = cm19a.getReceiveQueue() # -> List
if buttonpresses:
# One or more button presses were received so loop through the list of button presses
for button in buttonpresses:
print "Actioning button press: %s" % button
# Use a regular expression to extract the house code, unit number, and command
b = re.search(r'^(.)(\d+)(.+)', button) # start of string, 1 character (house code), 1 or more digits (unit number), 1 or more characters (command)
if b:
house = b.group(1).upper()
unit = b.group(2).upper()
command = b.group(3).upper()
else:
print >> sys.stderr, "Could not extract house code etc from %s" % button
if button == "A4ON":
# Really simple macro
# When A4ON is pressed on the remote, turn on E1, dim it by 5% (assuming it is a lamp module) and turn off E2
print "\nRunning macro (A4ON) to turn ON E1, dimming it by 5%, and then turn OFF E2"
result = cm19a.send("E", "1", "ON") # True if the command was sent OK
if not result:
print >> sys.stderr, "Command failed"
result = cm19a.send("E", "1", "DIM") # True if the command was sent OK
if not result:
print >> sys.stderr, "Command failed"
result = cm19a.send("E", "2", "OFF") # True if the command was sent OK
if not result:
print >> sys.stderr, "Command failed"
lasthouse = 'E'
lastunit = '2'
print "Macro for A4ON completed."
elif button == "A4OFF":
# exit the program
print "Stopping checking for button presses."
alive = False
break
else:
# Other button pressess
if command.find('BRIGHTBUTTONPRESSED')>= 0:
print 'Bright button was pressed => Brightening the last used device..'
result = cm19a.send(lasthouse, lastunit, 'bright') # True if the command was sent OK
if not result:
print >> sys.stderr, "Command failed"
elif command.find('DIMBUTTONPRESSED')>= 0:
print 'Dim button was pressed => Dimming the last used device...'
result = cm19a.send(lasthouse, lastunit, 'dim') # True if the command was sent OK
if not result:
print >> sys.stderr, "Command failed"
else:
# Just retransmit any other command recevied
result = cm19a.send(house, unit, command) # True if the command was sent OK
if not result:
print >> sys.stderr, "Command failed"
lasthouse = house
lastunit = unit
#end for loop
else:
# No buttons pressed so do just wait a bit (2 seconds in this case) before checking again
time.sleep(2)
# end while alive loop
cm19a.finish()
else:
print "Error initialising the CM19a...exiting..."
sys.exit(1)
print "...All done"
# End of example