def get_event_list(self, *, device_ids: Sequence[str] = [], event_values: Union[EventAlarmType, Sequence[EventAlarmType]] = [], event_tags: Sequence[str] = [], event_type: str = "1", begin: Optional[datetime] = None, end: Optional[datetime] = None, limit: int = 20, order_by: int = 2, **kwargs) -> WyzeResponse: SV_GET_EVENT_LIST = 'bdcb412e230049c0be0916e75022d3f3' if limit < 1 or limit > 20: raise WyzeRequestError(f"limit {limit} must be between 1 and 20") if order_by not in [1, 2]: raise WyzeRequestError(f"order_by {order_by} must be one of {[1, 2]}") if begin is None: begin = datetime.now() - timedelta(days=1) if end is None: end = datetime.now() if isinstance(event_values, (list, Tuple)): kwargs.update({ "event_value_list": [code for alarm_type in event_values for code in alarm_type.codes] }) else: kwargs.update({"event_value_list": [code for code in event_values.codes]}) kwargs.update({ "device_mac_list": device_ids, 'begin_time': datetime_to_epoch(begin), "event_tag_list": event_tags, "event_type": event_type, 'end_time': datetime_to_epoch(end), 'order_by': order_by, 'count': limit, "sv": SV_GET_EVENT_LIST, }) return self.api_call('/app/v2/device/get_event_list', json=kwargs)
def api_call( self, api_endpoint: str, *, http_verb: str = "POST", data: Union[dict] = None, params: dict = None, json: dict = None, headers: dict = None, auth: dict = None, ) -> WyzeResponse: """Create a request and execute the API call to Wyze. Args: api_endpoint (str): The target Wyze API endpoint. e.g. '/app/v2/home_page/get_object_list' http_verb (str): HTTP Verb. e.g. 'POST' data: The body to attach to the request. If a dictionary is provided, form-encoding will take place. e.g. {'key1': 'value1', 'key2': 'value2'} params (dict): The URL parameters to append to the URL. e.g. {'key1': 'value1', 'key2': 'value2'} json (dict): JSON for the body to attach to the request (if data is not specified). e.g. {'key1': 'value1', 'key2': 'value2'} headers (dict): Additional request headers auth (dict): A dictionary that consists of access_token and refresh_token Returns: (WyzeResponse) The server's response to an HTTP request. Data from the response can be accessed like a dict. Raises: WyzeApiError: The following Wyze API call failed: '/app/v2/home_page/get_object_list'. WyzeRequestError: JSON data can only be submitted as POST requests. """ has_json = json is not None if has_json and http_verb != "POST": msg = "JSON data can only be submitted as POST requests. GET requests should use the 'params' argument." raise WyzeRequestError(msg) api_url = self._get_url(self.base_url, api_endpoint) headers = headers or {} headers.update(self.headers) if http_verb == "POST": return self.do_post(url=api_url, headers=headers, payload=json, params=params) elif http_verb == "GET": return self.do_get(url=api_url, headers=headers, payload=params) msg = "Unknown request type." raise WyzeRequestError(msg)
def set_unit( self, *, device_mac: str, device_model: str, firmware_ver: str, mac: str, unit: str, broadcast: int, **kwargs, ) -> WyzeResponse: """Sets the weight/mass unit for the scale. Args: :param str device_mac: The device mac. e.g. ``JA.SC2.ABCDEF1234567890`` :param str mac: The device mac, without the leading product model identifier. e.g. ``ABCDEF1234567890`` :param str device_model: The device model. e.g. ``JA.SC2`` :param str firmware_ver: The firmware version. e.g. '' :param str unit: The new unit. e.g. ``kg`` :param int broadcast: The broadcast. e.g. ``1`` :raises WyzeRequestError: if the new unit is not ``kg`` or ``lb`` :rtype: WyzeResponse """ if unit not in ['kg', 'lb']: raise WyzeRequestError(f"{unit} must be one of {['kg', 'lb']}") response = self._set_scale_setting(device_mac, device_model, firmware_ver, mac, unit, broadcast) return response
def validate(self, value: Any): if not isinstance(value, self._type): try: value = bool(distutils.util.strtobool( str(value))) if self._type == bool else self._type(value) except TypeError: logging.debug( f"could not cast value {value} into expected type {self._type}" ) raise WyzeRequestError(f"{value} must be of type {self._type}") if self._acceptable_values is None: logging.debug("acceptable_values is not set, passing") return if value in self._acceptable_values: logging.debug(f"value {value} found in acceptable_values, passing") return raise WyzeRequestError( f"{value} must be one of {self.acceptable_values}")