Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
33 changes: 23 additions & 10 deletions youless_api/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions youless_api/test/test_gateway_enelogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading