Skip to content

Commit

Permalink
Fix local ctrl error (XiaoMi#271)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
topsworld authored Dec 20, 2024
1 parent 6ce3206 commit bd3a98b
Show file tree
Hide file tree
Showing 7 changed files with 1,534 additions and 10 deletions.
21 changes: 18 additions & 3 deletions custom_components/xiaomi_home/miot/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@
Common utilities.
"""
import json
from os import path
import random
from typing import Optional
import hashlib
from paho.mqtt.client import MQTTMatcher
import yaml

MIOT_ROOT_PATH: str = path.dirname(path.abspath(__file__))


def gen_absolute_path(relative_path: str) -> str:
"""Generate an absolute path."""
return path.join(MIOT_ROOT_PATH, relative_path)


def calc_group_id(uid: str, home_id: str) -> str:
Expand All @@ -64,6 +73,12 @@ def load_json_file(json_file: str) -> dict:
return json.load(f)


def load_yaml_file(yaml_file: str) -> dict:
"""Load a YAML file."""
with open(yaml_file, 'r', encoding='utf-8') as f:
return yaml.load(f, Loader=yaml.FullLoader)


def randomize_int(value: int, ratio: float) -> int:
"""Randomize an integer value."""
return int(value * (1 - ratio + random.random()*2*ratio))
Expand All @@ -74,12 +89,12 @@ class MIoTMatcher(MQTTMatcher):

def iter_all_nodes(self) -> any:
"""Return an iterator on all nodes with their paths and contents."""
def rec(node, path):
def rec(node, path_):
# pylint: disable=protected-access
if node._content:
yield ('/'.join(path), node._content)
yield ('/'.join(path_), node._content)
for part, child in node._children.items():
yield from rec(child, path + [part])
yield from rec(child, path_ + [part])
return rec(self._root, [])

def get(self, topic: str) -> Optional[any]:
Expand Down
Loading

0 comments on commit bd3a98b

Please sign in to comment.