Skip to content

Commit

Permalink
Improve services handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rowan committed Jun 21, 2024
1 parent ab9a866 commit 89eb1b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
24 changes: 17 additions & 7 deletions custom_components/miner/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@
async def async_setup_services(hass: HomeAssistant) -> None:
"""Service handler setup."""

async def reboot(call: ServiceCall) -> None:
async def get_miner(call: ServiceCall):
ip = call.data.get(CONF_IP)
miners = hass.data[DOMAIN]

if ip:
return await pyasic.get_miner(ip)
else:
LOGGER.error(
f"Cannot reboot, must specify an IP from [{miners}]",
)


miner = await pyasic.get_miner(ip)
await miner.reboot()
async def reboot(call: ServiceCall) -> None:
miner = await get_miner(call)
if miner is not None:
await miner.reboot()

hass.services.async_register(DOMAIN, SERVICE_REBOOT, reboot)

async def restart_backend(call: ServiceCall) -> None:
ip = call.data.get(CONF_IP)

miner = await pyasic.get_miner(ip)
await miner.restart_backend()
miner = await get_miner(call)
if miner is not None:
await miner.restart_backend()

hass.services.async_register(DOMAIN, SERVICE_RESTART_BACKEND, restart_backend)
13 changes: 13 additions & 0 deletions custom_components/miner/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
reboot:
fields:
ip:
example: 192.168.1.60
selector:
text:

restart_backend:
fields:
ip:
example: 192.168.1.60
selector:
text:

0 comments on commit 89eb1b0

Please sign in to comment.