1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +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 from ohme import OhmeApiClient
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import ( from homeassistant.core import (
HomeAssistant, HomeAssistant,
ServiceCall, ServiceCall,
@@ -13,8 +12,7 @@ from homeassistant.core import (
SupportsResponse, SupportsResponse,
callback, callback,
) )
from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import selector, service
from homeassistant.helpers import selector
from .const import DOMAIN from .const import DOMAIN
from .coordinator import OhmeConfigEntry from .coordinator import OhmeConfigEntry
@@ -48,25 +46,9 @@ SERVICE_SET_PRICE_CAP_SCHEMA: Final = vol.Schema(
def __get_client(call: ServiceCall) -> OhmeApiClient: def __get_client(call: ServiceCall) -> OhmeApiClient:
"""Get the client from the config entry.""" """Get the client from the config entry."""
entry_id: str = call.data[ATTR_CONFIG_ENTRY] entry: OhmeConfigEntry = service.async_get_config_entry(
entry: OhmeConfigEntry | None = call.hass.config_entries.async_get_entry(entry_id) call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
)
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,
},
)
return entry.runtime_data.charge_session_coordinator.client return entry.runtime_data.charge_session_coordinator.client

View File

@@ -121,12 +121,6 @@
}, },
"device_info_failed": { "device_info_failed": {
"message": "Unable to get Ohme device information" "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": { "services": {

View File

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