-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Expose delivery window attributes (date_expected_end, timestamps) #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robbybarnes
wants to merge
3
commits into
jmdevita:main
Choose a base branch
from
robbybarnes:feature/expose-delivery-window-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−10
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,12 @@ | |
| recent_data["utc_timestamp"] = mock_datetime | ||
| # Modify the active parcel to be out for delivery tomorrow | ||
| recent_data["deliveries"][0]["date_expected"] = datetime.strftime(tomorrow,"%Y-%m-%d") + "T00:00:00Z" | ||
| # Set delivery window end to tomorrow as well | ||
| recent_data["deliveries"][0]["date_expected_end"] = datetime.strftime(tomorrow,"%Y-%m-%d") + "T17:00:00Z" | ||
| # Set timestamp values for delivery window | ||
| tomorrow_start = datetime.combine(tomorrow, datetime.min.time()) | ||
| recent_data["deliveries"][0]["timestamp_expected"] = int(tomorrow_start.timestamp()) | ||
| recent_data["deliveries"][0]["timestamp_expected_end"] = int(tomorrow_start.timestamp()) + 61200 # +17 hours | ||
| # Modify the active parcel's event date to be yesterday | ||
| recent_data["deliveries"][0]["events"][0]["date"] = datetime.strftime(yesterday,"%A, %B %-d, %Y %-I:%M %p") | ||
|
|
||
|
|
@@ -66,15 +72,17 @@ async def test_recent_shipment_sensor(hass): | |
|
|
||
| # Assert the state and attributes for the first delivery in the fixture | ||
| assert sensor.state == "Delivery in transit." | ||
| assert sensor.extra_state_attributes == { | ||
| "full_description": "Wireless Mouse Set", | ||
| "tracking_number": "8217400125612976", | ||
| "date_expected": tomorrow, | ||
| "event_date": yesterday, | ||
| "event_location": "Harrisburg, PA, USA", | ||
| "status": "Delivery in transit.", | ||
| "carrier": "Fedex", | ||
| } | ||
| attrs = sensor.extra_state_attributes | ||
| assert attrs["full_description"] == "Wireless Mouse Set" | ||
| assert attrs["tracking_number"] == "8217400125612976" | ||
| assert attrs["date_expected"] == tomorrow | ||
| assert attrs["date_expected_end"] == tomorrow | ||
| assert attrs["timestamp_expected"] == datetime.fromtimestamp(recent_data["deliveries"][0]["timestamp_expected"]) | ||
| assert attrs["timestamp_expected_end"] == datetime.fromtimestamp(recent_data["deliveries"][0]["timestamp_expected_end"]) | ||
| assert attrs["event_date"] == yesterday | ||
| assert attrs["event_location"] == "Harrisburg, PA, USA" | ||
| assert attrs["status"] == "Delivery in transit." | ||
| assert attrs["carrier"] == "Fedex" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
|
|
@@ -103,6 +111,9 @@ async def test_active_shipment_sensor(hass): | |
| 'full_description': 'Wireless Mouse Set', | ||
| 'tracking_number': '8217400125612976', | ||
| 'date_expected': tomorrow, | ||
| 'date_expected_end': None, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think ideally here we'd have a test that provides a value other than None. Can you add an update to the recent.json fixture to provide this? |
||
| 'timestamp_expected': None, | ||
| 'timestamp_expected_end': None, | ||
| 'days_until_next_delivery': 1, | ||
| 'event': 'Departure Scan', | ||
| 'event_date': yesterday, | ||
|
|
@@ -163,6 +174,9 @@ async def test_recent_shipment_sensor_no_data(hass): | |
| assert sensor.state == 'No parcels for now..' | ||
| assert sensor.extra_state_attributes == { | ||
| 'date_expected': 'None', | ||
| 'date_expected_end': None, | ||
| 'timestamp_expected': None, | ||
| 'timestamp_expected_end': None, | ||
| 'days_until_next_delivery': 'No active parcels.', | ||
| 'event': 'None', | ||
| 'event_date': 'None', | ||
|
|
@@ -198,6 +212,9 @@ async def test_active_shipment_sensor_no_data(hass): | |
| assert sensor.state == 'No parcels for now..' | ||
| assert sensor.extra_state_attributes == { | ||
| 'date_expected': 'None', | ||
| 'date_expected_end': None, | ||
| 'timestamp_expected': None, | ||
| 'timestamp_expected_end': None, | ||
| 'days_until_next_delivery': 'No active parcels.', | ||
| 'event': 'None', | ||
| 'event_date': 'None', | ||
|
|
@@ -260,6 +277,9 @@ async def test_recent_shipment_sensor_multi_data(hass): | |
| "full_description": "Collectable Parcel", | ||
| "tracking_number": "12345678", | ||
| "date_expected": "Unknown", | ||
| "date_expected_end": None, | ||
| "timestamp_expected": None, | ||
| "timestamp_expected_end": None, | ||
| "event_date": yesterday, | ||
| "event_location": "Somewhere", | ||
| "status": "Delivery expecting a pickup by the recipient.", | ||
|
|
@@ -289,6 +309,9 @@ async def test_active_shipment_sensor_multi_data(hass): | |
| assert sensor.state == '1 parcel' | ||
| assert sensor.extra_state_attributes == { | ||
| 'date_expected': today, | ||
| 'date_expected_end': None, | ||
| 'timestamp_expected': None, | ||
| 'timestamp_expected_end': None, | ||
| 'days_until_next_delivery': 0, | ||
| 'event': 'Postmark Mailpiece by Carrier', | ||
| 'event_date': yesterday, | ||
|
|
@@ -331,4 +354,70 @@ async def test_collectable_shipment_sensor_multi_data(hass): | |
| "delivered": datetime.strftime(yesterday,"%d.%m.%Y %H:%M") | ||
| } | ||
| ], | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_delivery_window_attributes(hass): | ||
| """Test that delivery window attributes are properly exposed on both sensors.""" | ||
| # Create test data with delivery window fields | ||
| tomorrow_ts = int(datetime.combine(tomorrow, datetime.min.time()).timestamp()) | ||
| tomorrow_end_ts = tomorrow_ts + 14400 # +4 hours | ||
|
|
||
| delivery_window_data = { | ||
| "success": True, | ||
| "deliveries": [ | ||
| { | ||
| "carrier_code": "fedex", | ||
| "description": "Package with delivery window", | ||
| "status_code": 4, # Out for delivery | ||
| "tracking_number": "1234567890", | ||
| "date_expected": datetime.strftime(tomorrow, "%Y-%m-%d") + "T09:00:00Z", | ||
| "date_expected_end": datetime.strftime(tomorrow, "%Y-%m-%d") + "T13:00:00Z", | ||
| "timestamp_expected": tomorrow_ts, | ||
| "timestamp_expected_end": tomorrow_end_ts, | ||
| "events": [ | ||
| { | ||
| "event": "Out for Delivery", | ||
| "date": datetime.strftime(today, "%A, %B %-d, %Y %-I:%M %p"), | ||
| "location": "Local Facility" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "carrier_codes": {"fedex": "FedEx"}, | ||
| "carrier_codes_updated": mock_datetime, | ||
| "utc_timestamp": mock_datetime, | ||
| } | ||
|
|
||
| # Mock the coordinator | ||
| mock_coordinator = AsyncMock(spec=ParcelUpdateCoordinator) | ||
| mock_coordinator.config_entry = AsyncMock(spec=ParcelUpdateCoordinator) | ||
| mock_coordinator.data = delivery_window_data | ||
| mock_coordinator.config_entry.entry_id = "test_entry_12345" | ||
|
|
||
| # Test RecentShipment sensor | ||
| recent_sensor = RecentShipment(mock_coordinator) | ||
| recent_sensor.hass = hass | ||
| recent_sensor.async_write_ha_state = Mock() | ||
| recent_sensor._handle_coordinator_update() | ||
|
|
||
| # Verify delivery window attributes on RecentShipment | ||
| attrs = recent_sensor.extra_state_attributes | ||
| assert attrs["date_expected"] == tomorrow | ||
| assert attrs["date_expected_end"] == tomorrow | ||
| assert attrs["timestamp_expected"] == datetime.fromtimestamp(tomorrow_ts) | ||
| assert attrs["timestamp_expected_end"] == datetime.fromtimestamp(tomorrow_end_ts) | ||
|
|
||
| # Test ActiveShipment sensor | ||
| active_sensor = ActiveShipment(mock_coordinator) | ||
| active_sensor.hass = hass | ||
| active_sensor.async_write_ha_state = Mock() | ||
| active_sensor._handle_coordinator_update() | ||
|
|
||
| # Verify delivery window attributes on ActiveShipment | ||
| attrs = active_sensor.extra_state_attributes | ||
| assert attrs["date_expected"] == tomorrow | ||
| assert attrs["date_expected_end"] == tomorrow | ||
| assert attrs["timestamp_expected"] == datetime.fromtimestamp(tomorrow_ts) | ||
| assert attrs["timestamp_expected_end"] == datetime.fromtimestamp(tomorrow_end_ts) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this also be added to the Recent sensor?