-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update tools to update profile models file
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(file_path: str) -> dict: | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
return yaml.safe_load(file) | ||
|
||
|
||
def save_yaml_file(file_path: str, data: dict) -> None: | ||
with open(file_path, 'w', encoding='utf-8') as file: | ||
yaml.safe_dump( | ||
data=data, stream=file, allow_unicode=True) | ||
|
||
|
||
def load_json_file(file_path: str) -> dict: | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
return json.load(file) | ||
|
||
|
||
def save_json_file(file_path: str, data: dict) -> None: | ||
with open(file_path, '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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" 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') | ||
|
||
|
||
# SpecialModels | ||
|
||
|
||
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( | ||
file_path=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( | ||
file_path=file_path, data=local_rules) | ||
|
||
|
||
update_profile_model(file_path=LAN_PROFILE_MODELS_FILE) | ||
print('profile model list updated.') |