Repository navigation
Expand file tree
/
Copy pathautopower.py
More file actions
executable file
·55 lines (40 loc) · 1.76 KB
/
Copy pathautopower.py
File metadata and controls
executable file
·55 lines (40 loc) · 1.76 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
#!/bin/env python2
"""
Automate powered loudspeakers power with EATON UPS from Denon DN-500AV pre-amplifier state
"""
from twisted.internet import reactor
from twisted.internet.protocol import ClientFactory
from twisted.conch.telnet import TelnetTransport, TelnetProtocol # Unavailable in Python3, yet
from PyNUT import PyNUTClient
class DenonProtocol(TelnetProtocol):
ups_name = 'nutdev1' # TODO: store in a configuration file
ups_var = "outlet.2.switchable" # on means power down or off power up
ups_username = 'admin' # TODO: store in a configuration file
ups_userpass = 'ups' # TODO: store securely? in a configuration file
def connectionMade(self):
# Subscribe to the power state
self.transport.write("PW?\n")
def dataReceived(self, bytes):
ups = PyNUTClient(login=self.ups_username, password=self.ups_userpass)
if 'PWON' in bytes:
# Enable UPS sockets
ups.SetRWVar(ups=self.ups_name, var=self.ups_var, value='no')
if 'PWSTANDBY' in bytes:
# Disable UPS sockets
ups.SetRWVar(ups=self.ups_name, var=self.ups_var, value='yes')
class TelnetFactory(ClientFactory):
def buildProtocol(self, addr):
return TelnetTransport(DenonProtocol)
if __name__ == '__main__':
"""
The amplifier uses a Telnet interface (port 23) to send and receive serial commands
We can subscribe to the power state using PW?\r
The reply can be either PWON or PWSTANDBY
The UPS is an EATON powerstation
It exposes an interface through NUT to control 2 power sockets
We want them to follow the amp's state
"""
host = '192.168.1.10' # TODO: store in a configuration file
port = 23
reactor.connectTCP(host, port, TelnetFactory())
reactor.run()