Skip to content

Commit dbfd5ef

Browse files
authored
fix: Avoid Estimation for Private Producers (#142)
* fix: Avoid Estimation for Private Producers * fix * Fix * Update manifest and default behavior
1 parent 85a7c5e commit dbfd5ef

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

custom_components/iec/coordinator.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ async def _async_update_data(
285285
daily_readings: dict[str, list[RemoteReading] | None] | None = {}
286286

287287
is_smart_meter = contracts.get(contract_id).smart_meter
288+
is_private_producer = contracts.get(contract_id).from_private_producer
288289
attributes_to_add = {CONTRACT_ID_ATTR_NAME: str(contract_id),
289290
IS_SMART_METER_ATTR_NAME: is_smart_meter,
290291
METER_ID_ATTR_NAME: None}
@@ -373,33 +374,34 @@ async def _async_update_data(
373374
else:
374375
_LOGGER.debug("Failed fetching FutureConsumption, data in IEC API is corrupted")
375376

376-
devices_by_id: Devices = await self._get_devices_by_device_id(device.device_number)
377-
last_meter_read = int(devices_by_id.counter_devices[0].last_mr)
378-
last_meter_read_date = devices_by_id.counter_devices[0].last_mr_date
379-
phase_count = devices_by_id.counter_devices[0].connection_size.phase
380-
connection_size = (devices_by_id.counter_devices[0].
381-
connection_size.representative_connection_size)
382-
383-
distribution_tariff = await self._get_distribution_tariff(phase_count)
384-
delivery_tariff = await self._get_delivery_tariff(phase_count)
385-
power_size = await self._get_power_size(connection_size)
386-
387-
estimated_bill, fixed_price, consumption_price, total_days, delivery_price, distribution_price, \
388-
total_kva_price, estimated_kwh_consumption = (
389-
self._calculate_estimated_bill(device.device_number, future_consumption,
390-
last_meter_read, last_meter_read_date,
391-
kwh_tariff, kva_tariff, distribution_tariff,
392-
delivery_tariff, power_size, last_invoice))
393-
394-
estimated_bill_dict = {
395-
TOTAL_EST_BILL_ATTR_NAME: estimated_bill,
396-
EST_BILL_DAYS_ATTR_NAME: total_days,
397-
EST_BILL_CONSUMPTION_PRICE_ATTR_NAME: consumption_price,
398-
EST_BILL_DELIVERY_PRICE_ATTR_NAME: delivery_price,
399-
EST_BILL_DISTRIBUTION_PRICE_ATTR_NAME: distribution_price,
400-
EST_BILL_TOTAL_KVA_PRICE_ATTR_NAME: total_kva_price,
401-
EST_BILL_KWH_CONSUMPTION_ATTR_NAME: estimated_kwh_consumption
402-
}
377+
if not is_private_producer:
378+
devices_by_id: Devices = await self._get_devices_by_device_id(device.device_number)
379+
last_meter_read = int(devices_by_id.counter_devices[0].last_mr)
380+
last_meter_read_date = devices_by_id.counter_devices[0].last_mr_date
381+
phase_count = devices_by_id.counter_devices[0].connection_size.phase
382+
connection_size = (devices_by_id.counter_devices[0].
383+
connection_size.representative_connection_size)
384+
385+
distribution_tariff = await self._get_distribution_tariff(phase_count)
386+
delivery_tariff = await self._get_delivery_tariff(phase_count)
387+
power_size = await self._get_power_size(connection_size)
388+
389+
estimated_bill, fixed_price, consumption_price, total_days, delivery_price, distribution_price, \
390+
total_kva_price, estimated_kwh_consumption = (
391+
self._calculate_estimated_bill(device.device_number, future_consumption,
392+
last_meter_read, last_meter_read_date,
393+
kwh_tariff, kva_tariff, distribution_tariff,
394+
delivery_tariff, power_size, last_invoice))
395+
396+
estimated_bill_dict = {
397+
TOTAL_EST_BILL_ATTR_NAME: estimated_bill,
398+
EST_BILL_DAYS_ATTR_NAME: total_days,
399+
EST_BILL_CONSUMPTION_PRICE_ATTR_NAME: consumption_price,
400+
EST_BILL_DELIVERY_PRICE_ATTR_NAME: delivery_price,
401+
EST_BILL_DISTRIBUTION_PRICE_ATTR_NAME: distribution_price,
402+
EST_BILL_TOTAL_KVA_PRICE_ATTR_NAME: total_kva_price,
403+
EST_BILL_KWH_CONSUMPTION_ATTR_NAME: estimated_kwh_consumption
404+
}
403405

404406
data[str(contract_id)] = {CONTRACT_DICT_NAME: contracts.get(contract_id),
405407
INVOICE_DICT_NAME: last_invoice,

custom_components/iec/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"issue_tracker": "https://github.com/guykh/iec-custom-component/issues",
1212
"loggers": ["iec_api"],
1313
"requirements": ["iec-api==0.3.1"],
14-
"version": "0.0.29"
14+
"version": "0.0.30"
1515
}

custom_components/iec/sensor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ def _get_reading_by_date(readings: list[RemoteReading] | None, desired_date: dat
9696
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
9797
# state_class=SensorStateClass.TOTAL,
9898
suggested_display_precision=3,
99-
value_fn=lambda data:
100-
(data[ESTIMATED_BILL_DICT_NAME][EST_BILL_KWH_CONSUMPTION_ATTR_NAME] or 0)
99+
value_fn=lambda data: (data[ESTIMATED_BILL_DICT_NAME][EST_BILL_KWH_CONSUMPTION_ATTR_NAME] if data[ESTIMATED_BILL_DICT_NAME] else 0)
101100
if (data[ESTIMATED_BILL_DICT_NAME]
102101
and data[ESTIMATED_BILL_DICT_NAME][EST_BILL_KWH_CONSUMPTION_ATTR_NAME]) else None
103102
),
@@ -108,7 +107,7 @@ def _get_reading_by_date(readings: list[RemoteReading] | None, desired_date: dat
108107
# state_class=SensorStateClass.TOTAL,
109108
suggested_display_precision=2,
110109
# The API doesn't provide future *cost* so we can try to estimate it by the previous consumption
111-
value_fn=lambda data: (data[ESTIMATED_BILL_DICT_NAME][TOTAL_EST_BILL_ATTR_NAME] or 0),
110+
value_fn=lambda data: (data[ESTIMATED_BILL_DICT_NAME][TOTAL_EST_BILL_ATTR_NAME]) if data[ESTIMATED_BILL_DICT_NAME] else 0,
112111
custom_attrs_fn=lambda data: {
113112
EST_BILL_DAYS_ATTR_NAME: data[ESTIMATED_BILL_DICT_NAME][EST_BILL_DAYS_ATTR_NAME],
114113
EST_BILL_CONSUMPTION_PRICE_ATTR_NAME: data[ESTIMATED_BILL_DICT_NAME][EST_BILL_CONSUMPTION_PRICE_ATTR_NAME],
@@ -304,9 +303,11 @@ def __init__(
304303
attributes.update(attributes_to_add)
305304

306305
if self.entity_description.custom_attrs_fn:
307-
attributes.update(self.entity_description.custom_attrs_fn(
306+
custom_attr = self.entity_description.custom_attrs_fn(
308307
self.coordinator.data.get(str(int(self.contract_id)))
309-
))
308+
)
309+
if custom_attr:
310+
attributes.update(custom_attr)
310311

311312
if is_multi_contract:
312313
attributes["is_multi_contract"] = is_multi_contract

0 commit comments

Comments
 (0)