mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-20 09:19:37 +08:00
Compare commits
No commits in common. "0edb526e8ebfa994b8570d4981f0c523c28ec152" and "a69aa42ce7e13d645ab24c504256b1017c53632c" have entirely different histories.
0edb526e8e
...
a69aa42ce7
@ -74,7 +74,6 @@ class MIoTErrorCode(Enum):
|
|||||||
# Config flow error code, -10100
|
# Config flow error code, -10100
|
||||||
# Options flow error code , -10110
|
# Options flow error code , -10110
|
||||||
# MIoT lan error code, -10120
|
# MIoT lan error code, -10120
|
||||||
CODE_LAN_UNAVAILABLE = -10120
|
|
||||||
|
|
||||||
|
|
||||||
class MIoTError(Exception):
|
class MIoTError(Exception):
|
||||||
@ -142,7 +141,3 @@ class MIoTConfigError(MIoTError):
|
|||||||
|
|
||||||
class MIoTOptionsError(MIoTError):
|
class MIoTOptionsError(MIoTError):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
class MIoTLanError(MIoTError):
|
|
||||||
...
|
|
||||||
|
|||||||
@ -65,7 +65,7 @@ from cryptography.hazmat.backends import default_backend
|
|||||||
from cryptography.hazmat.primitives import hashes
|
from cryptography.hazmat.primitives import hashes
|
||||||
|
|
||||||
# pylint: disable=relative-beyond-top-level
|
# pylint: disable=relative-beyond-top-level
|
||||||
from .miot_error import MIoTError, MIoTLanError, MIoTErrorCode
|
from .miot_error import MIoTErrorCode
|
||||||
from .miot_network import InterfaceStatus, MIoTNetwork, NetworkInfo
|
from .miot_network import InterfaceStatus, MIoTNetwork, NetworkInfo
|
||||||
from .miot_mdns import MipsService, MipsServiceState
|
from .miot_mdns import MipsService, MipsServiceState
|
||||||
from .common import (
|
from .common import (
|
||||||
@ -548,12 +548,6 @@ class MIoTLan:
|
|||||||
0, lambda: self._main_loop.create_task(
|
0, lambda: self._main_loop.create_task(
|
||||||
self.init_async()))
|
self.init_async()))
|
||||||
|
|
||||||
def __assert_service_ready(self) -> None:
|
|
||||||
if not self._init_done:
|
|
||||||
raise MIoTLanError(
|
|
||||||
'MIoT lan is not ready',
|
|
||||||
MIoTErrorCode.CODE_LAN_UNAVAILABLE)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def virtual_did(self) -> str:
|
def virtual_did(self) -> str:
|
||||||
return self._virtual_did
|
return self._virtual_did
|
||||||
@ -686,16 +680,12 @@ class MIoTLan:
|
|||||||
|
|
||||||
def update_devices(self, devices: dict[str, dict]) -> bool:
|
def update_devices(self, devices: dict[str, dict]) -> bool:
|
||||||
_LOGGER.info('update devices, %s', devices)
|
_LOGGER.info('update devices, %s', devices)
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
self._internal_loop.call_soon_threadsafe(
|
self._internal_loop.call_soon_threadsafe(
|
||||||
self.__update_devices, devices)
|
self.__update_devices, devices)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def delete_devices(self, devices: list[str]) -> bool:
|
def delete_devices(self, devices: list[str]) -> bool:
|
||||||
_LOGGER.info('delete devices, %s', devices)
|
_LOGGER.info('delete devices, %s', devices)
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
self._internal_loop.call_soon_threadsafe(
|
self._internal_loop.call_soon_threadsafe(
|
||||||
self.__delete_devices, devices)
|
self.__delete_devices, devices)
|
||||||
return True
|
return True
|
||||||
@ -713,8 +703,6 @@ class MIoTLan:
|
|||||||
self, key: str, handler: Callable[[str, dict, Any], Coroutine],
|
self, key: str, handler: Callable[[str, dict, Any], Coroutine],
|
||||||
handler_ctx: Any = None
|
handler_ctx: Any = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
self._internal_loop.call_soon_threadsafe(
|
self._internal_loop.call_soon_threadsafe(
|
||||||
self.__sub_device_state,
|
self.__sub_device_state,
|
||||||
_MIoTLanSubDeviceData(
|
_MIoTLanSubDeviceData(
|
||||||
@ -723,8 +711,6 @@ class MIoTLan:
|
|||||||
|
|
||||||
@final
|
@final
|
||||||
def unsub_device_state(self, key: str) -> bool:
|
def unsub_device_state(self, key: str) -> bool:
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
self._internal_loop.call_soon_threadsafe(
|
self._internal_loop.call_soon_threadsafe(
|
||||||
self.__unsub_device_state, _MIoTLanUnsubDeviceData(key=key))
|
self.__unsub_device_state, _MIoTLanUnsubDeviceData(key=key))
|
||||||
return True
|
return True
|
||||||
@ -738,8 +724,6 @@ class MIoTLan:
|
|||||||
piid: Optional[int] = None,
|
piid: Optional[int] = None,
|
||||||
handler_ctx: Any = None
|
handler_ctx: Any = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
if not self._enable_subscribe:
|
if not self._enable_subscribe:
|
||||||
return False
|
return False
|
||||||
key = (
|
key = (
|
||||||
@ -758,8 +742,6 @@ class MIoTLan:
|
|||||||
siid: Optional[int] = None,
|
siid: Optional[int] = None,
|
||||||
piid: Optional[int] = None
|
piid: Optional[int] = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
if not self._enable_subscribe:
|
if not self._enable_subscribe:
|
||||||
return False
|
return False
|
||||||
key = (
|
key = (
|
||||||
@ -779,8 +761,6 @@ class MIoTLan:
|
|||||||
eiid: Optional[int] = None,
|
eiid: Optional[int] = None,
|
||||||
handler_ctx: Any = None
|
handler_ctx: Any = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
if not self._enable_subscribe:
|
if not self._enable_subscribe:
|
||||||
return False
|
return False
|
||||||
key = (
|
key = (
|
||||||
@ -799,8 +779,6 @@ class MIoTLan:
|
|||||||
siid: Optional[int] = None,
|
siid: Optional[int] = None,
|
||||||
eiid: Optional[int] = None
|
eiid: Optional[int] = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if not self._init_done:
|
|
||||||
return False
|
|
||||||
if not self._enable_subscribe:
|
if not self._enable_subscribe:
|
||||||
return False
|
return False
|
||||||
key = (
|
key = (
|
||||||
@ -815,7 +793,6 @@ class MIoTLan:
|
|||||||
async def get_prop_async(
|
async def get_prop_async(
|
||||||
self, did: str, siid: int, piid: int, timeout_ms: int = 10000
|
self, did: str, siid: int, piid: int, timeout_ms: int = 10000
|
||||||
) -> Any:
|
) -> Any:
|
||||||
self.__assert_service_ready()
|
|
||||||
result_obj = await self.__call_api_async(
|
result_obj = await self.__call_api_async(
|
||||||
did=did, msg={
|
did=did, msg={
|
||||||
'method': 'get_properties',
|
'method': 'get_properties',
|
||||||
@ -836,7 +813,6 @@ class MIoTLan:
|
|||||||
self, did: str, siid: int, piid: int, value: Any,
|
self, did: str, siid: int, piid: int, value: Any,
|
||||||
timeout_ms: int = 10000
|
timeout_ms: int = 10000
|
||||||
) -> dict:
|
) -> dict:
|
||||||
self.__assert_service_ready()
|
|
||||||
result_obj = await self.__call_api_async(
|
result_obj = await self.__call_api_async(
|
||||||
did=did, msg={
|
did=did, msg={
|
||||||
'method': 'set_properties',
|
'method': 'set_properties',
|
||||||
@ -854,14 +830,15 @@ class MIoTLan:
|
|||||||
return result_obj['result'][0]
|
return result_obj['result'][0]
|
||||||
if 'code' in result_obj:
|
if 'code' in result_obj:
|
||||||
return result_obj
|
return result_obj
|
||||||
raise MIoTError('Invalid result', MIoTErrorCode.CODE_INTERNAL_ERROR)
|
return {
|
||||||
|
'code': MIoTErrorCode.CODE_INTERNAL_ERROR.value,
|
||||||
|
'message': 'Invalid result'}
|
||||||
|
|
||||||
@final
|
@final
|
||||||
async def action_async(
|
async def action_async(
|
||||||
self, did: str, siid: int, aiid: int, in_list: list,
|
self, did: str, siid: int, aiid: int, in_list: list,
|
||||||
timeout_ms: int = 10000
|
timeout_ms: int = 10000
|
||||||
) -> dict:
|
) -> dict:
|
||||||
self.__assert_service_ready()
|
|
||||||
result_obj = await self.__call_api_async(
|
result_obj = await self.__call_api_async(
|
||||||
did=did, msg={
|
did=did, msg={
|
||||||
'method': 'action',
|
'method': 'action',
|
||||||
@ -873,7 +850,9 @@ class MIoTLan:
|
|||||||
return result_obj['result']
|
return result_obj['result']
|
||||||
if 'code' in result_obj:
|
if 'code' in result_obj:
|
||||||
return result_obj
|
return result_obj
|
||||||
raise MIoTError('Invalid result', MIoTErrorCode.CODE_INTERNAL_ERROR)
|
return {
|
||||||
|
'code': MIoTErrorCode.CODE_INTERNAL_ERROR.value,
|
||||||
|
'message': 'Invalid result'}
|
||||||
|
|
||||||
@final
|
@final
|
||||||
async def get_dev_list_async(
|
async def get_dev_list_async(
|
||||||
@ -931,7 +910,7 @@ class MIoTLan:
|
|||||||
await self.init_async()
|
await self.init_async()
|
||||||
return
|
return
|
||||||
self._internal_loop.call_soon_threadsafe(
|
self._internal_loop.call_soon_threadsafe(
|
||||||
self.__on_network_info_change,
|
self.__on_network_info_chnage,
|
||||||
_MIoTLanNetworkUpdateData(status=status, if_name=info.name))
|
_MIoTLanNetworkUpdateData(status=status, if_name=info.name))
|
||||||
|
|
||||||
async def __on_mips_service_change(
|
async def __on_mips_service_change(
|
||||||
@ -946,7 +925,7 @@ class MIoTLan:
|
|||||||
_LOGGER.info('no central service, init miot lan')
|
_LOGGER.info('no central service, init miot lan')
|
||||||
await self.init_async()
|
await self.init_async()
|
||||||
|
|
||||||
# The following methods SHOULD ONLY be called in the internal loop
|
# The folowing methods SHOULD ONLY be called in the internal loop
|
||||||
|
|
||||||
def ping(self, if_name: str | None, target_ip: str) -> None:
|
def ping(self, if_name: str | None, target_ip: str) -> None:
|
||||||
if not target_ip:
|
if not target_ip:
|
||||||
@ -1119,7 +1098,7 @@ class MIoTLan:
|
|||||||
continue
|
continue
|
||||||
lan_device.on_delete()
|
lan_device.on_delete()
|
||||||
|
|
||||||
def __on_network_info_change(self, data: _MIoTLanNetworkUpdateData) -> None:
|
def __on_network_info_chnage(self, data: _MIoTLanNetworkUpdateData) -> None:
|
||||||
if data.status == InterfaceStatus.ADD:
|
if data.status == InterfaceStatus.ADD:
|
||||||
self._available_net_ifs.add(data.if_name)
|
self._available_net_ifs.add(data.if_name)
|
||||||
if data.if_name in self._net_ifs:
|
if data.if_name in self._net_ifs:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user