Skip to content

Commit

Permalink
Update switch handling to only update after the switch mode matches t…
Browse files Browse the repository at this point in the history
…he working mode.
  • Loading branch information
b-rowan committed Apr 7, 2024
1 parent 82907aa commit b23af33
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions custom_components/miner/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(
super().__init__(coordinator=coordinator)
self._attr_unique_id = f"{self.coordinator.data['mac']}-active"
self._attr_is_on = self.coordinator.data["is_mining"]
self.updating_switch = False

@property
def name(self) -> str | None:
Expand All @@ -88,7 +89,7 @@ async def async_turn_on(self) -> None:
raise TypeError(f"{miner}: Shutdown not supported.")
self._attr_is_on = True
await miner.resume_mining()
self.coordinator.data["is_mining"] = True
self.updating_switch = True
self.async_write_ha_state()

async def async_turn_off(self) -> None:
Expand All @@ -99,14 +100,18 @@ async def async_turn_off(self) -> None:
raise TypeError(f"{miner}: Shutdown not supported.")
self._attr_is_on = False
await miner.stop_mining()
self.coordinator.data["is_mining"] = False
self.updating_switch = True
self.async_write_ha_state()

@callback
def _handle_coordinator_update(self) -> None:
is_mining = self.coordinator.data["is_mining"]
if is_mining is not None:
self._attr_is_on = self.coordinator.data["is_mining"]
if self.updating_switch:
if is_mining == self._attr_is_on:
self.updating_switch = False
if not self.updating_switch:
self._attr_is_on = is_mining

super()._handle_coordinator_update()

Expand Down

0 comments on commit b23af33

Please sign in to comment.