Use service helper to extract velbus config entry (#162813)

This commit is contained in:
epenet
2026-02-11 10:15:19 -08:00
committed by GitHub
parent 9f1b4c9035
commit 1d30486f82
2 changed files with 13 additions and 29 deletions
+13 -23
View File
@@ -8,11 +8,10 @@ from typing import TYPE_CHECKING
import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ADDRESS
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv, selector
from homeassistant.helpers import config_validation as cv, selector, service
from homeassistant.helpers.storage import STORAGE_DIR
if TYPE_CHECKING:
@@ -33,26 +32,11 @@ from .const import (
def async_setup_services(hass: HomeAssistant) -> None:
"""Register the velbus services."""
async def get_config_entry(call: ServiceCall) -> VelbusConfigEntry:
"""Get the config entry for this service call."""
entry_id: str = call.data[CONF_CONFIG_ENTRY]
if not (entry := hass.config_entries.async_get_entry(entry_id)):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="integration_not_found",
translation_placeholders={"target": DOMAIN},
)
if entry.state is not ConfigEntryState.LOADED:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="not_loaded",
translation_placeholders={"target": entry.title},
)
return entry
async def scan(call: ServiceCall) -> None:
"""Handle a scan service call."""
entry = await get_config_entry(call)
entry: VelbusConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[CONF_CONFIG_ENTRY]
)
try:
await entry.runtime_data.controller.scan()
except OSError as exc:
@@ -64,7 +48,9 @@ def async_setup_services(hass: HomeAssistant) -> None:
async def syn_clock(call: ServiceCall) -> None:
"""Handle a sync clock service call."""
entry = await get_config_entry(call)
entry: VelbusConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[CONF_CONFIG_ENTRY]
)
try:
await entry.runtime_data.controller.sync_clock()
except OSError as exc:
@@ -76,7 +62,9 @@ def async_setup_services(hass: HomeAssistant) -> None:
async def set_memo_text(call: ServiceCall) -> None:
"""Handle Memo Text service call."""
entry = await get_config_entry(call)
entry: VelbusConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[CONF_CONFIG_ENTRY]
)
memo_text = call.data[CONF_MEMO_TEXT]
address = call.data[CONF_ADDRESS]
module = entry.runtime_data.controller.get_module(address)
@@ -97,7 +85,9 @@ def async_setup_services(hass: HomeAssistant) -> None:
async def clear_cache(call: ServiceCall) -> None:
"""Handle a clear cache service call."""
entry = await get_config_entry(call)
entry: VelbusConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[CONF_CONFIG_ENTRY]
)
try:
if call.data.get(CONF_ADDRESS):
await hass.async_add_executor_job(
@@ -66,18 +66,12 @@
"connection_failed": {
"message": "Could not connect to Velbus."
},
"integration_not_found": {
"message": "Integration \"{target}\" not found in registry."
},
"invalid_hvac_mode": {
"message": "Climate mode {hvac_mode} is not supported."
},
"module_not_found": {
"message": "Module with address {address} not found."
},
"not_loaded": {
"message": "{target} is not loaded."
},
"scan_failed": {
"message": "Scan service: {error}."
},