Update thermostat state from controller code

This commit is contained in:
atse 2024-09-09 11:41:17 +08:00
parent b1e5c86cdc
commit d576395ad2
3 changed files with 36 additions and 25 deletions

View File

@ -40,14 +40,9 @@ class Thermostat(QObject, metaclass=PropertyMeta):
self.connection_state = ThermostatConnectionState.DISCONNECTED
async def start_session(self, host, port):
self.connection_state = ThermostatConnectionState.CONNECTING
await self._client.connect(host, port)
self.hw_rev = await self._client.hw_rev()
self.connection_state = ThermostatConnectionState.CONNECTED
self.start_watching()
async def run(self):
self._update_params_task = asyncio.create_task(self.update_params())
while True:
@ -60,6 +55,7 @@ class Thermostat(QObject, metaclass=PropertyMeta):
exc_info=True,
)
await self.end_session()
self.connection_state = ThermostatConnectionState.DISCONNECTED
self.connection_error.emit()
return
self._update_params_task = asyncio.create_task(self.update_params())
@ -131,7 +127,6 @@ class Thermostat(QObject, metaclass=PropertyMeta):
self.disconnect_cb()
await self._client.disconnect()
self.connection_state = ThermostatConnectionState.DISCONNECTED
async def set_ipv4(self, ipv4):
await self._client.set_param("ipv4", ipv4)

View File

@ -188,6 +188,7 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
await self._thermostat.reset()
await self._thermostat.end_session()
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
@asyncSlot(bool)
async def dfu_request(self, _):
@ -195,6 +196,7 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
await self._thermostat.dfu()
await self._thermostat.end_session()
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
@asyncSlot(bool)
async def net_settings_request(self, _):
@ -209,4 +211,5 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
assert self._thermostat.connected()
await self._thermostat.set_ipv4(ipv4_settings)
await self._thermostat.end_session()
await self._thermostat.end_session()
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED

View File

@ -128,6 +128,7 @@ class MainWindow(QtWidgets.QMainWindow):
async def closeEvent(self, _event):
try:
await self.thermostat.end_session()
self.thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
except:
pass
@ -181,27 +182,39 @@ class MainWindow(QtWidgets.QMainWindow):
@asyncSlot()
async def on_connect_btn_clicked(self):
if (self._connecting_task is None) and (not self.thermostat.connected()):
self._connecting_task = asyncio.create_task(
self.thermostat.start_session(
host=self.conn_menu.host_set_line.text(),
port=self.conn_menu.port_set_spin.value(),
match self.thermostat.connection_state:
case ThermostatConnectionState.DISCONNECTED:
self._connecting_task = asyncio.create_task(
self.thermostat.start_session(
host=self.conn_menu.host_set_line.text(),
port=self.conn_menu.port_set_spin.value(),
)
)
self.thermostat.connection_state = ThermostatConnectionState.CONNECTING
try:
await self._connecting_task
except (OSError, asyncio.CancelledError) as exc:
await self.thermostat.end_session()
if isinstance(exc, asyncio.CancelledError):
return
raise
finally:
self._connecting_task = None
self.thermostat.connection_state = ThermostatConnectionState.CONNECTED
self.thermostat.start_watching()
case ThermostatConnectionState.CONNECTING:
self._connecting_task.cancel()
self.thermostat.connection_state = (
ThermostatConnectionState.DISCONNECTED
)
)
try:
await self._connecting_task
except (OSError, asyncio.CancelledError) as exc:
await self.thermostat.end_session()
if isinstance(exc, asyncio.CancelledError):
return
raise
finally:
self._connecting_task = None
elif self._connecting_task is not None:
self._connecting_task.cancel()
else:
await self.thermostat.end_session()
case ThermostatConnectionState.CONNECTED:
await self.thermostat.end_session()
self.thermostat.connection_state = (
ThermostatConnectionState.DISCONNECTED
)
@asyncSlot(int)
async def on_report_box_stateChanged(self, enabled):