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
16 changes: 16 additions & 0 deletions youless_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file contains a helper class to easily obtain data from the YouLess sensor.
"""
import logging
from datetime import datetime
from typing import Optional

from youless_api.const import SensorType
Expand Down Expand Up @@ -94,6 +95,11 @@ def current_power_usage(self) -> Optional[YoulessSensor]:
"""Get the current power usage."""
return self._cache_data[SensorType.POWER_USAGE] if SensorType.POWER_USAGE in self._cache_data else None

@property
def average_power(self) -> Optional[YoulessSensor]:
"""Get the average power usage of active Tarif."""
return self._cache_data[SensorType.POWER_AVERAGE] if SensorType.POWER_AVERAGE in self._cache_data else None

@property
def power_meter(self) -> Optional[PowerMeter]:
"""Get the power meter values."""
Expand Down Expand Up @@ -124,6 +130,16 @@ def phase3(self) -> Optional[Phase]:
"""Get the phase 1 information"""
return self._cache_data[SensorType.PHASE3] if SensorType.PHASE3 in self._cache_data else None

@property
def peak_power(self) -> Optional[YoulessSensor]:
"""Get the peak power of the month."""
return self._cache_data[SensorType.MONTH_PEAK] if SensorType.MONTH_PEAK in self._cache_data else None

@property
def peak_power_time(self) -> Optional[datetime]:
"""Get the date time of the peak time."""
return self._cache_data[SensorType.MONTH_PEAK_TIME] if SensorType.MONTH_PEAK_TIME in self._cache_data else None

@property
def secured(self) -> bool:
"""Flag indicating if the API has authentication or not."""
Expand Down
3 changes: 3 additions & 0 deletions youless_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ class SensorType(Enum):
PHASE2 = "phase2"
PHASE3 = "phase3"
TARIFF = "tariff"
MONTH_PEAK = "month_peak"
MONTH_PEAK_TIME = "month_peak_time"
POWER_AVERAGE = "power_average"
4 changes: 4 additions & 0 deletions youless_api/device/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Optional

from youless_api.gateway import fetch_enologic_api, fetch_generic_api, fetch_phase_api
Expand Down Expand Up @@ -73,6 +74,9 @@ def update() -> Optional[dict]:
YoulessSensor(phase_info['v3'], 'V'),
YoulessSensor(phase_info['l3'], 'W')) if 'i3' in phase_info else None,
SensorType.TARIFF: phase_info['tr'] if phase_info else None,
SensorType.MONTH_PEAK: YoulessSensor(phase_info['pp'], 'W') if 'pp' in phase_info else None,
SensorType.POWER_AVERAGE: YoulessSensor(phase_info['pa'], 'W') if 'pa' in phase_info else None,
SensorType.MONTH_PEAK_TIME: datetime.strptime(str(phase_info['pts']), '%y%m%d%H%M') if 'pts' in phase_info and phase_info['pts'] > 0 else None,
}

return update
Expand Down
10 changes: 8 additions & 2 deletions youless_api/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def mock_ls120(*args, **kwargs) -> Response:
"i2": 0.123,
"v3": 240,
"l3": 230,
"i3": 0.123
"i3": 0.123,
"pp": 1200,
"pts": int(datetime.now().strftime("%y%m%d%H%M")),
"pa": 400
}
)

Expand Down Expand Up @@ -179,7 +182,10 @@ def test_device_ls120(self, mock_get: MagicMock):
self.assertEqual(api.current_power_usage.value, 2382)
self.assertEqual(api.power_meter.high.value, 4490.631)
self.assertEqual(api.power_meter.low.value, 4703.562)
self.assertEqual(api.power_meter.total.value, 9194.164)
self.assertEqual(9194.164, api.power_meter.total.value)
self.assertEqual(400, api.average_power.value)
self.assertEqual(1200, api.peak_power.value)
self.assertEqual(datetime.now().replace(second=0, microsecond=0), api.peak_power_time)

mock_get.assert_any_call('http://192.1.1.1/d', auth=None, timeout=2)
mock_get.assert_any_call('http://192.1.1.1/e', auth=None, timeout=2)
Expand Down
Loading