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: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Synology DSM component.""" """Set up the Synology DSM component."""
await async_setup_services(hass) async_setup_services(hass)
return True return True

View File

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