Skip to content

Commit

Permalink
fix thread naming
Browse files Browse the repository at this point in the history
  • Loading branch information
chemwolf6922 committed Dec 23, 2024
1 parent 23bf681 commit d1c49bd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions custom_components/xiaomi_home/miot/miot_mips.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class _MipsClient(ABC):
_event_connect: asyncio.Event
_event_disconnect: asyncio.Event
_internal_loop: asyncio.AbstractEventLoop
_mips_thread: threading.Thread
_mips_thread: threading.Thread | None = None
_mips_reconnect_tag: bool
_mips_reconnect_interval: float
_mips_reconnect_timer: Optional[asyncio.TimerHandle]
Expand Down Expand Up @@ -302,20 +302,23 @@ def mips_state(self) -> bool:
"""
return self._mqtt and self._mqtt.is_connected()

@final
def connect(self) -> None:
def connect(self, thread_name: Optional[str] = None) -> None:
"""mips connect."""
# Start mips thread
if self._mips_thread:
return
self._internal_loop = asyncio.new_event_loop()
self._mips_thread = threading.Thread(target=self.__mips_loop_thread)
self._mips_thread.daemon = True
self._mips_thread.name = self._client_id
self._mips_thread.name = \
self._client_id if thread_name is None else thread_name
self._mips_thread.start()

@final
def close(self) -> None:
self._internal_loop.call_soon_threadsafe(self.__mips_close)
self._mips_thread.join()
if self._mips_thread:
self._mips_thread.join()
self._internal_loop.close()

self._logger = None
Expand Down Expand Up @@ -1066,8 +1069,6 @@ def __init__(
super().__init__(
client_id=did, host=host, port=port,
ca_file=ca_file, cert_file=cert_file, key_file=key_file, loop=loop)
# MIPS local thread name use group_id
self._mips_thread.name = self._group_id

@property
def group_id(self) -> str:
Expand All @@ -1085,6 +1086,11 @@ def log_error(self, msg, *args, **kwargs) -> None:
if self._logger:
self._logger.error(f'{self._home_name}, '+msg, *args, **kwargs)

@final
def connect(self, thread_name: Optional[str] = None) -> None:
# MIPS local thread name use group_id
super().connect(self._group_id)

@final
def disconnect(self) -> None:
super().disconnect()
Expand Down

0 comments on commit d1c49bd

Please sign in to comment.