Skip to content

Commit

Permalink
🛜 add support for proxy device (#2043)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Dec 28, 2024
1 parent 14a8f14 commit 7e67534
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions custom_components/xiaomi_miot/core/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def pid(self):
def urn(self):
return self.data.get('urn') or self.data.get('spec_type') or ''

@property
def parent_id(self):
return self.data.get('parent_id', '')

@property
def extra(self):
return self.data.get('extra') or {}
Expand Down Expand Up @@ -157,6 +161,7 @@ class Device(CustomConfigHelper):
miot_entity = None
miot_results = None
_local_state = None
_proxy_device = None
_miot_mapping = None
_exclude_miot_services = None
_exclude_miot_properties = None
Expand Down Expand Up @@ -186,6 +191,14 @@ async def async_init(self):
if mps and self.miio2miot:
self.miio2miot.extend_miio_props(mps)

if self.info.pid not in [18]:
""" not proxy device """
elif parent := await self.get_parent_device():
if parent.use_local:
self.local = parent.local
self._proxy_device = parent if self.local else None
self.log.info('Proxy local device: %s', self.local)

self._exclude_miot_services = self.custom_config_list('exclude_miot_services', [])
self._exclude_miot_properties = self.custom_config_list('exclude_miot_properties', [])
self._unreadable_properties = self.custom_config_bool('unreadable_properties')
Expand Down Expand Up @@ -265,13 +278,17 @@ def identifiers(self):

@property
def hass_device_info(self):
via_device = None
if self._proxy_device:
via_device = next(iter(self._proxy_device.identifiers))
return {
'identifiers': self.identifiers,
'name': self.name,
'model': self.model,
'manufacturer': (self.model or 'Xiaomi').split('.', 1)[0],
'sw_version': self.sw_version,
'suggested_area': self.info.room_name,
'via_device': via_device,
'configuration_url': f'https://home.miot-spec.com/s/{self.model}',
}

Expand Down Expand Up @@ -721,6 +738,8 @@ def use_local(self):
return True
if self.model in MIOT_LOCAL_MODELS:
return True
if self._proxy_device:
return True
return False

@property
Expand All @@ -743,6 +762,12 @@ def auto_cloud(self):
return False
return self.custom_config_bool('auto_cloud')

async def get_parent_device(self):
if not (pid := self.info.parent_id):
return None
info = await self.entry.get_cloud_device(pid)
return await self.entry.new_device(info)

def miot_mapping(self):
if self._miot_mapping:
return self._miot_mapping
Expand Down
4 changes: 3 additions & 1 deletion custom_components/xiaomi_miot/core/hass_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry):
self.adders: dict[str, AddEntitiesCallback] = {}
self.devices: dict[str, 'Device'] = {}
self.mac_to_did = {}
self.did_to_unique = {}

@staticmethod
def init(hass: HomeAssistant, entry: ConfigEntry):
Expand Down Expand Up @@ -93,8 +94,9 @@ async def new_device(self, device_info: dict):
if device := self.devices.get(info.unique_id):
return device
device = Device(info, self)
await device.async_init()
self.devices[info.unique_id] = device
self.did_to_unique[info.did] = info.unique_id
await device.async_init()
return device

def new_adder(self, domain, adder: AddEntitiesCallback):
Expand Down

0 comments on commit 7e67534

Please sign in to comment.