1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-14 23:28:42 +00:00

Use service helper to extract ohme config entry (#162801)

This commit is contained in:
epenet
2026-02-11 17:18:25 +01:00
committed by GitHub
parent 66a0b44284
commit bf79721e97
3 changed files with 5 additions and 31 deletions

View File

@@ -5,7 +5,6 @@ from typing import Final
from ohme import OhmeApiClient
import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import (
HomeAssistant,
ServiceCall,
@@ -13,8 +12,7 @@ from homeassistant.core import (
SupportsResponse,
callback,
)
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import selector
from homeassistant.helpers import selector, service
from .const import DOMAIN
from .coordinator import OhmeConfigEntry
@@ -48,25 +46,9 @@ SERVICE_SET_PRICE_CAP_SCHEMA: Final = vol.Schema(
def __get_client(call: ServiceCall) -> OhmeApiClient:
"""Get the client from the config entry."""
entry_id: str = call.data[ATTR_CONFIG_ENTRY]
entry: OhmeConfigEntry | None = call.hass.config_entries.async_get_entry(entry_id)
if not entry:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="invalid_config_entry",
translation_placeholders={
"config_entry": entry_id,
},
)
if entry.state != ConfigEntryState.LOADED:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="unloaded_config_entry",
translation_placeholders={
"config_entry": entry.title,
},
)
entry: OhmeConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
)
return entry.runtime_data.charge_session_coordinator.client

View File

@@ -121,12 +121,6 @@
},
"device_info_failed": {
"message": "Unable to get Ohme device information"
},
"invalid_config_entry": {
"message": "Invalid config entry provided. Got {config_entry}"
},
"unloaded_config_entry": {
"message": "Invalid config entry provided. {config_entry} is not loaded."
}
},
"services": {

View File

@@ -85,9 +85,7 @@ async def test_list_charge_slots_exception(
await setup_integration(hass, mock_config_entry)
# Test error
with pytest.raises(
ServiceValidationError, match="Invalid config entry provided. Got invalid"
):
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
await hass.services.async_call(
DOMAIN,
SERVICE_LIST_CHARGE_SLOTS,