diff --git a/CHANGELOG.md b/CHANGELOG.md index 05244ca..aa16d96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## v3.0.2 + +- Fix unload integration process +- Add last reset date for sensors (none, daily, monthly) +- Correct state_class of sensors +- Correct data of total cost per low & rate and sewage consumption +- Fix convert to string function of data models + ## v3.0.1 - Add total cost per low & rate and sewage consumption diff --git a/custom_components/citymind_water_meter/__init__.py b/custom_components/citymind_water_meter/__init__.py index 3ce674f..d4553d6 100644 --- a/custom_components/citymind_water_meter/__init__.py +++ b/custom_components/citymind_water_meter/__init__.py @@ -81,10 +81,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" _LOGGER.info(f"Unloading {DOMAIN} integration, Entry ID: {entry.entry_id}") - coordinator: Coordinator = hass.data[DOMAIN][entry.entry_id] - - await coordinator.terminate() - for platform in PLATFORMS: await hass.config_entries.async_forward_entry_unload(entry, platform) diff --git a/custom_components/citymind_water_meter/common/entity_descriptions.py b/custom_components/citymind_water_meter/common/entity_descriptions.py index 7ac54c8..4a340a1 100644 --- a/custom_components/citymind_water_meter/common/entity_descriptions.py +++ b/custom_components/citymind_water_meter/common/entity_descriptions.py @@ -13,13 +13,14 @@ from homeassistant.helpers.entity import EntityDescription from .consts import UNIT_COST -from .enums import EntityKeys, EntityType +from .enums import EntityKeys, EntityType, ResetPolicy @dataclass(frozen=True, kw_only=True) class IntegrationEntityDescription(EntityDescription): platform: Platform | None = None entity_type: EntityType | None + reset_policy: ResetPolicy = ResetPolicy.NONE @dataclass(frozen=True, kw_only=True) @@ -64,6 +65,7 @@ class IntegrationNumberEntityDescription( icon="mdi:meter-gas", state_class=SensorStateClass.TOTAL, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.MONTHLY, ), IntegrationSensorEntityDescription( key=EntityKeys.LAST_READ, @@ -71,6 +73,7 @@ class IntegrationNumberEntityDescription( device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.MONTHLY, ), IntegrationSensorEntityDescription( key=EntityKeys.MONTHLY_CONSUMPTION, @@ -78,6 +81,7 @@ class IntegrationNumberEntityDescription( device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.MONTHLY, ), IntegrationSensorEntityDescription( key=EntityKeys.TODAYS_CONSUMPTION, @@ -85,6 +89,7 @@ class IntegrationNumberEntityDescription( device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.DAILY, ), IntegrationSensorEntityDescription( key=EntityKeys.YESTERDAYS_CONSUMPTION, @@ -92,6 +97,7 @@ class IntegrationNumberEntityDescription( device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.DAILY, ), IntegrationSensorEntityDescription( key=EntityKeys.HIGH_RATE_CONSUMPTION, @@ -99,6 +105,7 @@ class IntegrationNumberEntityDescription( device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.MONTHLY, ), IntegrationSensorEntityDescription( key=EntityKeys.LOW_RATE_CONSUMPTION, @@ -106,14 +113,16 @@ class IntegrationNumberEntityDescription( device_class=SensorDeviceClass.WATER, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, + reset_policy=ResetPolicy.MONTHLY, ), IntegrationSensorEntityDescription( key=EntityKeys.LOW_RATE_TOTAL_COST, entity_type=EntityType.METER, entity_category=EntityCategory.DIAGNOSTIC, - state_class=SensorStateClass.MEASUREMENT, + state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UNIT_COST, icon="mdi:currency-ils", + reset_policy=ResetPolicy.MONTHLY, ), IntegrationNumberEntityDescription( key=EntityKeys.LOW_RATE_COST, @@ -130,9 +139,10 @@ class IntegrationNumberEntityDescription( key=EntityKeys.HIGH_RATE_TOTAL_COST, entity_type=EntityType.METER, entity_category=EntityCategory.DIAGNOSTIC, - state_class=SensorStateClass.MEASUREMENT, + state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UNIT_COST, icon="mdi:currency-ils", + reset_policy=ResetPolicy.MONTHLY, ), IntegrationNumberEntityDescription( key=EntityKeys.HIGH_RATE_COST, @@ -149,9 +159,10 @@ class IntegrationNumberEntityDescription( key=EntityKeys.SEWAGE_TOTAL_COST, entity_type=EntityType.METER, entity_category=EntityCategory.DIAGNOSTIC, - state_class=SensorStateClass.MEASUREMENT, + state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UNIT_COST, icon="mdi:currency-ils", + reset_policy=ResetPolicy.MONTHLY, ), IntegrationNumberEntityDescription( key=EntityKeys.SEWAGE_COST, diff --git a/custom_components/citymind_water_meter/common/enums.py b/custom_components/citymind_water_meter/common/enums.py index f82c94d..ac3bae9 100644 --- a/custom_components/citymind_water_meter/common/enums.py +++ b/custom_components/citymind_water_meter/common/enums.py @@ -17,6 +17,12 @@ class EntityType(StrEnum): ACCOUNT = "account" +class ResetPolicy(Enum): + NONE = 0 + DAILY = 1 + MONTHLY = 2 + + class EntityKeys(StrEnum): CONSUMPTION_FORECAST = "consumption_forecast" LAST_READ = "last_read" diff --git a/custom_components/citymind_water_meter/data_processors/base_processor.py b/custom_components/citymind_water_meter/data_processors/base_processor.py index 1af9539..8e58778 100644 --- a/custom_components/citymind_water_meter/data_processors/base_processor.py +++ b/custom_components/citymind_water_meter/data_processors/base_processor.py @@ -23,6 +23,7 @@ class BaseProcessor: _last_name: str | None = None _today_iso: str | None = None _yesterday_iso: str | None = None + _current_month_iso: str | None = None _config_manager: ConfigManager | None = None _config_data: ConfigData | None = None _unique_messages: list[str] | None = None @@ -35,16 +36,18 @@ def __init__(self, config_manager: ConfigManager): self._account_number = None self._first_name = None self._last_name = None - self._today_iso = None - self._yesterday_iso = None self.processor_type = None self._unique_messages = [] - def update(self, api_data: dict, today_iso: str, yesterday_iso: str): + def update(self, api_data: dict): self._api_data = api_data - self._today_iso = today_iso - self._yesterday_iso = yesterday_iso + + analytic_periods = self._config_manager.analytic_periods + + self._today_iso = analytic_periods.today_iso + self._yesterday_iso = analytic_periods.yesterday_iso + self._current_month_iso = analytic_periods.current_month_iso self._process_api_data() diff --git a/custom_components/citymind_water_meter/data_processors/meter_processor.py b/custom_components/citymind_water_meter/data_processors/meter_processor.py index 0bf1006..62c7abe 100644 --- a/custom_components/citymind_water_meter/data_processors/meter_processor.py +++ b/custom_components/citymind_water_meter/data_processors/meter_processor.py @@ -148,10 +148,6 @@ def _load_meter( last_read_value = last_read_details.get(meter_id, 0) last_read = self._format_number(last_read_value, 3) - date_parts = self._today_iso.split("-") - month_parts = date_parts[:2] - current_month = "-".join(month_parts) - yesterday_consumption = self._get_consumption( daily_consumption_section, meter_id, self._yesterday_iso ) @@ -159,7 +155,7 @@ def _load_meter( daily_consumption_section, meter_id, self._today_iso ) monthly_consumption = self._get_consumption( - monthly_consumption_section, meter_id, current_month + monthly_consumption_section, meter_id, self._current_month_iso ) consumption_forecast_data = consumption_forecast_section.get(str(meter_id)) diff --git a/custom_components/citymind_water_meter/managers/config_manager.py b/custom_components/citymind_water_meter/managers/config_manager.py index 174612a..73edd4c 100644 --- a/custom_components/citymind_water_meter/managers/config_manager.py +++ b/custom_components/citymind_water_meter/managers/config_manager.py @@ -29,6 +29,7 @@ STORAGE_DATA_METERS, ) from ..common.entity_descriptions import IntegrationEntityDescription +from ..models.analytics_periods import AnalyticPeriodsData from ..models.config_data import ConfigData _LOGGER = logging.getLogger(__name__) @@ -47,6 +48,8 @@ class ConfigManager: _is_set_up_mode: bool _is_initialized: bool + analytic_periods: AnalyticPeriodsData + def __init__(self, hass: HomeAssistant | None, entry: ConfigEntry | None = None): self._hass = hass self._entry = entry @@ -65,6 +68,8 @@ def __init__(self, hass: HomeAssistant | None, entry: ConfigEntry | None = None) self._is_set_up_mode = entry is None self._is_initialized = False + self.analytic_periods = AnalyticPeriodsData() + if hass is not None: self._store = Store( hass, STORAGE_VERSION, CONFIGURATION_FILE, encoder=JSONEncoder diff --git a/custom_components/citymind_water_meter/managers/coordinator.py b/custom_components/citymind_water_meter/managers/coordinator.py index 8c85c98..b8b0e65 100644 --- a/custom_components/citymind_water_meter/managers/coordinator.py +++ b/custom_components/citymind_water_meter/managers/coordinator.py @@ -91,9 +91,10 @@ def __init__(self, hass, config_manager: ConfigManager): entry.async_on_unload(async_dispatcher_connect(hass, signal, handler)) config_data = config_manager.config_data + analytic_periods = config_manager.analytic_periods entry_id = config_manager.entry_id - self._api = RestAPI(self.hass, config_data, entry_id) + self._api = RestAPI(self.hass, config_data, analytic_periods, entry_id) self._config_manager = config_manager @@ -176,6 +177,8 @@ async def _on_api_status_changed(self, entry_id: str, status: ConnectivityStatus return if status == ConnectivityStatus.Connected: + self.config_manager.analytic_periods.update() + await self._api.update() elif status in [ConnectivityStatus.Failed]: @@ -222,7 +225,7 @@ async def _on_data_changed(self, entry_id: str): if api_connected: for processor_type in self._processors: processor = self._processors[processor_type] - processor.update(self._api.data, self._api.today, self._api.yesterday) + processor.update(self._api.data) account = self._account_processor.get() @@ -252,6 +255,8 @@ async def _async_update_data(self): now = datetime.now().timestamp() if now - self._last_update >= self.update_interval.total_seconds(): + self.config_manager.analytic_periods.update() + await self._api.update() self._last_update = now @@ -277,8 +282,11 @@ def _build_data_mapping(self): EntityKeys.HIGH_RATE_CONSUMPTION: self._get_high_rate_consumption_data, EntityKeys.LOW_RATE_CONSUMPTION: self._get_low_rate_consumption_data, EntityKeys.LOW_RATE_COST: self._get_low_rate_cost_data, + EntityKeys.LOW_RATE_TOTAL_COST: self._get_low_rate_total_cost_data, EntityKeys.HIGH_RATE_COST: self._get_high_rate_cost_data, + EntityKeys.HIGH_RATE_TOTAL_COST: self._get_high_rate_total_cost_data, EntityKeys.SEWAGE_COST: self._get_sewage_cost_data, + EntityKeys.SEWAGE_TOTAL_COST: self._get_sewage_total_cost_data, EntityKeys.LOW_RATE_CONSUMPTION_THRESHOLD: self._get_low_rate_consumption_threshold_data, EntityKeys.ALERTS: self._get_alerts_data, EntityKeys.ALERT_EXCEEDED_THRESHOLD_SMS: self._get_alert_setting_data, @@ -448,6 +456,15 @@ def _get_low_rate_cost_data( return result + def _get_low_rate_total_cost_data( + self, _entity_description, meter_id: str + ) -> dict | None: + data = self._meter_processor.get_data(meter_id) + + result = {ATTR_STATE: data.low_rate_cost * data.low_rate_monthly_consumption} + + return result + def _get_high_rate_cost_data( self, _entity_description, meter_id: str ) -> dict | None: @@ -462,6 +479,15 @@ def _get_high_rate_cost_data( return result + def _get_high_rate_total_cost_data( + self, _entity_description, meter_id: str + ) -> dict | None: + data = self._meter_processor.get_data(meter_id) + + result = {ATTR_STATE: data.high_rate_cost * data.high_rate_monthly_consumption} + + return result + def _get_sewage_cost_data(self, _entity_description, meter_id: str) -> dict | None: data = self._meter_processor.get_data(meter_id) @@ -474,6 +500,15 @@ def _get_sewage_cost_data(self, _entity_description, meter_id: str) -> dict | No return result + def _get_sewage_total_cost_data( + self, _entity_description, meter_id: str + ) -> dict | None: + data = self._meter_processor.get_data(meter_id) + + result = {ATTR_STATE: data.sewage_cost * data.monthly_consumption} + + return result + def _get_low_rate_consumption_threshold_data( self, _entity_description, meter_id: str ) -> dict | None: diff --git a/custom_components/citymind_water_meter/managers/rest_api.py b/custom_components/citymind_water_meter/managers/rest_api.py index a4e0213..35df920 100644 --- a/custom_components/citymind_water_meter/managers/rest_api.py +++ b/custom_components/citymind_water_meter/managers/rest_api.py @@ -2,7 +2,7 @@ import asyncio from collections.abc import Awaitable -from datetime import datetime, timedelta +from datetime import datetime import logging import sys from typing import Any, Callable @@ -38,8 +38,6 @@ ENDPOINT_PARAMETER_TODAY, ENDPOINT_PARAMETER_YESTERDAY, ERROR_REASON_INVALID_CREDENTIALS, - FORMAT_DATE_ISO, - FORMAT_DATE_YEAR_MONTH, LOGIN_DEVICE_ID, LOGIN_EMAIL, LOGIN_PASSWORD, @@ -49,6 +47,7 @@ SIGNAL_DATA_CHANGED, ) from ..common.enums import AlertChannel, AlertType +from ..models.analytics_periods import AnalyticPeriodsData from ..models.config_data import ConfigData _LOGGER = logging.getLogger(__name__) @@ -56,6 +55,9 @@ class RestAPI: _hass: HomeAssistant | None + _config_data: ConfigData + _analytic_periods: AnalyticPeriodsData + data: dict _status: ConnectivityStatus | None @@ -66,18 +68,13 @@ class RestAPI: _last_valid: datetime | None - today: str | None - yesterday: str | None - first_day_of_month: str | None - _last_day_of_current_month: str | None - current_month: str | None - _alert_settings_actions: dict[bool, Callable[[str, list[int]], Awaitable[dict]]] def __init__( self, hass: HomeAssistant | None, config_data: ConfigData, + analytic_periods: AnalyticPeriodsData, entry_id: str | None = None, ): try: @@ -87,6 +84,7 @@ def __init__( self.data = {} self._config_data = config_data + self._analytic_periods = analytic_periods self._local_async_dispatcher_send = None @@ -98,12 +96,6 @@ def __init__( self._dispatched_server = False self._last_valid = None - self.today = None - self.yesterday = None - self.first_day_of_month = None - self._last_day_of_current_month = None - self.current_month = None - self._alert_settings_actions = { True: self._async_put, False: self._async_delete, @@ -174,8 +166,6 @@ async def update(self): await self.initialize(self._config_data) if self.status == ConnectivityStatus.Connected: - self._set_date() - if self.municipal_id is None: await self._load_data(ENDPOINT_DATA_INITIALIZE) @@ -278,39 +268,17 @@ def _async_dispatcher_send(self, signal: str, *args: Any) -> None: else: dispatcher_send(self._hass, signal, self._entry_id, *args) - def _set_date(self): - today = datetime.now() - yesterday = today - timedelta(days=1) - - year = today.year if today.month <= 11 else today.year + 1 - month = today.month + 1 if today.month <= 11 else 1 - - next_month_date = datetime(year=year, month=month, day=1) - last_day_of_current_month = next_month_date - timedelta(days=1) - - first_day_of_month = today.replace( - day=1, hour=0, minute=0, second=0, microsecond=0 - ) - - self.today = today.strftime(FORMAT_DATE_ISO) - self.yesterday = yesterday.strftime(FORMAT_DATE_ISO) - self.current_month = today.strftime(FORMAT_DATE_YEAR_MONTH) - self.first_day_of_month = first_day_of_month.strftime(FORMAT_DATE_ISO) - self._last_day_of_current_month = last_day_of_current_month.strftime( - FORMAT_DATE_ISO - ) - def _build_endpoint( self, endpoint, meter_count: str | None = None, alert_type: str | None = None ): data = { ENDPOINT_PARAMETER_METER_ID: meter_count, - ENDPOINT_PARAMETER_YESTERDAY: self.yesterday, - ENDPOINT_PARAMETER_TODAY: self.today, - ENDPOINT_PARAMETER_LAST_DAY_MONTH: self._last_day_of_current_month, - ENDPOINT_PARAMETER_MUNICIPALITY_ID: self.municipal_id, - ENDPOINT_PARAMETER_CURRENT_MONTH: self.current_month, ENDPOINT_PARAMETER_ALERT_TYPE: alert_type, + ENDPOINT_PARAMETER_MUNICIPALITY_ID: self.municipal_id, + ENDPOINT_PARAMETER_YESTERDAY: self._analytic_periods.yesterday_iso, + ENDPOINT_PARAMETER_TODAY: self._analytic_periods.today_iso, + ENDPOINT_PARAMETER_LAST_DAY_MONTH: self._analytic_periods.last_date_of_month_iso, + ENDPOINT_PARAMETER_CURRENT_MONTH: self._analytic_periods.current_month_iso, } url = endpoint.format(**data) diff --git a/custom_components/citymind_water_meter/manifest.json b/custom_components/citymind_water_meter/manifest.json index 05c5828..db7f947 100644 --- a/custom_components/citymind_water_meter/manifest.json +++ b/custom_components/citymind_water_meter/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/maorcc/citymind_water_meter/issues", "requirements": [], - "version": "3.0.1" + "version": "3.0.2" } diff --git a/custom_components/citymind_water_meter/models/account_data.py b/custom_components/citymind_water_meter/models/account_data.py index a765f69..e0eb5b7 100644 --- a/custom_components/citymind_water_meter/models/account_data.py +++ b/custom_components/citymind_water_meter/models/account_data.py @@ -49,6 +49,6 @@ def to_dict(self): return obj def __repr__(self): - to_string = json.dumps(self, default=str) + to_string = json.dumps(self.to_dict(), default=str) return to_string diff --git a/custom_components/citymind_water_meter/models/analytics_periods.py b/custom_components/citymind_water_meter/models/analytics_periods.py new file mode 100644 index 0000000..8c9d4cd --- /dev/null +++ b/custom_components/citymind_water_meter/models/analytics_periods.py @@ -0,0 +1,86 @@ +import calendar +from datetime import datetime, timedelta +import json + +from custom_components.citymind_water_meter.common.consts import ( + FORMAT_DATE_ISO, + FORMAT_DATE_YEAR_MONTH, +) + + +class AnalyticPeriodsData: + today: datetime | None + yesterday: datetime | None + first_date_of_month: datetime | None + last_date_of_month: datetime | None + + def __init__(self): + self.today = datetime.now() + self.yesterday = None + self.first_date_of_month = None + self.last_date_of_month = None + + self.update() + + @property + def today_iso(self) -> str | None: + return None if self.today is None else self.today.strftime(FORMAT_DATE_ISO) + + @property + def yesterday_iso(self) -> str | None: + return ( + None if self.yesterday is None else self.yesterday.strftime(FORMAT_DATE_ISO) + ) + + @property + def current_month_iso(self) -> str | None: + return ( + None + if self.first_date_of_month is None + else self.first_date_of_month.strftime(FORMAT_DATE_YEAR_MONTH) + ) + + @property + def first_date_of_month_iso(self) -> str | None: + return ( + None + if self.first_date_of_month is None + else self.first_date_of_month.strftime(FORMAT_DATE_ISO) + ) + + @property + def last_date_of_month_iso(self) -> str | None: + return ( + None + if self.last_date_of_month is None + else self.last_date_of_month.strftime(FORMAT_DATE_ISO) + ) + + def update(self, current_date: datetime | None = None): + now = datetime.now() if current_date is None else current_date + last_day_of_month = calendar.monthrange(now.year, now.month)[1] + + self.today = now.replace(hour=0, minute=0, second=0, microsecond=0) + self.yesterday = self.today - timedelta(days=1) + self.first_date_of_month = self.today.replace(day=1) + self.last_date_of_month = self.today.replace(day=last_day_of_month) + + def to_dict(self): + obj = { + "today": self.today, + "today_iso": self.today_iso, + "yesterday": self.yesterday, + "yesterday_iso": self.yesterday_iso, + "first_date_of_month": self.first_date_of_month, + "first_date_of_month_iso": self.first_date_of_month_iso, + "last_date_of_month": self.last_date_of_month, + "last_date_of_month_iso": self.last_date_of_month_iso, + "current_month_iso": self.current_month_iso, + } + + return obj + + def __repr__(self): + to_string = json.dumps(self.to_dict(), default=str) + + return to_string diff --git a/custom_components/citymind_water_meter/models/meter_data.py b/custom_components/citymind_water_meter/models/meter_data.py index d1944de..aeb1b4e 100644 --- a/custom_components/citymind_water_meter/models/meter_data.py +++ b/custom_components/citymind_water_meter/models/meter_data.py @@ -72,6 +72,6 @@ def to_dict(self): return obj def __repr__(self): - to_string = json.dumps(self, default=str) + to_string = json.dumps(self.to_dict(), default=str) return to_string diff --git a/custom_components/citymind_water_meter/sensor.py b/custom_components/citymind_water_meter/sensor.py index af199f6..9022978 100644 --- a/custom_components/citymind_water_meter/sensor.py +++ b/custom_components/citymind_water_meter/sensor.py @@ -1,3 +1,4 @@ +from datetime import datetime import logging from homeassistant.components.sensor import SensorEntity @@ -7,7 +8,7 @@ from .common.base_entity import IntegrationBaseEntity, async_setup_base_entry from .common.entity_descriptions import IntegrationSensorEntityDescription -from .common.enums import EntityType +from .common.enums import EntityType, ResetPolicy from .managers.coordinator import Coordinator _LOGGER = logging.getLogger(__name__) @@ -43,6 +44,23 @@ def __init__( entity_description.native_unit_of_measurement ) + self._attr_last_reset = self._get_last_reset() + + def _get_last_reset(self) -> datetime | None: + last_reset: datetime | None = None + reset_policy = self._entity_description.reset_policy + + if reset_policy != ResetPolicy.NONE: + analytic_periods = self.coordinator.config_manager.analytic_periods + + if reset_policy == ResetPolicy.DAILY: + last_reset = analytic_periods.today + + elif reset_policy == ResetPolicy.MONTHLY: + last_reset = analytic_periods.first_date_of_month + + return last_reset + def update_component(self, data): """Fetch new state parameters for the sensor.""" if data is not None: diff --git a/tests/test.py b/tests/test.py index 3576f58..c33f23e 100644 --- a/tests/test.py +++ b/tests/test.py @@ -88,7 +88,12 @@ async def initialize(self): self._config_data = self._config_manager.config_data - self._api = RestAPI(None, self._config_data) + self._api = RestAPI( + None, + self._config_data, + self._config_manager.analytic_periods + ) + self._api.set_local_async_dispatcher_send(self.local_async_dispatcher_send) await self._api.initialize() @@ -120,15 +125,16 @@ async def update(self): for processor_type in self._processors: processor = self._processors[processor_type] - processor.update(self._api.data, self._api.today, self._api.yesterday) + processor.update(self._api.data) - account = self._account_processor.get().to_dict() - meters = self._meter_processor.get_all() + account = self._account_processor.get() + meters = self._meter_processor.get_meters() _LOGGER.info(json.dumps(self._api.data, indent=4, default=str)) _LOGGER.info(f"account: {account}") - for meter in meters: + for meter_id in meters: + meter = self._meter_processor.get_data(meter_id) _LOGGER.info(f"{meter}") def _get_api_data(self) -> str: @@ -152,7 +158,6 @@ def _get_api_data(self) -> str: return result - loop = asyncio.new_event_loop() instance = Test(loop)