mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Use service helper to extract seventeentrack config entry (#162807)
This commit is contained in:
@@ -5,7 +5,6 @@ from typing import Any, Final
|
||||
from pyseventeentrack.package import PACKAGE_STATUS_MAP, Package
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
||||
from homeassistant.const import ATTR_CONFIG_ENTRY_ID, ATTR_FRIENDLY_NAME, ATTR_LOCATION
|
||||
from homeassistant.core import (
|
||||
HomeAssistant,
|
||||
@@ -14,8 +13,7 @@ from homeassistant.core import (
|
||||
SupportsResponse,
|
||||
callback,
|
||||
)
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import config_validation as cv, selector
|
||||
from homeassistant.helpers import config_validation as cv, selector, service
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from . import SeventeenTrackCoordinator
|
||||
@@ -72,13 +70,14 @@ SERVICE_ARCHIVE_PACKAGE_SCHEMA: Final = vol.Schema(
|
||||
|
||||
async def _get_packages(call: ServiceCall) -> ServiceResponse:
|
||||
"""Get packages from 17Track."""
|
||||
config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
package_states = call.data.get(ATTR_PACKAGE_STATE, [])
|
||||
|
||||
await _validate_service(call.hass, config_entry_id)
|
||||
entry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
)
|
||||
|
||||
seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][
|
||||
config_entry_id
|
||||
entry.entry_id
|
||||
]
|
||||
live_packages = sorted(
|
||||
await seventeen_coordinator.client.profile.packages(
|
||||
@@ -97,14 +96,15 @@ async def _get_packages(call: ServiceCall) -> ServiceResponse:
|
||||
|
||||
async def _add_package(call: ServiceCall) -> None:
|
||||
"""Add a new package to 17Track."""
|
||||
config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER]
|
||||
friendly_name = call.data[ATTR_PACKAGE_FRIENDLY_NAME]
|
||||
|
||||
await _validate_service(call.hass, config_entry_id)
|
||||
entry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
)
|
||||
|
||||
seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][
|
||||
config_entry_id
|
||||
entry.entry_id
|
||||
]
|
||||
|
||||
await seventeen_coordinator.client.profile.add_package(
|
||||
@@ -113,13 +113,14 @@ async def _add_package(call: ServiceCall) -> None:
|
||||
|
||||
|
||||
async def _archive_package(call: ServiceCall) -> None:
|
||||
config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER]
|
||||
|
||||
await _validate_service(call.hass, config_entry_id)
|
||||
entry = service.async_get_config_entry(
|
||||
call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
)
|
||||
|
||||
seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][
|
||||
config_entry_id
|
||||
entry.entry_id
|
||||
]
|
||||
|
||||
await seventeen_coordinator.client.profile.archive_package(tracking_number)
|
||||
@@ -142,26 +143,6 @@ def _package_to_dict(package: Package) -> dict[str, Any]:
|
||||
return result
|
||||
|
||||
|
||||
async def _validate_service(hass: HomeAssistant, config_entry_id: str) -> None:
|
||||
entry: ConfigEntry | None = hass.config_entries.async_get_entry(config_entry_id)
|
||||
if not entry:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_config_entry",
|
||||
translation_placeholders={
|
||||
"config_entry_id": config_entry_id,
|
||||
},
|
||||
)
|
||||
if entry.state != ConfigEntryState.LOADED:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="unloaded_config_entry",
|
||||
translation_placeholders={
|
||||
"config_entry_id": entry.title,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Set up the services for the seventeentrack integration."""
|
||||
|
||||
@@ -46,14 +46,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"invalid_config_entry": {
|
||||
"message": "Invalid config entry provided. Got {config_entry_id}"
|
||||
},
|
||||
"unloaded_config_entry": {
|
||||
"message": "Invalid config entry provided. {config_entry_id} is not loaded."
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
|
||||
Reference in New Issue
Block a user