Skip to content

Commit

Permalink
Allow miner to revert to last mining mode state when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rowan committed Nov 22, 2024
1 parent b46853f commit 70afb18
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions custom_components/miner/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
self._attr_unique_id = f"{self.coordinator.data['mac']}-active"
self._attr_is_on = self.coordinator.data["is_mining"]
self.updating_switch = False
self._last_mining_mode = None

@property
def name(self) -> str | None:
Expand All @@ -89,6 +90,10 @@ async def async_turn_on(self) -> None:
raise TypeError(f"{miner}: Shutdown not supported.")
self._attr_is_on = True
await miner.resume_mining()
if miner.supports_power_modes:
config = await miner.get_config()
config.mining_mode = self._last_mining_mode
await miner.send_config(config)
self.updating_switch = True
self.async_write_ha_state()

Expand All @@ -98,6 +103,8 @@ async def async_turn_off(self) -> None:
_LOGGER.debug(f"{self.coordinator.entry.title}: Stop mining.")
if not miner.supports_shutdown:
raise TypeError(f"{miner}: Shutdown not supported.")
if miner.supports_power_modes:
self._last_mining_mode = self.coordinator.data["config"].mining_mode
self._attr_is_on = False
await miner.stop_mining()
self.updating_switch = True
Expand Down

0 comments on commit 70afb18

Please sign in to comment.