mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 19:57:07 +00:00
Simplify seventeentrack service actions (#146610)
* Simplify seventeentrack service actions * callback
This commit is contained in:
parent
74a92e2cd8
commit
c34596e54d
@ -13,7 +13,7 @@ from homeassistant.helpers.typing import ConfigType
|
|||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import SeventeenTrackCoordinator
|
from .coordinator import SeventeenTrackCoordinator
|
||||||
from .services import setup_services
|
from .services import async_setup_services
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
|||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the 17Track component."""
|
"""Set up the 17Track component."""
|
||||||
|
|
||||||
setup_services(hass)
|
async_setup_services(hass)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ from homeassistant.core import (
|
|||||||
ServiceCall,
|
ServiceCall,
|
||||||
ServiceResponse,
|
ServiceResponse,
|
||||||
SupportsResponse,
|
SupportsResponse,
|
||||||
|
callback,
|
||||||
)
|
)
|
||||||
from homeassistant.exceptions import ServiceValidationError
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
from homeassistant.helpers import config_validation as cv, selector
|
from homeassistant.helpers import config_validation as cv, selector
|
||||||
@ -70,100 +71,106 @@ SERVICE_ARCHIVE_PACKAGE_SCHEMA: Final = vol.Schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_services(hass: HomeAssistant) -> None:
|
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)
|
||||||
|
|
||||||
|
seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][
|
||||||
|
config_entry_id
|
||||||
|
]
|
||||||
|
live_packages = sorted(
|
||||||
|
await seventeen_coordinator.client.profile.packages(
|
||||||
|
show_archived=seventeen_coordinator.show_archived
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"packages": [
|
||||||
|
_package_to_dict(package)
|
||||||
|
for package in live_packages
|
||||||
|
if slugify(package.status) in package_states or package_states == []
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][
|
||||||
|
config_entry_id
|
||||||
|
]
|
||||||
|
|
||||||
|
await seventeen_coordinator.client.profile.add_package(
|
||||||
|
tracking_number, friendly_name
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][
|
||||||
|
config_entry_id
|
||||||
|
]
|
||||||
|
|
||||||
|
await seventeen_coordinator.client.profile.archive_package(tracking_number)
|
||||||
|
|
||||||
|
|
||||||
|
def _package_to_dict(package: Package) -> dict[str, Any]:
|
||||||
|
result = {
|
||||||
|
ATTR_DESTINATION_COUNTRY: package.destination_country,
|
||||||
|
ATTR_ORIGIN_COUNTRY: package.origin_country,
|
||||||
|
ATTR_PACKAGE_TYPE: package.package_type,
|
||||||
|
ATTR_TRACKING_INFO_LANGUAGE: package.tracking_info_language,
|
||||||
|
ATTR_TRACKING_NUMBER: package.tracking_number,
|
||||||
|
ATTR_LOCATION: package.location,
|
||||||
|
ATTR_STATUS: package.status,
|
||||||
|
ATTR_INFO_TEXT: package.info_text,
|
||||||
|
ATTR_FRIENDLY_NAME: package.friendly_name,
|
||||||
|
}
|
||||||
|
if timestamp := package.timestamp:
|
||||||
|
result[ATTR_TIMESTAMP] = timestamp.isoformat()
|
||||||
|
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."""
|
"""Set up the services for the seventeentrack integration."""
|
||||||
|
|
||||||
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(config_entry_id)
|
|
||||||
|
|
||||||
seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][
|
|
||||||
config_entry_id
|
|
||||||
]
|
|
||||||
live_packages = sorted(
|
|
||||||
await seventeen_coordinator.client.profile.packages(
|
|
||||||
show_archived=seventeen_coordinator.show_archived
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"packages": [
|
|
||||||
package_to_dict(package)
|
|
||||||
for package in live_packages
|
|
||||||
if slugify(package.status) in package_states or package_states == []
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
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(config_entry_id)
|
|
||||||
|
|
||||||
seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][
|
|
||||||
config_entry_id
|
|
||||||
]
|
|
||||||
|
|
||||||
await seventeen_coordinator.client.profile.add_package(
|
|
||||||
tracking_number, friendly_name
|
|
||||||
)
|
|
||||||
|
|
||||||
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(config_entry_id)
|
|
||||||
|
|
||||||
seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][
|
|
||||||
config_entry_id
|
|
||||||
]
|
|
||||||
|
|
||||||
await seventeen_coordinator.client.profile.archive_package(tracking_number)
|
|
||||||
|
|
||||||
def package_to_dict(package: Package) -> dict[str, Any]:
|
|
||||||
result = {
|
|
||||||
ATTR_DESTINATION_COUNTRY: package.destination_country,
|
|
||||||
ATTR_ORIGIN_COUNTRY: package.origin_country,
|
|
||||||
ATTR_PACKAGE_TYPE: package.package_type,
|
|
||||||
ATTR_TRACKING_INFO_LANGUAGE: package.tracking_info_language,
|
|
||||||
ATTR_TRACKING_NUMBER: package.tracking_number,
|
|
||||||
ATTR_LOCATION: package.location,
|
|
||||||
ATTR_STATUS: package.status,
|
|
||||||
ATTR_INFO_TEXT: package.info_text,
|
|
||||||
ATTR_FRIENDLY_NAME: package.friendly_name,
|
|
||||||
}
|
|
||||||
if timestamp := package.timestamp:
|
|
||||||
result[ATTR_TIMESTAMP] = timestamp.isoformat()
|
|
||||||
return result
|
|
||||||
|
|
||||||
async def _validate_service(config_entry_id):
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_GET_PACKAGES,
|
SERVICE_GET_PACKAGES,
|
||||||
get_packages,
|
_get_packages,
|
||||||
schema=SERVICE_GET_PACKAGES_SCHEMA,
|
schema=SERVICE_GET_PACKAGES_SCHEMA,
|
||||||
supports_response=SupportsResponse.ONLY,
|
supports_response=SupportsResponse.ONLY,
|
||||||
)
|
)
|
||||||
@ -171,13 +178,13 @@ def setup_services(hass: HomeAssistant) -> None:
|
|||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_ADD_PACKAGE,
|
SERVICE_ADD_PACKAGE,
|
||||||
add_package,
|
_add_package,
|
||||||
schema=SERVICE_ADD_PACKAGE_SCHEMA,
|
schema=SERVICE_ADD_PACKAGE_SCHEMA,
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_ARCHIVE_PACKAGE,
|
SERVICE_ARCHIVE_PACKAGE,
|
||||||
archive_package,
|
_archive_package,
|
||||||
schema=SERVICE_ARCHIVE_PACKAGE_SCHEMA,
|
schema=SERVICE_ARCHIVE_PACKAGE_SCHEMA,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user