-
Notifications
You must be signed in to change notification settings - Fork 0
/
pioneer_avr.py
146 lines (125 loc) · 6.61 KB
/
pioneer_avr.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''
pioneer_avr.py is part of knxadapter3.py
Copyright (C) 2020 Andreas Frisch <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
'''
import asyncio
from helper import BasePlugin, knxalog as log
def plugin_def():
return PioneerAVR
class PioneerAVR(BasePlugin):
def __init__(self, daemon, cfg):
super(PioneerAVR, self).__init__(daemon, cfg)
self.avr_reader = None
self.avr_writer = None
self.accu_word = None
daemon.knx_read_cbs.append(self.process_knx)
log.debug("{} obj_list: {!r}".format(self.device_name, self.obj_list))
async def avr_client(self):
self.avr_reader, self.avr_writer = await asyncio.open_connection(
self.cfg["host"], self.cfg["port"])
async def send_avr(self, data):
log.debug("sending to avr: '%s'" % data)
self.avr_writer.write((data+'\r').encode(encoding='ascii'))
self.avr_writer.drain()
def get_value_by_avr(self, avr_object):
return next(item for item in self.obj_list if item["avr_object"] == avr_object)["value"]
def get_knx_by_avr(self, avr_object):
return next(item for item in self.obj_list if item["avr_object"] == avr_object)["knx_group"]
def set_value_for_avr(self, avr_object, value):
next(item for item in self.obj_list if item["avr_object"] == avr_object)["value"] = value
async def process_knx(self, cmd):
msg = None
log.debug("avr processes knx command '%r'" % cmd)
try:
if cmd[0] == 'P':
if cmd[1:3] == "on" and self.get_value_by_avr("power") != "on":
msg = "PO"
elif cmd[1:4] == "off" and self.get_value_by_avr("power") != "off":
msg = "PF"
elif cmd[0] == 'V':
new_vol = int(cmd[1:])
if new_vol != self.get_value_by_avr("volume"):
avr_vol = round(new_vol * (185.0 / 255.0))
msg = "%03dVL" % avr_vol
self.set_value_for_avr("volume", new_vol)
elif cmd[0] == 'F':
new_fn = int(cmd[1:3])
if new_fn in range (0,32) and new_fn != self.get_value_by_avr("fn"):
msg = "%02dFN" % new_fn
self.set_value_for_avr("fn", new_fn)
if msg:
await self.send_avr(msg)
return True
except:
return False
async def handle_avr(self):
while True:
group_value_dict = {}
data = await self.avr_reader.readline()
if not data:
break
line = data.decode('ascii')
log.debug('avr received {!r}'.format(line))
if line.startswith('FL02'):
if line == "FL022020202020202020202020202020\r\n":
self.accu_word = self.accu_word and self.accu_word.rstrip() or ""
self.set_value_for_avr("display_text", self.accu_word)
group_value_dict[self.get_knx_by_avr("display_text")] = self.get_value_by_avr("display_text")
log.debug("display_text complete! '%s'" % self.get_value_by_avr("display_text"))
else:
new_word = bytes.fromhex(line[4:-2]).decode('iso8859_15')
if self.accu_word == None:
self.accu_word = new_word
log.debug("1START new_word={!r} accu_word={!r}".format(new_word, self.accu_word))
elif self.accu_word[-13:] != new_word[:-1]:
if not self.get_value_by_avr("display_text") or new_word not in self.get_value_by_avr("display_text"):
self.set_value_for_avr("display_text", None)
self.accu_word = new_word
group_value_dict[self.get_knx_by_avr("display_text")] = self.accu_word
log.debug("CHANGE new_word={!r} accu_word={!r}".format(new_word, self.accu_word))
else:
log.debug("STARTOVER new_word={!r} accu_word={!r}".format(new_word, self.accu_word))
else:
self.accu_word += new_word[-1:]
log.debug("+++++ new_word={!r} accu_word={!r}".format(new_word, self.accu_word))
if not self.get_value_by_avr("display_text") and self.accu_word[-1] != ' ':
group_value_dict[self.get_knx_by_avr("display_text")] = self.accu_word.rstrip()
log.debug("COMMIT new_word={!r} accu_word={!r}".format(new_word, self.accu_word))
elif line.startswith('VOL'):
avr_volume = int(line[3:])
new_volume = round(avr_volume * (255.0 / 185.0))
if new_volume != self.get_value_by_avr("volume"):
self.set_value_for_avr("volume", new_volume)
group_value_dict[self.get_knx_by_avr("volume")] = self.get_value_by_avr("volume")
elif line.startswith('PWR'):
if line[3] == '0':
self.set_value_for_avr("power", "on")
else:
self.set_value_for_avr("power", "off")
group_value_dict[self.get_knx_by_avr("power")] = self.get_value_by_avr("power")
self.set_value_for_avr("display_text", None)
group_value_dict[self.get_knx_by_avr("display_text")] = self.get_value_by_avr("display_text")
elif line.startswith('FN'):
new_fn = int(line[2:4])
if new_fn != self.get_value_by_avr("fn"):
self.set_value_for_avr("fn", new_fn)
group_value_dict[self.get_knx_by_avr("fn")] = self.get_value_by_avr("fn")
self.set_value_for_avr("display_text", None)
group_value_dict[self.get_knx_by_avr("display_text")] = self.get_value_by_avr("display_text")
if group_value_dict:
await self.d.set_group_value_dict(group_value_dict)
def _run(self):
self.client = self.d.loop.run_until_complete(self.avr_client(self.d.loop))
return [self.handle_avr()]