Skip to content

Commit e1722a9

Browse files
authored
fix: multiple sensors created (#34)
1 parent 5e0fba0 commit e1722a9

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

custom_components/iec/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
DAILY_READINGS_DICT_NAME = "daily_readings"
1313
FUTURE_CONSUMPTIONS_DICT_NAME = "future_consumption"
1414
STATIC_KWH_TARIFF = "kwh_tariff"
15+
STATIC_CONTRACT = "contract_number"
16+
STATIC_BP_NUMBER = "bp_number"

custom_components/iec/coordinator.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
from iec_api.models.remote_reading import ReadingResolution, RemoteReading, FutureConsumptionInfo
2828

2929
from .const import DOMAIN, CONF_USER_ID, STATICS_DICT_NAME, STATIC_KWH_TARIFF, INVOICE_DICT_NAME, \
30-
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME
30+
FUTURE_CONSUMPTIONS_DICT_NAME, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT, STATIC_BP_NUMBER
3131

3232
_LOGGER = logging.getLogger(__name__)
3333
TIMEZONE = pytz.timezone("Asia/Jerusalem")
3434

3535

36-
3736
class IecApiCoordinator(DataUpdateCoordinator[dict[int, dict]]):
3837
"""Handle fetching IEC data, updating sensors and inserting statistics."""
3938

@@ -139,9 +138,11 @@ async def _async_update_data(
139138
# Sort by Date
140139
daily_readings.sort(key=lambda x: x.date)
141140

142-
143-
static_data = {}
144-
static_data[STATIC_KWH_TARIFF] = await self.api.get_kwh_tariff()
141+
static_data = {
142+
STATIC_KWH_TARIFF: await self.api.get_kwh_tariff(),
143+
STATIC_CONTRACT: self._contract_id,
144+
STATIC_BP_NUMBER: self._bp_number
145+
}
145146

146147
data = {STATICS_DICT_NAME: static_data, INVOICE_DICT_NAME: last_invoice,
147148
FUTURE_CONSUMPTIONS_DICT_NAME: future_consumption,

custom_components/iec/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"iot_class": "cloud_polling",
1111
"issue_tracker": "https://github.com/ludeeus/iec-custom-component/issues",
1212
"requirements": ["iec-api==0.2.7"],
13-
"version": "0.0.11"
13+
"version": "0.0.12"
1414
}

custom_components/iec/sensor.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from iec_api.models.invoice import Invoice
2222

2323
from .const import DOMAIN, ILS, STATICS_DICT_NAME, STATIC_KWH_TARIFF, FUTURE_CONSUMPTIONS_DICT_NAME, INVOICE_DICT_NAME, \
24-
ILS_PER_KWH, DAILY_READINGS_DICT_NAME
24+
ILS_PER_KWH, DAILY_READINGS_DICT_NAME, STATIC_CONTRACT
2525
from .coordinator import IecApiCoordinator
2626

2727
_LOGGER = logging.getLogger(__name__)
@@ -69,7 +69,7 @@ def get_previous_bill_kwh_price(invoice: Invoice) -> float:
6969
# state_class=SensorStateClass.TOTAL,
7070
suggested_display_precision=2,
7171
# The API doesn't provide future *cost* so we can try to estimate it by the previous consumption
72-
value_fn=lambda data: data[FUTURE_CONSUMPTIONS_DICT_NAME].future_consumption * get_previous_bill_kwh_price(data[INVOICE_DICT_NAME])
72+
value_fn=lambda data: data[FUTURE_CONSUMPTIONS_DICT_NAME].future_consumption * data[STATICS_DICT_NAME][STATIC_KWH_TARIFF]
7373
),
7474
IecEntityDescription(
7575
key="elec_today_consumption",
@@ -167,22 +167,22 @@ async def async_setup_entry(
167167

168168
coordinator: IecApiCoordinator = hass.data[DOMAIN][entry.entry_id]
169169
entities: list[SensorEntity] = []
170-
contracts = coordinator.data.keys()
171-
for contract_id in contracts:
172-
if coordinator.is_smart_meter:
173-
sensors_desc: tuple[IecEntityDescription, ...] = ELEC_SENSORS + SMART_ELEC_SENSORS
174-
else:
175-
sensors_desc: tuple[IecEntityDescription, ...] = ELEC_SENSORS
176-
# sensors_desc: tuple[IecEntityDescription, ...] = ELEC_SENSORS
177-
178-
for sensor_desc in sensors_desc:
179-
entities.append(
180-
IecSensor(
181-
coordinator,
182-
sensor_desc,
183-
contract_id
184-
)
170+
171+
if coordinator.is_smart_meter:
172+
sensors_desc: tuple[IecEntityDescription, ...] = ELEC_SENSORS + SMART_ELEC_SENSORS
173+
else:
174+
sensors_desc: tuple[IecEntityDescription, ...] = ELEC_SENSORS
175+
# sensors_desc: tuple[IecEntityDescription, ...] = ELEC_SENSORS
176+
177+
contract_id = coordinator.data[STATICS_DICT_NAME][STATIC_CONTRACT]
178+
for sensor_desc in sensors_desc:
179+
entities.append(
180+
IecSensor(
181+
coordinator,
182+
sensor_desc,
183+
contract_id
185184
)
185+
)
186186

187187
for sensor_desc in STATIC_SENSORS:
188188
entities.append(

0 commit comments

Comments
 (0)