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