mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2025-03-31 14:55:31 +08:00
* feat: common.py add gen_absolute_path, load_yaml_file * fix: miot_lan add profile devices filter * fix: add lan ctrl profile model list * test: improve lan test * fix: fix pylint redefined-outer-name * feat: update tools to update profile models file * fix: fix pylint waning * fix: update miot lan NETWORK_UNSTABLE_RESUME_TH value
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Common functions."""
|
|
import json
|
|
import yaml
|
|
from urllib.parse import urlencode
|
|
from urllib.request import Request, urlopen
|
|
|
|
|
|
def load_yaml_file(yaml_file: str) -> dict:
|
|
with open(yaml_file, 'r', encoding='utf-8') as file:
|
|
return yaml.safe_load(file)
|
|
|
|
|
|
def save_yaml_file(yaml_file: str, data: dict) -> None:
|
|
with open(yaml_file, 'w', encoding='utf-8') as file:
|
|
yaml.safe_dump(
|
|
data=data, stream=file, allow_unicode=True)
|
|
|
|
|
|
def load_json_file(json_file: str) -> dict:
|
|
with open(json_file, 'r', encoding='utf-8') as file:
|
|
return json.load(file)
|
|
|
|
|
|
def save_json_file(json_file: str, data: dict) -> None:
|
|
with open(json_file, 'w', encoding='utf-8') as file:
|
|
json.dump(data, file, ensure_ascii=False, indent=4)
|
|
|
|
|
|
def http_get(
|
|
url: str, params: dict = None, headers: dict = None
|
|
) -> dict:
|
|
if params:
|
|
encoded_params = urlencode(params)
|
|
full_url = f'{url}?{encoded_params}'
|
|
else:
|
|
full_url = url
|
|
request = Request(full_url, method='GET', headers=headers or {})
|
|
content: bytes = None
|
|
with urlopen(request) as response:
|
|
content = response.read()
|
|
return (
|
|
json.loads(str(content, 'utf-8'))
|
|
if content is not None else None)
|