Skip to content

Commit

Permalink
Fix issue when timestamp is None (#130133)
Browse files Browse the repository at this point in the history
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
  • Loading branch information
2 people authored and frenck committed Nov 8, 2024
1 parent b0b163d commit b71383c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
33 changes: 19 additions & 14 deletions homeassistant/components/seventeentrack/services.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Services for the seventeentrack integration."""

from typing import Final
from typing import Any, Final

from pyseventeentrack.package import PACKAGE_STATUS_MAP
from pyseventeentrack.package import PACKAGE_STATUS_MAP, Package
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, ConfigEntryState
Expand Down Expand Up @@ -81,18 +81,7 @@ async def get_packages(call: ServiceCall) -> ServiceResponse:

return {
"packages": [
{
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_TIMESTAMP: package.timestamp.isoformat(),
ATTR_INFO_TEXT: package.info_text,
ATTR_FRIENDLY_NAME: package.friendly_name,
}
package_to_dict(package)
for package in live_packages
if slugify(package.status) in package_states or package_states == []
]
Expand All @@ -110,6 +99,22 @@ async def archive_package(call: ServiceCall) -> None:

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:
Expand Down
29 changes: 29 additions & 0 deletions tests/components/seventeentrack/snapshots/test_services.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,32 @@
]),
})
# ---
# name: test_packages_with_none_timestamp
dict({
'packages': list([
dict({
'destination_country': 'Belgium',
'friendly_name': 'friendly name 1',
'info_text': 'info text 1',
'location': 'location 1',
'origin_country': 'Belgium',
'package_type': 'Registered Parcel',
'status': 'In Transit',
'tracking_info_language': 'Unknown',
'tracking_number': '456',
}),
dict({
'destination_country': 'Belgium',
'friendly_name': 'friendly name 2',
'info_text': 'info text 1',
'location': 'location 1',
'origin_country': 'Belgium',
'package_type': 'Registered Parcel',
'status': 'Delivered',
'timestamp': '2020-08-10T10:32:00+00:00',
'tracking_info_language': 'Unknown',
'tracking_number': '789',
}),
]),
})
# ---
38 changes: 38 additions & 0 deletions tests/components/seventeentrack/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ async def test_archive_package(
)


async def test_packages_with_none_timestamp(
hass: HomeAssistant,
mock_seventeentrack: AsyncMock,
mock_config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Ensure service returns all packages when non provided."""
await _mock_invalid_packages(mock_seventeentrack)
await init_integration(hass, mock_config_entry)
service_response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_PACKAGES,
{
CONFIG_ENTRY_ID_KEY: mock_config_entry.entry_id,
},
blocking=True,
return_response=True,
)

assert service_response == snapshot


async def _mock_packages(mock_seventeentrack):
package1 = get_package(status=10)
package2 = get_package(
Expand All @@ -167,3 +189,19 @@ async def _mock_packages(mock_seventeentrack):
package2,
package3,
]


async def _mock_invalid_packages(mock_seventeentrack):
package1 = get_package(
status=10,
timestamp=None,
)
package2 = get_package(
tracking_number="789",
friendly_name="friendly name 2",
status=40,
)
mock_seventeentrack.return_value.profile.packages.return_value = [
package1,
package2,
]

0 comments on commit b71383c

Please sign in to comment.