Debian Codegraph @

src/tests/call_json_outlets.py

raw symbols SCIP
1 """
2 Outlet Device API Responses
3
4 OUTLET variable is a list of device types
5
6 DETAILS_RESPONSES variable is a dictionary of responses from the API
7 for get_details() methods. The keys are the device types and the
8 values are the responses. The responses are tuples of (response, status)
9
10 METHOD_RESPONSES variable is a defaultdict of responses from the API. This is
11 the FunctionResponse variable from the utils module in the tests dir.
12 The default response is a tuple with the value ({"code": 0, "msg": "success"}, 200).
13
14 The values of METHOD_RESPONSES can be a function that takes a single argument or
15 a static value. The value is checked if callable at runtime and if so, it is called
16 with the provided argument. If not callable, the value is returned as is.
17
18 METHOD_RESPONSES = {
19 'DEV_TYPE': defaultdict(
20 lambda: ({"code": 0, "msg": "success"}, 200))
21 )
22 }
23
24 ### For a function to handle the response
25 def status_response(request_kwargs=None):
26 # do work with request_kwargs
27 return request_body, 200
28
29 METHOD_RESPONSES['DEV_TYPE']['set_status'] = status_response
30
31 ### To change the default value for a device type
32
33 METHOD_RESPONSES['DEVTYPE'].default_factory = lambda: ({"code": 0, "msg": "success"}, 200)
34
35 If changing default response for all devices, change the default factory of the import
36 default dict but make sure to use `deepcopy` to avoid unintended side effects.
37 """
38
39 from copy import deepcopy
40 from collections import defaultdict
41 from pyvesync.const import (
42 DeviceStatus,
43 ConnectionStatus,
44 NightlightModes,
45 NightlightStatus,
46 )
47 from pyvesync.device_map import outlet_modules
48 from defaults import (
49 FunctionResponses,
50 TestDefaults,
51 build_bypass_v1_response,
52 build_bypass_v2_response,
53 FunctionResponsesV1,
54 FunctionResponsesV2,
55 )
56
57
58 OUTLETS = [m.setup_entry for m in outlet_modules]
59 OUTLETS_NUM = len(OUTLETS)
60
61
62 class OutletDefaults:
63 device_status = DeviceStatus.ON
64 connection_status = ConnectionStatus.ONLINE
65 night_light_status = NightlightStatus.ON
66 night_light_mode = NightlightModes.AUTO
67 voltage = 120 # volts
68 energy = 10 # kilowatt
69 power = 20 # kilowatt-hours
70 current = 0.5 # amps
71 round_7a_voltage = "78000:78000" # 120 Volts
72 round_7a_power = "1000:1000" # 1 watt
73
74
75 OUTLET_DETAILS: dict[str, dict] = {
76 "wifi-switch-1.3": {
77 "deviceStatus": OutletDefaults.device_status.value,
78 "deviceImg": "https://image.vesync.com/defaultImages/deviceDefaultImages/7aoutlet_240.png",
79 "energy": OutletDefaults.energy,
80 "activeTime": TestDefaults.active_time,
81 "power": OutletDefaults.round_7a_power,
82 "voltage": OutletDefaults.round_7a_voltage,
83 },
84 "ESW03": {
85 "activeTime": TestDefaults.active_time,
86 "deviceName": "ESW03",
87 "deviceStatus": OutletDefaults.device_status.value,
88 "power": str(OutletDefaults.power),
89 "voltage": str(OutletDefaults.voltage),
90 "energy": OutletDefaults.energy,
91 "connectionStatus": OutletDefaults.connection_status.value,
92 },
93 "ESW10-USA": {
94 "id": 0,
95 "enabled": bool(OutletDefaults.device_status),
96 },
97 "ESW15-USA": { # V1
98 "activeTime": TestDefaults.active_time,
99 "deviceName": "Etekcity 15A WiFi Outlet US/CA",
100 "deviceStatus": OutletDefaults.device_status.value,
101 "power": OutletDefaults.power,
102 "voltage": OutletDefaults.voltage,
103 "energy": OutletDefaults.energy,
104 "nightLightStatus": OutletDefaults.night_light_status.value,
105 "nightLightAutomode": OutletDefaults.night_light_mode.value,
106 "nightLightBrightness": 50,
107 "deviceImg": "https://image.vesync.com/defaultImages/deviceDefaultImages/15aoutletnightlight_240.png",
108 "connectionStatus": OutletDefaults.connection_status.value,
109 },
110 "ESO15-TB": {
111 "activeTime": TestDefaults.active_time,
112 "deviceName": "Etekcity Outdoor Plug",
113 "deviceStatus": OutletDefaults.device_status.value,
114 "power": OutletDefaults.power,
115 "voltage": OutletDefaults.voltage,
116 "energy": OutletDefaults.energy,
117 "subDevices": [
118 {
119 "subDeviceNo": 1,
120 "defaultName": "Socket A",
121 "subDeviceName": "Socket A",
122 "subDeviceImg": "",
123 "subDeviceStatus": OutletDefaults.device_status.value,
124 },
125 {
126 "subDeviceNo": 2,
127 "defaultName": "Socket B",
128 "subDeviceName": "Socket B",
129 "subDeviceImg": "",
130 "subDeviceStatus": OutletDefaults.device_status.value,
131 },
132 ],
133 "deviceImg": "",
134 "connectionStatus": OutletDefaults.connection_status.value,
135 },
136 "BSDOG01": {
137 "channelNum": 1,
138 "powerSwitch_1": 1,
139 "realTimeVoltage": OutletDefaults.voltage,
140 "realTimePower": OutletDefaults.power,
141 "electricalEnergy": OutletDefaults.energy,
142 "voltageUpperThreshold": 280,
143 "voltageUnderThreshold": 75,
144 "protectionStatus": "normal",
145 "currentUpperThreshold": 16,
146 },
147 "WHOGPLUG": {
148 "enabled": True,
149 "voltage": OutletDefaults.voltage,
150 "energy": OutletDefaults.energy,
151 "power": OutletDefaults.power,
152 "current": OutletDefaults.current,
153 "highestVoltage": 240,
154 "voltagePtStatus": False,
155 },
156 }
157
158
159 DETAILS_RESPONSES = {
160 "wifi-switch-1.3": OUTLET_DETAILS["wifi-switch-1.3"],
161 "ESW03": build_bypass_v1_response(result_dict=OUTLET_DETAILS["ESW03"]),
162 "ESW10-USA": build_bypass_v2_response(inner_result=OUTLET_DETAILS["ESW10-USA"]),
163 "ESW15-USA": build_bypass_v1_response(result_dict=OUTLET_DETAILS["ESW15-USA"]),
164 "ESO15-TB": build_bypass_v1_response(result_dict=OUTLET_DETAILS["ESO15-TB"]),
165 "BSDOG01": build_bypass_v2_response(inner_result=OUTLET_DETAILS["BSDOG01"]),
166 "WHOGPLUG": build_bypass_v2_response(inner_result=OUTLET_DETAILS["WHOGPLUG"]),
167 }
168
169
170 ENERGY_HISTORY = {
171 "traceId": TestDefaults.trace_id,
172 "code": 0,
173 "msg": "request success",
174 "module": None,
175 "stacktrace": None,
176 "result": {
177 "energyConsumptionOfToday": 1,
178 "costPerKWH": 0.50,
179 "maxEnergy": 0.7,
180 "totalEnergy": 2.0,
181 "totalMoney": 1.0,
182 "averageEnergy": 0.01,
183 "averageMoney": 0.0,
184 "maxCost": 35.0,
185 "energySavingStatus": "off",
186 "currency": "USD",
187 "energyInfos": [
188 {"timestamp": 1742184000025, "energyKWH": 0.0015, "money": 0.0},
189 {"timestamp": 1742270400025, "energyKWH": 0.0016, "money": 0.0},
190 {"timestamp": 1742356800025, "energyKWH": 0.0017, "money": 0.0},
191 ],
192 },
193 }
194
195 WHOPLUG_ENERGY_HISTORY_COMMON = {
196 "energyInfos": [
197 {"timestamp": 1697673600, "energy": 0.06002924054861},
198 {"timestamp": 1697587200, "energy": 0.03776630222797},
199 {"timestamp": 1697500800, "energy": 0.00002495583819},
200 {"timestamp": 1697414400, "energy": 0.00038652237961},
201 {"timestamp": 1697328000, "energy": 0},
202 {"timestamp": 1697241600, "energy": 0},
203 {"timestamp": 1697155200, "energy": 0},
204 ],
205 "total": 7,
206 }
207
208 WHOPLUG_YEAR_ENERGY_HISTORY = {
209 "ELECConsumeList": [
210 {"month": "2022-11", "ELECConsume": 0},
211 {"month": "2022-12", "ELECConsume": 0},
212 {"month": "2023-01", "ELECConsume": 0},
213 {"month": "2023-02", "ELECConsume": 0},
214 {"month": "2023-03", "ELECConsume": 0},
215 {"month": "2023-04", "ELECConsume": 0},
216 {"month": "2023-05", "ELECConsume": 0},
217 {"month": "2023-06", "ELECConsume": 0},
218 {"month": "2023-07", "ELECConsume": 0},
219 {"month": "2023-08", "ELECConsume": 0},
220 {"month": "2023-09", "ELECConsume": 0},
221 {"month": "2023-10", "ELECConsume": 0.0281},
222 ]
223 }
224
225 METHOD_RESPONSES = {
226 "wifi-switch-1.3": defaultdict(lambda: None),
227 "ESW10-USA": deepcopy(FunctionResponsesV2),
228 "ESW03": deepcopy(FunctionResponses),
229 "ESW15-USA": deepcopy(FunctionResponsesV1),
230 "ESO15-TB": deepcopy(FunctionResponsesV1),
231 "BSDOG01": deepcopy(FunctionResponsesV2),
232 "WHOGPLUG": deepcopy(FunctionResponsesV2),
233 }
234
235 for k in METHOD_RESPONSES:
236 if k in ["ESW10-USA"]:
237 METHOD_RESPONSES[k]["get_weekly_energy"] = None
238 METHOD_RESPONSES[k]["get_monthly_energy"] = None
239 METHOD_RESPONSES[k]["get_yearly_energy"] = None
240 elif k in ["BSDOG01", "WHOGPLUG"]:
241 METHOD_RESPONSES[k]["get_weekly_energy"] = build_bypass_v2_response(
242 inner_result=WHOPLUG_ENERGY_HISTORY_COMMON
243 )
244 METHOD_RESPONSES[k]["get_monthly_energy"] = build_bypass_v2_response(
245 inner_result=WHOPLUG_ENERGY_HISTORY_COMMON
246 )
247 METHOD_RESPONSES[k]["get_yearly_energy"] = build_bypass_v1_response(
248 result_dict=WHOPLUG_YEAR_ENERGY_HISTORY
249 )
250 else:
251 METHOD_RESPONSES[k]["get_weekly_energy"] = ENERGY_HISTORY
252 METHOD_RESPONSES[k]["get_monthly_energy"] = ENERGY_HISTORY
253 METHOD_RESPONSES[k]["get_yearly_energy"] = ENERGY_HISTORY