| 1 | """Common base class for tests and Default values. |
| 2 | |
| 3 | Routine Listings |
| 4 | ---------------- |
| 5 | |
| 6 | FunctionResponses : defaultdict |
| 7 | Defaultdict of the standard response tuple for device methods |
| 8 | Defaults: class |
| 9 | Default values and methods for generating default values |
| 10 | """ |
| 11 | from typing import TypeVar, Any |
| 12 | from collections import defaultdict |
| 13 | from requests.structures import CaseInsensitiveDict |
| 14 | import pyvesync.const as const |
| 15 | from pyvesync.utils.helpers import Converters |
| 16 | from pyvesync.device_map import DeviceMapTemplate |
| 17 | from pyvesync.utils.colors import Color, RGB |
| 18 | |
| 19 | T = TypeVar("T", bound=DeviceMapTemplate) |
| 20 | |
| 21 | FunctionResponses: defaultdict = defaultdict(lambda: {"code": 0, "msg": None}) |
| 22 | |
| 23 | FunctionResponsesV1: defaultdict = defaultdict( |
| 24 | lambda: {"traceId": TestDefaults.trace_id, "code": 0, "msg": None, "result": {}} |
| 25 | ) |
| 26 | |
| 27 | FunctionResponsesV2: defaultdict = defaultdict( |
| 28 | lambda: { |
| 29 | "traceId": TestDefaults.trace_id, |
| 30 | "code": 0, |
| 31 | "msg": None, |
| 32 | "result": {"code": 0, "msg": None, "traceId": TestDefaults.trace_id, "result": {}}, |
| 33 | } |
| 34 | ) |
| 35 | |
| 36 | CALL_API_ARGS = ['url', 'method', 'json_object', 'headers'] |
| 37 | |
| 38 | ID_KEYS = ['CID', 'UUID', 'MACID'] |
| 39 | |
| 40 | |
| 41 | def clean_name(name: str) -> str: |
| 42 | """Clean the device name by removing unwanted characters.""" |
| 43 | return name.strip().lower().replace(" ", "_").replace("-", "_").replace(".", "_") |
| 44 | |
| 45 | |
| 46 | def build_base_response( |
| 47 | code: int = 0, msg: str | None = None, merge_dict: dict | None = None |
| 48 | ) -> dict: |
| 49 | """Build the standard response response tuple.""" |
| 50 | resp_dict = { |
| 51 | "code": code, |
| 52 | "msg": msg, |
| 53 | "stacktrace": None, |
| 54 | "module": None, |
| 55 | "traceId": TestDefaults.trace_id, |
| 56 | } |
| 57 | resp_dict |= merge_dict or {} |
| 58 | return resp_dict |
| 59 | |
| 60 | |
| 61 | def build_bypass_v1_response( |
| 62 | *, |
| 63 | code: int = 0, |
| 64 | msg: str | None = None, |
| 65 | result_dict: dict | None = None, |
| 66 | result_override: dict | None = None, |
| 67 | ) -> dict: |
| 68 | """Build the standard response response tuple. |
| 69 | |
| 70 | This function has kw-only arguments. |
| 71 | |
| 72 | Args: |
| 73 | code (int): The response code. |
| 74 | msg (str | None): The response message. |
| 75 | result_dict (dict | None): The result dictionary. |
| 76 | result_override (dict | None): dictionary to override the supplied result_dict. |
| 77 | |
| 78 | Returns: |
| 79 | tuple[dict, int]: The response dictionary and status code. |
| 80 | """ |
| 81 | resp_dict = { |
| 82 | "code": code, |
| 83 | "msg": msg, |
| 84 | "stacktrace": None, |
| 85 | "module": None, |
| 86 | "traceId": TestDefaults.trace_id, |
| 87 | "result": result_dict or {}, |
| 88 | } |
| 89 | return resp_dict |
| 90 | |
| 91 | |
| 92 | def build_bypass_v2_response( |
| 93 | *, |
| 94 | code: int = 0, |
| 95 | msg: str | None = None, |
| 96 | inner_code: int = 0, |
| 97 | inner_result: dict[str, Any] | None = None, |
| 98 | inner_result_override: dict | None = None |
| 99 | ) -> dict: |
| 100 | """Build the standard response response tuple for BypassV2 endpoints. |
| 101 | |
| 102 | This function has kw-only arguments. |
| 103 | |
| 104 | Args: |
| 105 | code(int): The response code. |
| 106 | msg(str | None): The response message. |
| 107 | inner_code(int): The inner response code. |
| 108 | inner_result(dict | None): The inner response result. |
| 109 | inner_result_override(dict | None): dictionary to override the supplied inner_result. |
| 110 | |
| 111 | Returns: |
| 112 | tuple[dict, int]: The response dictionary and status code. |
| 113 | """ |
| 114 | inner_result = inner_result or {} |
| 115 | resp_dict = build_base_response(code, msg) |
| 116 | if inner_result_override is not None: |
| 117 | inner_result = inner_result | inner_result_override |
| 118 | resp_dict['result'] = { |
| 119 | "traceId": TestDefaults.trace_id, |
| 120 | "code": inner_code, |
| 121 | "result": inner_result, |
| 122 | } |
| 123 | return resp_dict |
| 124 | |
| 125 | |
| 126 | def get_device_map_from_setup_entry(module_list: list[T], setup_entry: str) -> T | None: |
| 127 | """Get the device map from the setup entry. |
| 128 | |
| 129 | Args: |
| 130 | module_list (list[T]): The list of device modules. |
| 131 | setup_entry (str): The setup entry string. |
| 132 | |
| 133 | Returns: |
| 134 | dict: The device map dictionary. |
| 135 | """ |
| 136 | for module in module_list: |
| 137 | if clean_name(module.setup_entry) == clean_name(setup_entry): |
| 138 | return module |
| 139 | return None |
| 140 | |
| 141 | |
| 142 | class TestDefaults: |
| 143 | """General defaults for API responses and requests. |
| 144 | |
| 145 | Attributes |
| 146 | ---------- |
| 147 | token : str |
| 148 | Default token for API requests |
| 149 | active_time : str |
| 150 | Default active time for API responses |
| 151 | account_id : str |
| 152 | Default account ID for API responses |
| 153 | active_time : str |
| 154 | Default active time for API responses |
| 155 | color: Color (dataclass) |
| 156 | Red=50, Green=100, Blue=225, Hue=223, Saturation=77.78, value=88.24 |
| 157 | Default Color dataclass |
| 158 | contains red, green, blue, hue, saturation and value attributes |
| 159 | |
| 160 | Methods |
| 161 | -------- |
| 162 | name(dev_type='NA') |
| 163 | Default device name created from "dev_type-NAME" |
| 164 | cid(dev_type='NA') |
| 165 | Default device cid created from "dev_type-CID" |
| 166 | uuid(dev_type='NA') |
| 167 | Default device uuid created from "dev_type-UUID" |
| 168 | macid(dev_type='NA') |
| 169 | Default device macid created from "dev_type-MACID" |
| 170 | """ |
| 171 | |
| 172 | token = 'sample_tk' |
| 173 | account_id = 'sample_id' |
| 174 | trace_id = "TRACE_ID" |
| 175 | terminal_id = "TERMINAL_ID" |
| 176 | app_id = "APP_ID" |
| 177 | email = 'EMAIL' |
| 178 | password = 'PASSWORD' |
| 179 | authorization_code = "AUTHORIZATION_CODE" |
| 180 | biz_token = "BIZ_TOKEN" |
| 181 | region = 'US' |
| 182 | country_code = 'US' |
| 183 | active_time = 1 |
| 184 | time_zone = const.DEFAULT_TZ |
| 185 | color: Color = Color(RGB(50, 100, 225)) |
| 186 | brightness = 100 |
| 187 | color_temp = 100 |
| 188 | color_temp_k = Converters.color_temp_pct_to_kelvin(100) |
| 189 | bool_toggle = True |
| 190 | str_toggle = 'on' |
| 191 | bin_toggle = 1 |
| 192 | level_int = 1 |
| 193 | country_code = 'US' |
| 194 | |
| 195 | @staticmethod |
| 196 | def name(setup_entry: str = 'NA'): |
| 197 | """Name of device with format f"{setup_entry}-NAME". |
| 198 | |
| 199 | Parameters |
| 200 | ---------- |
| 201 | setup_entry : str |
| 202 | Device type use to create default name |
| 203 | |
| 204 | Returns |
| 205 | ------- |
| 206 | str |
| 207 | Default name for device f"{setup_entry}-NAME" |
| 208 | """ |
| 209 | return f'{setup_entry}-NAME' |
| 210 | |
| 211 | @staticmethod |
| 212 | def cid(setup_entry='NA'): |
| 213 | """CID for a device with format f"{setup_entry}-CID". |
| 214 | |
| 215 | Parameters |
| 216 | ---------- |
| 217 | setup_entry : str |
| 218 | Device type use to create default cid |
| 219 | |
| 220 | Returns |
| 221 | ------- |
| 222 | str |
| 223 | Default cid for device f"setup_entry-CID" |
| 224 | """ |
| 225 | return f'{setup_entry}-CID' |
| 226 | |
| 227 | @staticmethod |
| 228 | def uuid(setup_entry='NA'): |
| 229 | """UUID for a device with format f"{setup_entry}-UUID". |
| 230 | |
| 231 | Parameters |
| 232 | ---------- |
| 233 | setup_entry : str |
| 234 | Device type use to create default UUID |
| 235 | |
| 236 | Returns |
| 237 | ------- |
| 238 | str |
| 239 | Default uuid for device f"{setup_entry}-UUID" |
| 240 | """ |
| 241 | return f'{setup_entry}-UUID' |
| 242 | |
| 243 | @staticmethod |
| 244 | def macid(setup_entry='NA'): |
| 245 | """MACID for a device with format f"{setup_entry}-MACID". |
| 246 | |
| 247 | Parameters |
| 248 | ---------- |
| 249 | setup_entry : str |
| 250 | Device type use to create default macid |
| 251 | |
| 252 | Returns |
| 253 | ------- |
| 254 | str |
| 255 | Default macID for device f"{setup_entry}-MACID" |
| 256 | """ |
| 257 | return f'{setup_entry}-MACID' |
| 258 | |
| 259 | @staticmethod |
| 260 | def config_module(setup_entry='NA'): |
| 261 | """Config module for a device with format f"{setup_entry}-CONFIG". |
| 262 | |
| 263 | Parameters |
| 264 | ---------- |
| 265 | setup_entry : str |
| 266 | Device type use to create default config module |
| 267 | |
| 268 | Returns |
| 269 | ------- |
| 270 | str |
| 271 | Default config module for device f"{setup_entry}-CONFIG_MODULE" |
| 272 | """ |
| 273 | return f'{setup_entry}-CONFIG_MODULE' |
| 274 | |
| 275 | |
| 276 | API_DEFAULTS = CaseInsensitiveDict({ |
| 277 | 'accountID': TestDefaults.account_id, |
| 278 | 'token': TestDefaults.token, |
| 279 | 'timeZone': const.DEFAULT_TZ, |
| 280 | 'acceptLanguage': 'en', |
| 281 | 'appVersion': const.APP_VERSION, |
| 282 | 'phoneBrand': const.PHONE_BRAND, |
| 283 | 'phoneOS': const.PHONE_OS, |
| 284 | 'userType': const.USER_TYPE, |
| 285 | "tk": TestDefaults.token, |
| 286 | "mobileId": "MOBILE_ID", |
| 287 | "traceId": "TRACE_ID", |
| 288 | 'verifyEmail': 'EMAIL', |
| 289 | 'nickName': 'NICKNAME', |
| 290 | 'password': 'PASSWORD', |
| 291 | 'username': 'EMAIL', |
| 292 | 'email': 'EMAIL', |
| 293 | 'deviceName': 'NAME' |
| 294 | |
| 295 | }) |