diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 20433f9..f770bdc 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: [ "3.9", "3.10", "3.11" ] + python-version: [ "3.10", "3.11" ] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} diff --git a/youless_api/gateway/__init__.py b/youless_api/gateway/__init__.py index 5f3352d..4088875 100644 --- a/youless_api/gateway/__init__.py +++ b/youless_api/gateway/__init__.py @@ -1,9 +1,25 @@ -from datetime import datetime +from datetime import datetime, timedelta from typing import Optional import requests +def _is_stale_timestamp(timestamp): + if timestamp in (None, 0): + return False + + timestamp = str(timestamp) + if len(timestamp) != 10 or not timestamp.isdigit(): + return True + + try: + timestamp = datetime.strptime(timestamp, "%y%m%d%H%M") + except ValueError: + return True + + return datetime.now() - timestamp > timedelta(hours=2) + + def fetch_generic_api(host, authentication=None) -> Optional[dict]: """Fetches the data from the Youless API on the /a endpoint.""" response = requests.get(f"http://{host}/a?f=j", auth=authentication, timeout=2) @@ -47,15 +63,12 @@ def fetch_enologic_api(host, authentication=None): 'ps0': None}, **response.json()[0] } - if 'gts' in corrected: - formatted_date = datetime.now().strftime("%y%m%d") + "0000" - if corrected["gts"] != 0 and int(formatted_date) >= corrected["gts"]: - corrected["gas"] = None - - if 'wts' in corrected: - formatted_date = datetime.now().strftime("%y%m%d") + "0000" - if corrected["wts"] != 0 and int(formatted_date) >= corrected["wts"]: - corrected["wtr"] = None + + if _is_stale_timestamp(corrected.get("gts")): + corrected["gas"] = None + + if _is_stale_timestamp(corrected.get("wts")): + corrected["wtr"] = None return corrected diff --git a/youless_api/test/test_gateway_enelogic.py b/youless_api/test/test_gateway_enelogic.py index 9f83466..f690b2b 100644 --- a/youless_api/test/test_gateway_enelogic.py +++ b/youless_api/test/test_gateway_enelogic.py @@ -87,6 +87,46 @@ def mock_enologic_error(*args, **kwargs) -> Response: class GatewayTest(TestCase): + @patch('youless_api.gateway.datetime') + @patch('youless_api.gateway.requests.get') + def test_enologic_recent_gas_from_previous_day_is_not_stale( + self, mock_get: MagicMock, mock_datetime: MagicMock): + mock_datetime.now.return_value = datetime(2026, 11, 27, 0, 1) + mock_datetime.strptime.side_effect = datetime.strptime + mock_get.return_value = Mock( + ok=True, + headers={'Content-Type': 'application/json'}, + json=lambda: [{ + "gas": 1624.264, + "gts": 2611262359, + "wtr": 1234.564, + "wts": 2611262359 + }] + ) + + dataset = fetch_enologic_api('localhost', None) + + self.assertEqual(dataset['gas'], 1624.264) + mock_get.assert_called_once_with('http://localhost/e', auth=None, timeout=2) + + @patch('youless_api.gateway.requests.get') + def test_enologic_invalid_timestamp_is_stale(self, mock_get: MagicMock): + mock_get.return_value = Mock( + ok=True, + headers={'Content-Type': 'application/json'}, + json=lambda: [{ + "gas": 1624.264, + "gts": 2613320000, + "wtr": 1234.564, + "wts": 0 + }] + ) + + dataset = fetch_enologic_api('localhost', None) + + self.assertIsNone(dataset['gas']) + self.assertEqual(dataset['wtr'], 1234.564) + @patch('youless_api.gateway.requests.get', side_effect=mock_ls120_ok) def test_enologic_correct(self, mock_get: MagicMock): dataset = fetch_enologic_api('localhost', None)