Skip to content

Commit

Permalink
fix: get last reading from monthly report (#43)
Browse files Browse the repository at this point in the history
* fix: get last reading from monthly report

* fix: instead of jumping the next day, try to add 4 hrs to counter

* fix: lint

* fix: remove `today_reading`

* fix: logs to debug

* fix: some debug logs

* chore: update manifest
  • Loading branch information
GuyKh authored Mar 6, 2024
1 parent ade900a commit 0cb8a28
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 0 additions & 1 deletion custom_components/iec/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
INVOICE_DICT_NAME = "invoice"
DAILY_READINGS_DICT_NAME = "daily_readings"
FUTURE_CONSUMPTIONS_DICT_NAME = "future_consumption"
TODAY_READING_DICT_NAME = "today_reading"
STATIC_KWH_TARIFF = "kwh_tariff"
STATIC_CONTRACT = "contract_number"
STATIC_BP_NUMBER = "bp_number"
20 changes: 13 additions & 7 deletions custom_components/iec/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from .commons import find_reading_by_date
from .const import DOMAIN, CONF_USER_ID, STATICS_DICT_NAME, STATIC_KWH_TARIFF, INVOICE_DICT_NAME, \
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, STATIC_BP_NUMBER, TODAY_READING_DICT_NAME
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, STATIC_BP_NUMBER

_LOGGER = logging.getLogger(__name__)
TIMEZONE = pytz.timezone("Asia/Jerusalem")
Expand Down Expand Up @@ -184,8 +184,7 @@ async def _async_update_data(

data = {STATICS_DICT_NAME: static_data, INVOICE_DICT_NAME: last_invoice,
FUTURE_CONSUMPTIONS_DICT_NAME: future_consumption,
DAILY_READINGS_DICT_NAME: daily_readings,
TODAY_READING_DICT_NAME: today_reading}
DAILY_READINGS_DICT_NAME: daily_readings}

# Clean today reading for next reading cycle
self._today_reading = None
Expand All @@ -197,7 +196,7 @@ async def _insert_statistics(self) -> None:
# Support only smart meters at the moment
return

_LOGGER.info(f"Updating statistics for IEC Contract {self._contract_id}")
_LOGGER.debug(f"Updating statistics for IEC Contract {self._contract_id}")
devices = await self.api.get_devices(self._contract_id)
month_ago_time = (datetime.now() - timedelta(weeks=4))

Expand All @@ -219,10 +218,17 @@ async def _insert_statistics(self) -> None:
self._contract_id)
else:
last_stat_time = last_stat[consumption_statistic_id][0]["start"]
# API returns daily data, so need to increase the start date by 1 day to get the next day
from_date = datetime.fromtimestamp(last_stat_time) + timedelta(days=1)
# API returns daily data, so need to increase the start date by 4 hrs to get the next day
from_date = datetime.fromtimestamp(last_stat_time)
_LOGGER.debug(f"Last statistics are from {from_date.strftime('%Y-%m-%d %H:%M:%S')}")

if from_date.hour == 23:
from_date = from_date + timedelta(hours=2)

_LOGGER.debug(f"Calculated from_date = {from_date.strftime('%Y-%m-%d %H:%M:%S')}")
if (datetime.today() - from_date).days <= 0:
from_date = TIMEZONE.localize(datetime.today())
_LOGGER.debug("The date to fetch is today or later, replacing it with Today at 01:00:00")
from_date = TIMEZONE.localize(datetime.today().replace(hour=1, minute=0, second=0, microsecond=0))

_LOGGER.debug(f"Fetching consumption from {from_date.strftime('%Y-%m-%d %H:%M:%S')}")
readings = await self.api.get_remote_reading(device.device_number, int(device.device_code),
Expand Down
2 changes: 1 addition & 1 deletion custom_components/iec/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/ludeeus/iec-custom-component/issues",
"requirements": ["iec-api==0.2.8"],
"version": "0.0.14"
"version": "0.0.15"
}
4 changes: 2 additions & 2 deletions custom_components/iec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from .commons import find_reading_by_date
from .const import DOMAIN, ILS, STATICS_DICT_NAME, STATIC_KWH_TARIFF, FUTURE_CONSUMPTIONS_DICT_NAME, INVOICE_DICT_NAME, \
ILS_PER_KWH, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, EMPTY_REMOTE_READING, TODAY_READING_DICT_NAME
ILS_PER_KWH, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, EMPTY_REMOTE_READING
from .coordinator import IecApiCoordinator

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -122,7 +122,7 @@ def _get_reading_by_date(readings: list[RemoteReading] | None, desired_date: dat
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
suggested_display_precision=3,
value_fn=lambda data: data[TODAY_READING_DICT_NAME].future_consumption_info.total_import
value_fn=lambda data: data[FUTURE_CONSUMPTIONS_DICT_NAME].total_import
),
)

Expand Down

0 comments on commit 0cb8a28

Please sign in to comment.