forked from M-Labs/thermostat
Update thermostat state from controller code
This commit is contained in:
parent
60672d9590
commit
97d26e3c65
@ -40,14 +40,9 @@ class Thermostat(QObject, metaclass=PropertyMeta):
|
|||||||
self.connection_state = ThermostatConnectionState.DISCONNECTED
|
self.connection_state = ThermostatConnectionState.DISCONNECTED
|
||||||
|
|
||||||
async def start_session(self, host, port):
|
async def start_session(self, host, port):
|
||||||
self.connection_state = ThermostatConnectionState.CONNECTING
|
|
||||||
|
|
||||||
await self._client.connect(host, port)
|
await self._client.connect(host, port)
|
||||||
self.hw_rev = await self._client.hw_rev()
|
self.hw_rev = await self._client.hw_rev()
|
||||||
|
|
||||||
self.connection_state = ThermostatConnectionState.CONNECTED
|
|
||||||
self.start_watching()
|
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
self._update_params_task = asyncio.create_task(self.update_params())
|
self._update_params_task = asyncio.create_task(self.update_params())
|
||||||
while True:
|
while True:
|
||||||
@ -60,6 +55,7 @@ class Thermostat(QObject, metaclass=PropertyMeta):
|
|||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
await self.end_session()
|
await self.end_session()
|
||||||
|
self.connection_state = ThermostatConnectionState.DISCONNECTED
|
||||||
self.connection_error.emit()
|
self.connection_error.emit()
|
||||||
return
|
return
|
||||||
self._update_params_task = asyncio.create_task(self.update_params())
|
self._update_params_task = asyncio.create_task(self.update_params())
|
||||||
@ -131,7 +127,6 @@ class Thermostat(QObject, metaclass=PropertyMeta):
|
|||||||
self.disconnect_cb()
|
self.disconnect_cb()
|
||||||
|
|
||||||
await self._client.disconnect()
|
await self._client.disconnect()
|
||||||
self.connection_state = ThermostatConnectionState.DISCONNECTED
|
|
||||||
|
|
||||||
async def set_ipv4(self, ipv4):
|
async def set_ipv4(self, ipv4):
|
||||||
await self._client.set_param("ipv4", ipv4)
|
await self._client.set_param("ipv4", ipv4)
|
||||||
|
@ -188,6 +188,7 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
|
|||||||
|
|
||||||
await self._thermostat.reset()
|
await self._thermostat.reset()
|
||||||
await self._thermostat.end_session()
|
await self._thermostat.end_session()
|
||||||
|
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
|
||||||
|
|
||||||
@asyncSlot(bool)
|
@asyncSlot(bool)
|
||||||
async def dfu_request(self, _):
|
async def dfu_request(self, _):
|
||||||
@ -195,6 +196,7 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
|
|||||||
|
|
||||||
await self._thermostat.dfu()
|
await self._thermostat.dfu()
|
||||||
await self._thermostat.end_session()
|
await self._thermostat.end_session()
|
||||||
|
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
|
||||||
|
|
||||||
@asyncSlot(bool)
|
@asyncSlot(bool)
|
||||||
async def net_settings_request(self, _):
|
async def net_settings_request(self, _):
|
||||||
@ -210,3 +212,4 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
|
|||||||
|
|
||||||
await self._thermostat.set_ipv4(ipv4_settings)
|
await self._thermostat.set_ipv4(ipv4_settings)
|
||||||
await self._thermostat.end_session()
|
await self._thermostat.end_session()
|
||||||
|
self._thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
|
@ -128,6 +128,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
async def closeEvent(self, _event):
|
async def closeEvent(self, _event):
|
||||||
try:
|
try:
|
||||||
await self.thermostat.end_session()
|
await self.thermostat.end_session()
|
||||||
|
self.thermostat.connection_state = ThermostatConnectionState.DISCONNECTED
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -181,27 +182,39 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
|
|
||||||
@asyncSlot()
|
@asyncSlot()
|
||||||
async def on_connect_btn_clicked(self):
|
async def on_connect_btn_clicked(self):
|
||||||
if (self._connecting_task is None) and (not self.thermostat.connected()):
|
match self.thermostat.connection_state:
|
||||||
self._connecting_task = asyncio.create_task(
|
case ThermostatConnectionState.DISCONNECTED:
|
||||||
self.thermostat.start_session(
|
self._connecting_task = asyncio.create_task(
|
||||||
host=self.conn_menu.host_set_line.text(),
|
self.thermostat.start_session(
|
||||||
port=self.conn_menu.port_set_spin.value(),
|
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
|
self._connecting_task = None
|
||||||
|
|
||||||
elif self._connecting_task is not None:
|
case ThermostatConnectionState.CONNECTED:
|
||||||
self._connecting_task.cancel()
|
await self.thermostat.end_session()
|
||||||
else:
|
self.thermostat.connection_state = (
|
||||||
await self.thermostat.end_session()
|
ThermostatConnectionState.DISCONNECTED
|
||||||
|
)
|
||||||
|
|
||||||
@asyncSlot(int)
|
@asyncSlot(int)
|
||||||
async def on_report_box_stateChanged(self, enabled):
|
async def on_report_box_stateChanged(self, enabled):
|
||||||
|
Loading…
Reference in New Issue
Block a user