Compare commits

...

3 Commits

Author SHA1 Message Date
Feng Wang
0edb526e8e guard lan apis with init_done 2024-12-27 19:22:48 +08:00
Feng Wang
7b41773f63 Fix typo in comments 2024-12-27 19:04:24 +08:00
Feng Wang
92ff6a4cf1 fix typo 2024-12-27 19:03:26 +08:00
2 changed files with 36 additions and 10 deletions

View File

@ -74,6 +74,7 @@ class MIoTErrorCode(Enum):
# Config flow error code, -10100
# Options flow error code , -10110
# MIoT lan error code, -10120
CODE_LAN_UNAVAILABLE = -10120
class MIoTError(Exception):
@ -141,3 +142,7 @@ class MIoTConfigError(MIoTError):
class MIoTOptionsError(MIoTError):
...
class MIoTLanError(MIoTError):
...

View File

@ -65,7 +65,7 @@ from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
# pylint: disable=relative-beyond-top-level
from .miot_error import MIoTErrorCode
from .miot_error import MIoTError, MIoTLanError, MIoTErrorCode
from .miot_network import InterfaceStatus, MIoTNetwork, NetworkInfo
from .miot_mdns import MipsService, MipsServiceState
from .common import (
@ -548,6 +548,12 @@ class MIoTLan:
0, lambda: self._main_loop.create_task(
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
def virtual_did(self) -> str:
return self._virtual_did
@ -680,12 +686,16 @@ class MIoTLan:
def update_devices(self, devices: dict[str, dict]) -> bool:
_LOGGER.info('update devices, %s', devices)
if not self._init_done:
return False
self._internal_loop.call_soon_threadsafe(
self.__update_devices, devices)
return True
def delete_devices(self, devices: list[str]) -> bool:
_LOGGER.info('delete devices, %s', devices)
if not self._init_done:
return False
self._internal_loop.call_soon_threadsafe(
self.__delete_devices, devices)
return True
@ -703,6 +713,8 @@ class MIoTLan:
self, key: str, handler: Callable[[str, dict, Any], Coroutine],
handler_ctx: Any = None
) -> bool:
if not self._init_done:
return False
self._internal_loop.call_soon_threadsafe(
self.__sub_device_state,
_MIoTLanSubDeviceData(
@ -711,6 +723,8 @@ class MIoTLan:
@final
def unsub_device_state(self, key: str) -> bool:
if not self._init_done:
return False
self._internal_loop.call_soon_threadsafe(
self.__unsub_device_state, _MIoTLanUnsubDeviceData(key=key))
return True
@ -724,6 +738,8 @@ class MIoTLan:
piid: Optional[int] = None,
handler_ctx: Any = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
@ -742,6 +758,8 @@ class MIoTLan:
siid: Optional[int] = None,
piid: Optional[int] = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
@ -761,6 +779,8 @@ class MIoTLan:
eiid: Optional[int] = None,
handler_ctx: Any = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
@ -779,6 +799,8 @@ class MIoTLan:
siid: Optional[int] = None,
eiid: Optional[int] = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
@ -793,6 +815,7 @@ class MIoTLan:
async def get_prop_async(
self, did: str, siid: int, piid: int, timeout_ms: int = 10000
) -> Any:
self.__assert_service_ready()
result_obj = await self.__call_api_async(
did=did, msg={
'method': 'get_properties',
@ -813,6 +836,7 @@ class MIoTLan:
self, did: str, siid: int, piid: int, value: Any,
timeout_ms: int = 10000
) -> dict:
self.__assert_service_ready()
result_obj = await self.__call_api_async(
did=did, msg={
'method': 'set_properties',
@ -830,15 +854,14 @@ class MIoTLan:
return result_obj['result'][0]
if 'code' in result_obj:
return result_obj
return {
'code': MIoTErrorCode.CODE_INTERNAL_ERROR.value,
'message': 'Invalid result'}
raise MIoTError('Invalid result', MIoTErrorCode.CODE_INTERNAL_ERROR)
@final
async def action_async(
self, did: str, siid: int, aiid: int, in_list: list,
timeout_ms: int = 10000
) -> dict:
self.__assert_service_ready()
result_obj = await self.__call_api_async(
did=did, msg={
'method': 'action',
@ -850,9 +873,7 @@ class MIoTLan:
return result_obj['result']
if 'code' in result_obj:
return result_obj
return {
'code': MIoTErrorCode.CODE_INTERNAL_ERROR.value,
'message': 'Invalid result'}
raise MIoTError('Invalid result', MIoTErrorCode.CODE_INTERNAL_ERROR)
@final
async def get_dev_list_async(
@ -910,7 +931,7 @@ class MIoTLan:
await self.init_async()
return
self._internal_loop.call_soon_threadsafe(
self.__on_network_info_chnage,
self.__on_network_info_change,
_MIoTLanNetworkUpdateData(status=status, if_name=info.name))
async def __on_mips_service_change(
@ -925,7 +946,7 @@ class MIoTLan:
_LOGGER.info('no central service, init miot lan')
await self.init_async()
# The folowing methods SHOULD ONLY be called in the internal loop
# The following methods SHOULD ONLY be called in the internal loop
def ping(self, if_name: str | None, target_ip: str) -> None:
if not target_ip:
@ -1098,7 +1119,7 @@ class MIoTLan:
continue
lan_device.on_delete()
def __on_network_info_chnage(self, data: _MIoTLanNetworkUpdateData) -> None:
def __on_network_info_change(self, data: _MIoTLanNetworkUpdateData) -> None:
if data.status == InterfaceStatus.ADD:
self._available_net_ifs.add(data.if_name)
if data.if_name in self._net_ifs: