-
Notifications
You must be signed in to change notification settings - Fork 0
/
daikin_ac.py
146 lines (117 loc) · 5.4 KB
/
daikin_ac.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
'''
daikin_ac.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
import logging
from helper import BasePlugin, knxalog as log
from daikinapi import Daikin
from requests import ConnectionError
def plugin_def():
return DaikinAC
class DaikinAC(BasePlugin):
def __init__(self, daemon, cfg):
super(DaikinAC, self).__init__(daemon, cfg)
daemon.knx_read_cbs.append(self.process_knx)
self.poll_interval = "poll_interval" in cfg and cfg["poll_interval"] or 10
for obj in cfg["objects"]:
if obj["enabled"]:
obj.update({"value": None})
log.debug("{} obj_list: {!r}".format(self.device_name, self.obj_list))
async def handle_ac(self):
log_msg = []
while True:
self._client.receive_info()
group_value_dict = {}
for o in self.obj_list:
ac_obj = o["ac_object"]
try:
value = getattr(self._client, ac_obj)
if value == "A":
value = 0
elif value == "B":
value = 1
except KeyError:
continue
except ValueError as e:
log.debug("{} {!r} on key {}".format(self.device_name, e, ac_obj))
continue
if value == o["value"]:
log_msg.append("{!r}: {!r}".format(ac_obj, value))
continue
group_value_dict[o["knx_group"]] = str(value)
o["value"] = value
if group_value_dict:
await self.d.set_group_value_dict(group_value_dict)
if log_msg:
log.debug(self.device_name+" skipped objects: "+', '.join(log_msg))
log_msg = []
await asyncio.sleep(self.poll_interval)
def _get_value_by_acobj(self, ac_object):
return next(item for item in self.obj_list if item["ac_object"] == ac_object)["value"]
async def process_knx(self, cmd):
msg = None
try:
knx_grp, raw = cmd.split("=")
debug_msg = "{} knx group {} raw={}".format(self.device_name, knx_grp, raw)
try:
ac_obj = self.get_obj_by_knxgrp(knx_grp)["ac_object"]
except StopIteration:
log.debug("{} no AC object for given KNX group, ignored".format(debug_msg))
return True
if ac_obj == "fan_rate":
if raw == "0":
value = "A"
elif raw== "1":
value = "B"
elif raw in ["off", "false", "disable", "stop", "inactive"]:
value = 0
elif raw in ["on", "true", "enable", "start", "active"]:
value = 1
else:
value = raw
if str(value) == str(self._get_value_by_acobj(ac_obj)):
log.debug("{} ac_obj {} unchanged value {}->{}, ignored!".format(debug_msg, ac_obj, str(self._get_value_by_acobj(ac_obj)), str(value)))
return True
log.debug("{} ac_obj {} updated value {}=>{}".format(debug_msg, ac_obj, self._get_value_by_acobj(ac_obj), value))
setattr(self._client, ac_obj, value)
return True
except:
return False
def _run(self):
class DaikinCtrl(Daikin):
def __init__(self, host):
super(DaikinCtrl, self).__init__(host)
reqlog = logging.getLogger('urllib3')
reqlog.setLevel(logging.WARNING)
self._cached_ctrl_fields = {}
self._cached_sens_fields = {}
def receive_info(self):
try:
self._cached_ctrl_fields = self._get("/aircon/get_control_info")
log.debug("Daikin AC Control Fields {!r}".format(self._cached_ctrl_fields))
self._cached_sens_fields = self._get("/aircon/get_sensor_info")
log.debug("Daikin AC Sensor Fields {!r}".format(self._cached_sens_fields))
except ConnectionError as e:
log.warning("Daikin AC Couldn't perform API read {!r}".format(e))
def _get_control(self, all_fields=False):
""" Replacement for daikinapi function to operate on cached control info """
return self._cached_ctrl_fields
def _get_sensor(self):
""" Replacement for daikinapi function to operate on cached sensor info """
return self._cached_sens_fields
self._client = DaikinCtrl(self.cfg["host"])
handle_task = self.d.loop.create_task(self.handle_ac())
return [handle_task]