Fix local ctrl error (#271)

* 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
This commit is contained in:
Paul Shawn
2024-12-20 19:21:43 +08:00
committed by GitHub
parent 6ce3206b30
commit bd3a98b976
7 changed files with 1534 additions and 10 deletions

44
tools/common.py Normal file
View File

@ -0,0 +1,44 @@
# -*- 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)

80
tools/update_lan_rule.py Normal file
View File

@ -0,0 +1,80 @@
""" Update LAN rule."""
# -*- coding: utf-8 -*-
# pylint: disable=relative-beyond-top-level
from os import path
from common import (
http_get,
load_yaml_file,
save_yaml_file)
ROOT_PATH: str = path.dirname(path.abspath(__file__))
LAN_PROFILE_MODELS_FILE: str = path.join(
ROOT_PATH,
'../custom_components/xiaomi_home/miot/lan/profile_models.yaml')
SPECIAL_MODELS: list[str] = [
# model2class-v2
'chuangmi.camera.ipc007b', 'chuangmi.camera.ipc019b',
'chuangmi.camera.ipc019e', 'chuangmi.camera.ipc020',
'chuangmi.camera.v2', 'chuangmi.camera.v5',
'chuangmi.camera.v6', 'chuangmi.camera.xiaobai',
'chuangmi.radio.v1', 'chuangmi.radio.v2',
'hith.foot_bath.q2', 'imou99.camera.tp2',
'isa.camera.hl5', 'isa.camera.isc5',
'jiqid.mistory.pro', 'jiqid.mistory.v1',
'lumi.airrtc.tcpco2ecn01', 'lumi.airrtc.tcpecn02',
'lumi.camera.gwagl01', 'miir.light.ir01',
'miir.projector.ir01', 'miir.tv.hir01',
'miir.tvbox.ir01', 'roome.bhf_light.yf6002',
'smith.waterpuri.jnt600', 'viomi.fridge.u2',
'xiaovv.camera.lamp', 'xiaovv.camera.ptz',
'xiaovv.camera.xva3', 'xiaovv.camera.xvb4',
'xiaovv.camera.xvsnowman', 'zdeer.ajh.a8',
'zdeer.ajh.a9', 'zdeer.ajh.zda10',
'zdeer.ajh.zda9', 'zdeer.ajh.zjy', 'zimi.clock.myk01',
# specialModels
'chuangmi.camera.ipc004b', 'chuangmi.camera.ipc009',
'chuangmi.camera.ipc010', 'chuangmi.camera.ipc013',
'chuangmi.camera.ipc013d', 'chuangmi.camera.ipc016',
'chuangmi.camera.ipc017', 'chuangmi.camera.ipc019',
'chuangmi.camera.ipc021', 'chuangmi.camera.v3',
'chuangmi.camera.v4', 'isa.camera.df3',
'isa.camera.hlc6', 'lumi.acpartner.v1',
'lumi.acpartner.v2', 'lumi.acpartner.v3',
'lumi.airrtc.tcpecn01', 'lumi.camera.aq1',
'miir.aircondition.ir01', 'miir.aircondition.ir02',
'miir.fan.ir01', 'miir.stb.ir01',
'miir.tv.ir01', 'mijia.camera.v1',
'mijia.camera.v3', 'roborock.sweeper.s5v2',
'roborock.vacuum.c1', 'roborock.vacuum.e2',
'roborock.vacuum.m1s', 'roborock.vacuum.s5',
'rockrobo.vacuum.v1', 'xiaovv.camera.xvd5']
def update_profile_model(file_path: str):
profile_rules: dict = http_get(
url='https://miot-spec.org/instance/translate/models')
if not profile_rules and 'models' not in profile_rules and not isinstance(
profile_rules['models'], dict):
raise ValueError('Failed to get profile rule')
local_rules: dict = load_yaml_file(
yaml_file=file_path) or {}
for rule, ts in profile_rules['models'].items():
if rule not in local_rules:
local_rules[rule] = {'ts': ts}
else:
local_rules[rule]['ts'] = ts
for mode in SPECIAL_MODELS:
if mode not in local_rules:
local_rules[mode] = {'ts': 1531108800}
else:
local_rules[mode]['ts'] = 1531108800
local_rules = dict(sorted(local_rules.items()))
save_yaml_file(
yaml_file=file_path, data=local_rules)
update_profile_model(file_path=LAN_PROFILE_MODELS_FILE)
print('profile model list updated.')