diff --git a/src/app/bindicator.py b/src/app/bindicator.py index c21dc79..1bcc9fa 100644 --- a/src/app/bindicator.py +++ b/src/app/bindicator.py @@ -1,7 +1,7 @@ import logging from .api import Api -from datetime import date +from datetime import date, timedelta import requests @@ -20,18 +20,36 @@ def run(self): logging.debug( f"{len(collections)} found publishing first collection date information" ) - if date.today() == collections[0].date: - message = ( - f"Bin collection is today for {collections[0].wheelie.bin_type}" + today = date.today() + tomorrow = today + timedelta(days=1) + collection_date = collections[0].date + bin_type = collections[0].wheelie.bin_type + if today == collection_date: + message = f"Bin collection is today for {bin_type}" + logging.info(message) + logging.info("Publishing message to ntfy.sh") + requests.post( + f"https://ntfy.sh/{self.topic}", + data=message.encode(encoding="utf-8"), + headers={ + "Title": "Binday Today", + "Priority": "3", + "Tags": "rotating_light", + }, ) + elif tomorrow == collection_date: + message = f"Bin collection is tomorrow for {bin_type}" logging.info(message) - logging.info(f"Publishing message to ntfy.sh") + logging.info("Publishing message to ntfy.sh") requests.post( f"https://ntfy.sh/{self.topic}", data=message.encode(encoding="utf-8"), + headers={ + "Title": "Binday Tomorrow", + "Priority": "3", + "Tags": "warning", + }, ) else: logging.info("No bin collection today") - logging.info( - f"Next bin collection is {collections[0].date}, {collections[0].wheelie.bin_type}" - ) + logging.info(f"Next bin collection is {collection_date}, {bin_type}") diff --git a/tests/test_bindicator.py b/tests/test_bindicator.py index 2e5a668..e147668 100644 --- a/tests/test_bindicator.py +++ b/tests/test_bindicator.py @@ -36,8 +36,40 @@ def test_bindicator_run_collections(mock_requests, mock_api): mock_requests.post.assert_called_once_with( "https://ntfy.sh/topic", data=f"Bin collection is today for {collection_date.wheelie.bin_type}".encode( - encoding="utf-8" + encoding="utf-8", ), + headers={ + "Title": "Binday Today", + "Priority": "3", + "Tags": "rotating_light", + }, + ) + + +@patch("src.app.bindicator.Api") +@patch("src.app.bindicator.requests") +def test_bincollection_tomorrow(mock_requests, mock_api): + bindicator = Bindicator("uprn", "topic") + today = date.today() + collection = Collection( + "recycling", (today + timedelta(days=1)).strftime("%d/%m/%Y") + ) + collection_date = CollectionDate(collection) + mock_api().get_data.return_value = [collection_date] + + bindicator.run() + + mock_api().get_data.assert_called_once() + mock_requests.post.assert_called_once_with( + "https://ntfy.sh/topic", + data=f"Bin collection is tomorrow for {collection_date.wheelie.bin_type}".encode( + encoding="utf-8", + ), + headers={ + "Title": "Binday Tomorrow", + "Priority": "3", + "Tags": "warning", + }, ) @@ -46,7 +78,7 @@ def test_bindicator_run_collections(mock_requests, mock_api): def test_no_collection_today(mock_requests, mock_api): bindicator = Bindicator("uprn", "topic") - future_date = (date.today() + timedelta(days=1)).strftime("%d/%m/%Y") + future_date = (date.today() + timedelta(days=2)).strftime("%d/%m/%Y") collection = Collection("recycling", future_date) collection_date = CollectionDate(collection) mock_api().get_data.return_value = [collection_date]