forked from 3v1n0/netatmo-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetatmo_modules.py
229 lines (177 loc) · 5.33 KB
/
netatmo_modules.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Marco Trevisan
#
# CoPyCloud: a simple python wrapper to manage the Copy.com cloud
#
# Authors:
# Marco Trevisan (Treviño) <[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; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUTa
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import time
class BaseObject(object):
def __init__(self, data):
self.__data = data
def __getitem__(self, value):
return self.__data[value]
def __getattr__(self, name):
return self[name]
class User(BaseObject):
class Units:
SI = 0
IMPERIAL = 1
class WindUnit:
KPH = 0
MPH = 1
MS = 2
BEAUFORT = 3
KNOT = 4
class PressureUnit:
MBAR = 0
INHG = 1
MMHG = 2
@property
def id(self):
return self._id
@property
def units(self):
return self.administrative['unit']
@property
def wind_unit(self):
return self.administrative['windunit']
@property
def pressure_unit(self):
return self.administrative['pressureunit']
class Module(BaseObject):
class Type:
MAIN = "NAMain"
OUTDOOR = "NAModule1"
WIND = "NAModule2"
RAIN = "NAModule3"
INDOOR = "NAModule4"
@staticmethod
def factory(data):
if data['type'] == Module.Type.MAIN:
return ControlUnit(data)
if data['type'] == Module.Type.OUTDOOR:
return Outdoor(data)
if data['type'] == Module.Type.WIND:
return Wind(data)
if data['type'] == Module.Type.RAIN:
return Outdoor(data)
if data['type'] == Module.Type.INDOOR:
return Indoor(data)
raise Exception("No valid type for data found")
@property
def id(self):
return self._id
@property
def type(self):
return self['type']
@property
def name(self):
return self.module_name
@property
def dashboard(self):
return self.dashboard_data
@property
def updated_time(self):
return self.dashboard['time_utc']
@property
def age(self):
return time.time() - self.updated_time
@property
def sensors(self):
return self.data_type
def has_battery(self):
return False
def get_sensors_data(self):
return { s: self.dashboard[s] if s in self.dashboard.keys() else None for s in self.sensors }
class WirelessModule(Module):
def __signal_levels(self):
return [self.Signal.LEVEL_0, self.Signal.LEVEL_1, self.Signal.LEVEL_2, self.Signal.LEVEL_3]
def __signal_nearest_level(self):
return min(self.__signal_levels(), key=lambda l:abs(l-self.signal_strength))
@property
def signal_level(self):
return self.__signal_levels().index(self.__signal_nearest_level())
@property
def signal_percent(self):
return clamp(0, (self.Signal.MIN - self.signal_strength) * 100.0 / (self.Signal.MIN - self.Signal.MAX), 100)
class WifiModule(WirelessModule):
class Signal:
MIN = 100
MAX = 60
LEVEL_0 = 90
LEVEL_1 = 80
LEVEL_2 = 70
LEVEL_3 = 60
@property
def signal_strength(self):
return self.wifi_status
class RadioModule(WirelessModule):
class Signal:
MIN = 90
MAX = 20
LEVEL_0 = 86
LEVEL_1 = 71
LEVEL_2 = 56
LEVEL_3 = 20
def has_battery(self):
return True
def __battery_levels(self):
return [self.Battery.LEVEL_3, self.Battery.LEVEL_2, self.Battery.LEVEL_1, self.Battery.LEVEL_0]
def __battery_nearest_level(self):
return min(self.__battery_levels(), key=lambda l:abs(l-self.battery_power))
@property
def signal_strength(self):
return self.rf_status
@property
def battery_power(self):
return self.battery_vp
@property
def battery_level(self):
return self.__battery_levels().index(self.__battery_nearest_level())
@property
def battery_percent(self):
return clamp(0, (self.battery_power - self.Battery.MIN) * 100.0 / (self.Battery.MAX - self.Battery.MIN), 100)
class ControlUnit(WifiModule):
@property
def station_name(self):
return self['station_name']
class Indoor(RadioModule):
class Battery:
MIN = 4200
MAX = 6000
LEVEL_0 = 5640
LEVEL_1 = 5280
LEVEL_2 = 4920
LEVEL_3 = 4560
class Outdoor(RadioModule):
class Battery:
MIN = 3600
MAX = 6000
LEVEL_0 = 5500
LEVEL_1 = 5000
LEVEL_2 = 4500
LEVEL_3 = 4000
class Wind(RadioModule):
class Battery:
MIN = 3950
MAX = 6000
LEVEL_0 = 5590
LEVEL_1 = 5180
LEVEL_2 = 4770
LEVEL_3 = 4360
def clamp(minvalue, value, maxvalue):
return max(minvalue, min(value, maxvalue))