-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update pyasic version, and add reboot and restart services.
- Loading branch information
Showing
8 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Provides device actions for Miner.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
import voluptuous as vol | ||
from homeassistant.const import ATTR_ENTITY_ID | ||
from homeassistant.const import CONF_DEVICE_ID | ||
from homeassistant.const import CONF_DOMAIN | ||
from homeassistant.const import CONF_ENTITY_ID | ||
from homeassistant.const import CONF_TYPE | ||
from homeassistant.core import Context | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import config_validation as cv | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from .const import DOMAIN | ||
from .const import SERVICE_REBOOT | ||
from .const import SERVICE_RESTART_BACKEND | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
ACTION_TYPES = {"reboot", "restart_backend"} | ||
|
||
ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( | ||
{ | ||
vol.Required(CONF_TYPE): vol.In(ACTION_TYPES), | ||
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN), | ||
} | ||
) | ||
|
||
|
||
async def async_get_actions( | ||
hass: HomeAssistant, device_id: str | ||
) -> list[dict[str, str]]: | ||
"""List device actions for Miner devices.""" | ||
registry = er.async_get(hass) | ||
actions = [] | ||
|
||
# Get all the integrations entities for this device | ||
for entry in er.async_entries_for_device(registry, device_id): | ||
if entry.domain != DOMAIN: | ||
continue | ||
|
||
# Add actions for each entity that belongs to this integration | ||
base_action = { | ||
CONF_DEVICE_ID: device_id, | ||
CONF_DOMAIN: DOMAIN, | ||
CONF_ENTITY_ID: entry.entity_id, | ||
} | ||
for action_type in ACTION_TYPES: | ||
try: | ||
actions.append( | ||
{ | ||
**base_action, | ||
CONF_TYPE: action_type, | ||
} | ||
) | ||
except AttributeError: | ||
_LOGGER.error( | ||
"Failed to run device command for miner: Unable to access entry data." | ||
) | ||
|
||
return actions | ||
|
||
|
||
async def async_call_action_from_config( | ||
hass: HomeAssistant, config: dict, variables: dict, context: Context | None | ||
) -> None: | ||
"""Execute a device action.""" | ||
service = None | ||
service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]} | ||
|
||
if config[CONF_TYPE] == "reboot": | ||
service = SERVICE_REBOOT | ||
elif config[CONF_TYPE] == "restart_backend": | ||
service = SERVICE_RESTART_BACKEND | ||
|
||
if service is None: | ||
_LOGGER.error(f"Failed to call the service {config[CONF_TYPE]} for miner.") | ||
return | ||
await hass.services.async_call( | ||
DOMAIN, service, service_data, blocking=True, context=context | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""The Miner component services.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
import pyasic | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.core import ServiceCall | ||
|
||
from .const import CONF_IP | ||
from .const import DOMAIN | ||
from .const import SERVICE_REBOOT | ||
from .const import SERVICE_RESTART_BACKEND | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_services(hass: HomeAssistant) -> None: | ||
"""Service handler setup.""" | ||
|
||
async def reboot(call: ServiceCall) -> None: | ||
ip = call.data.get(CONF_IP) | ||
|
||
miner = await pyasic.get_miner(ip) | ||
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() | ||
|
||
hass.services.async_register(DOMAIN, SERVICE_RESTART_BACKEND, restart_backend) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
colorlog==6.7.0 | ||
homeassistant>=2024.1.0 | ||
homeassistant>=2024.6.3 | ||
pip>=21.0,<23.2 | ||
ruff==0.0.267 | ||
pyasic==0.54.8 | ||
pyasic==0.57.6 | ||
setuptools==69.0.3 | ||
pre-commit |