Simplify synology_dsm service actions (#146612)

This commit is contained in:
epenet 2025-06-12 10:42:40 +02:00 committed by GitHub
parent 25e6eab008
commit 30dbd5a900
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 52 deletions

View File

@ -45,7 +45,7 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Synology DSM component."""
await async_setup_services(hass)
async_setup_services(hass)
return True

View File

@ -7,7 +7,7 @@ from typing import cast
from synology_dsm.exceptions import SynologyDSMException
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.core import HomeAssistant, ServiceCall, callback
from .const import CONF_SERIAL, DOMAIN, SERVICE_REBOOT, SERVICE_SHUTDOWN, SERVICES
from .coordinator import SynologyDSMConfigEntry
@ -15,63 +15,63 @@ from .coordinator import SynologyDSMConfigEntry
LOGGER = logging.getLogger(__name__)
async def async_setup_services(hass: HomeAssistant) -> None:
"""Service handler setup."""
async def _service_handler(call: ServiceCall) -> None:
"""Handle service call."""
serial: str | None = call.data.get(CONF_SERIAL)
entries: list[SynologyDSMConfigEntry] = (
call.hass.config_entries.async_loaded_entries(DOMAIN)
)
dsm_devices = {cast(str, entry.unique_id): entry.runtime_data for entry in entries}
async def service_handler(call: ServiceCall) -> None:
"""Handle service call."""
serial: str | None = call.data.get(CONF_SERIAL)
entries: list[SynologyDSMConfigEntry] = (
hass.config_entries.async_loaded_entries(DOMAIN)
if serial:
entry: SynologyDSMConfigEntry | None = (
call.hass.config_entries.async_entry_for_domain_unique_id(DOMAIN, serial)
)
dsm_devices = {
cast(str, entry.unique_id): entry.runtime_data for entry in entries
}
assert entry
dsm_device = entry.runtime_data
elif len(dsm_devices) == 1:
dsm_device = next(iter(dsm_devices.values()))
serial = next(iter(dsm_devices))
else:
LOGGER.error(
"More than one DSM configured, must specify one of serials %s",
sorted(dsm_devices),
)
return
if serial:
entry: SynologyDSMConfigEntry | None = (
hass.config_entries.async_entry_for_domain_unique_id(DOMAIN, serial)
)
assert entry
dsm_device = entry.runtime_data
elif len(dsm_devices) == 1:
dsm_device = next(iter(dsm_devices.values()))
serial = next(iter(dsm_devices))
else:
LOGGER.error(
"More than one DSM configured, must specify one of serials %s",
sorted(dsm_devices),
)
return
if not dsm_device:
LOGGER.error("DSM with specified serial %s not found", serial)
return
if not dsm_device:
if call.service in [SERVICE_REBOOT, SERVICE_SHUTDOWN]:
if serial not in dsm_devices:
LOGGER.error("DSM with specified serial %s not found", serial)
return
if call.service in [SERVICE_REBOOT, SERVICE_SHUTDOWN]:
if serial not in dsm_devices:
LOGGER.error("DSM with specified serial %s not found", serial)
return
LOGGER.debug("%s DSM with serial %s", call.service, serial)
LOGGER.warning(
(
"The %s service is deprecated and will be removed in future"
" release. Please use the corresponding button entity"
),
LOGGER.debug("%s DSM with serial %s", call.service, serial)
LOGGER.warning(
(
"The %s service is deprecated and will be removed in future"
" release. Please use the corresponding button entity"
),
call.service,
)
dsm_device = dsm_devices[serial]
dsm_api = dsm_device.api
try:
await getattr(dsm_api, f"async_{call.service}")()
except SynologyDSMException as ex:
LOGGER.error(
"%s of DSM with serial %s not possible, because of %s",
call.service,
serial,
ex,
)
dsm_device = dsm_devices[serial]
dsm_api = dsm_device.api
try:
await getattr(dsm_api, f"async_{call.service}")()
except SynologyDSMException as ex:
LOGGER.error(
"%s of DSM with serial %s not possible, because of %s",
call.service,
serial,
ex,
)
return
return
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Service handler setup."""
for service in SERVICES:
hass.services.async_register(DOMAIN, service, service_handler)
hass.services.async_register(DOMAIN, service, _service_handler)