Skip to content

Commit

Permalink
guard lan apis with init_done
Browse files Browse the repository at this point in the history
  • Loading branch information
chemwolf6922 committed Dec 27, 2024
1 parent 7b41773 commit 0edb526
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions custom_components/xiaomi_home/miot/miot_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -141,3 +142,7 @@ class MIoTConfigError(MIoTError):

class MIoTOptionsError(MIoTError):
...


class MIoTLanError(MIoTError):
...
35 changes: 28 additions & 7 deletions custom_components/xiaomi_home/miot/miot_lan.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
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 (
Expand Down Expand Up @@ -548,6 +548,12 @@ def __init__(
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
Expand Down Expand Up @@ -680,12 +686,16 @@ async def update_subscribe_option(self, enable_subscribe: bool) -> None:

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
Expand All @@ -703,6 +713,8 @@ def sub_device_state(
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(
Expand All @@ -711,6 +723,8 @@ def sub_device_state(

@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
Expand All @@ -724,6 +738,8 @@ def sub_prop(
piid: Optional[int] = None,
handler_ctx: Any = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
Expand All @@ -742,6 +758,8 @@ def unsub_prop(
siid: Optional[int] = None,
piid: Optional[int] = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
Expand All @@ -761,6 +779,8 @@ def sub_event(
eiid: Optional[int] = None,
handler_ctx: Any = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
Expand All @@ -779,6 +799,8 @@ def unsub_event(
siid: Optional[int] = None,
eiid: Optional[int] = None
) -> bool:
if not self._init_done:
return False
if not self._enable_subscribe:
return False
key = (
Expand All @@ -793,6 +815,7 @@ def unsub_event(
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',
Expand All @@ -813,6 +836,7 @@ async def set_prop_async(
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',
Expand All @@ -830,15 +854,14 @@ async def set_prop_async(
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',
Expand All @@ -850,9 +873,7 @@ async def action_async(
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(
Expand Down

0 comments on commit 0edb526

Please sign in to comment.