thermostat: Properly register task

Also Thermostat.task -> Thermostat._update_params_task
This commit is contained in:
atse 2024-08-22 17:48:24 +08:00
parent 958fddf953
commit dcf628b542

View File

@ -34,22 +34,23 @@ class Thermostat(QObject, metaclass=PropertyMeta):
self._watch_task = None self._watch_task = None
self._report_mode_task = None self._report_mode_task = None
self._poll_for_report = True self._poll_for_report = True
self._update_params_task = None
super().__init__(parent) super().__init__(parent)
async def run(self): async def run(self):
self.task = asyncio.create_task(self.update_params()) self._update_params_task = asyncio.create_task(self.update_params())
while True: while True:
if self.task.done(): if self._update_params_task.done():
if self.task.exception() is not None: if self._update_params_task.exception() is not None:
try: try:
raise self.task.exception() raise self._update_params_task.exception()
except asyncio.TimeoutError: except asyncio.TimeoutError:
logging.error( logging.error(
"Encountered an error while polling for information from Thermostat.", "Encountered an error while polling for information from Thermostat.",
exc_info=True, exc_info=True,
) )
_ = self.task.result() _ = self._update_params_task.result()
self.task = asyncio.create_task(self.update_params()) self._update_params_task = asyncio.create_task(self.update_params())
await asyncio.sleep(self._update_s) await asyncio.sleep(self._update_s)
async def get_hw_rev(self): async def get_hw_rev(self):
@ -83,8 +84,8 @@ class Thermostat(QObject, metaclass=PropertyMeta):
await self.set_report_mode(False) await self.set_report_mode(False)
self._watch_task.cancel() self._watch_task.cancel()
self._watch_task = None self._watch_task = None
self.task.cancel() self._update_params_task.cancel()
self.task = None self._update_params_task = None
async def set_report_mode(self, enabled: bool): async def set_report_mode(self, enabled: bool):
self._poll_for_report = not enabled self._poll_for_report = not enabled