Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Sep 24, 2024
1 parent b6db51f commit c29273b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def name(self):

@property
def name_model(self):
return f'{self.device_name}({self.model})'
return self.device.name_model

@property
def device_name(self):
Expand Down
49 changes: 47 additions & 2 deletions custom_components/xiaomi_miot/core/device.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import copy
import json
import voluptuous as vol
from typing import TYPE_CHECKING, Optional, Callable
from functools import partial, cached_property
from homeassistant.core import HomeAssistant
from homeassistant.const import CONF_HOST, CONF_TOKEN, CONF_MODEL
from homeassistant.helpers.device_registry import format_mac
import homeassistant.helpers.config_validation as cv

from .const import DOMAIN, DEVICE_CUSTOMIZES, DEFAULT_NAME, CONF_CONN_MODE, DEFAULT_CONN_MODE
from .hass_entry import HassEntry
Expand Down Expand Up @@ -143,6 +146,10 @@ def model(self):
def name_model(self):
return f'{self.name}({self.model})'

@cached_property
def unique_id(self):
return f'{self.info.unique_id}-{self.entry.entry_id}'

@property
def conn_mode(self):
return self.entry.get_config(CONF_CONN_MODE) or DEFAULT_CONN_MODE
Expand All @@ -164,7 +171,7 @@ def hass_device_info(self):
if updater and updater not in ['none']:
swv = f'{swv} ({updater})'
return {
'identifiers': {(DOMAIN, self.info.unique_id)},
'identifiers': {(DOMAIN, self.unique_id)},
'name': self.info.name,
'model': self.model,
'manufacturer': (self.model or 'Xiaomi').split('.', 1)[0],
Expand All @@ -181,6 +188,14 @@ def custom_config(self, key=None, default=None):
cfg = self.customizes
return cfg if key is None else cfg.get(key, default)

def custom_config_bool(self, key=None, default=None):
val = self.custom_config(key, default)
try:
val = cv.boolean(val)
except vol.Invalid:
val = default
return val

def custom_config_list(self, key=None, default=None):
lst = self.custom_config(key)
if lst is None:
Expand All @@ -190,6 +205,18 @@ def custom_config_list(self, key=None, default=None):
lst = list(map(lambda x: x.strip(), lst))
return lst

def custom_config_json(self, key=None, default=None):
dic = self.custom_config(key)
if dic:
if not isinstance(dic, (dict, list)):
try:
dic = json.loads(dic or '{}')
except (TypeError, ValueError):
dic = None
if isinstance(dic, (dict, list)):
return dic
return default

@cached_property
def extend_miot_specs(self):
if self.cloud_only:
Expand Down Expand Up @@ -355,6 +382,21 @@ async def async_write(self, payload: dict):
self.dispatch(payload)
return result

def miot_mapping(self):
if dic := self.custom_config_json('miot_mapping'):
self.spec.set_custom_mapping(dic)
if ems := self.custom_config_list('exclude_miot_services') or []:
self.data['exclude_miot_services'] = ems
if eps := self.custom_config_list('exclude_miot_properties') or []:
self.data['exclude_miot_properties'] = eps
urp = self.custom_config_bool('unreadable_properties')
mapping = self.spec.services_mapping(
excludes=ems,
exclude_properties=eps,
unreadable_properties=urp,
) or {}
return mapping

async def update_miot_status(
self,
mapping=None,
Expand All @@ -376,12 +418,15 @@ async def update_miot_status(
use_cloud = not use_local and self.cloud
if not self.local:
use_cloud = self.cloud

if mapping is None:
mapping = self.miot_mapping()
if not (mapping or local_mapping):
use_local = False
use_cloud = False

if use_local:
self.miot_results.updater = 'lan'
self.miot_results.updater = 'local'
if not local_mapping:
local_mapping = mapping
try:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/core/hass_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def new_device(self, device_info: dict, cloud: Optional[MiotCloud] = None)
spec = await device.get_spec()
if spec and not device.cloud_only:
device.miio2miot = Miio2MiotHelper.from_model(self.hass, device.model, spec)
await device.update_miot_status()
await device.update_miot_status() # TODO
self.devices[info.unique_id] = device
return device

Expand Down

0 comments on commit c29273b

Please sign in to comment.