Skip to content

Commit

Permalink
fix: Fixed issue where old inactive meters were being registered (1 h…
Browse files Browse the repository at this point in the history
…our dev time)
  • Loading branch information
BottlecapDave committed Jun 22, 2024
1 parent e20489b commit 9ab2bf7
Showing 1 changed file with 59 additions and 48 deletions.
107 changes: 59 additions & 48 deletions custom_components/octopus_energy/api_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ async def async_get_account(self, account_id):
"""Get the user's account"""
await self.async_refresh_token()

local_now: datetime = now()
local_date = local_now.date()

try:
client = self._create_client_session()
url = f'{self._base_url}/v1/graphql/'
Expand All @@ -471,33 +474,37 @@ async def async_get_account(self, account_id):
else False,
"electricity_meter_points": list(map(lambda mp: {
"mpan": mp["meterPoint"]["mpan"],
"meters": list(map(lambda m: {
"active_from": parse_date(m["activeFrom"]) if m["activeFrom"] is not None else None,
"active_to": parse_date(m["activeTo"]) if m["activeTo"] is not None else None,
"serial_number": m["serialNumber"],
"is_export": m["smartExportElectricityMeter"] is not None,
"is_smart_meter": f'{m["meterType"]}'.startswith("S1") or f'{m["meterType"]}'.startswith("S2"),
"device_id": m["smartImportElectricityMeter"]["deviceId"] if m["smartImportElectricityMeter"] is not None else None,
"manufacturer": m["smartImportElectricityMeter"]["manufacturer"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["manufacturer"]
if m["smartExportElectricityMeter"] is not None
else m["makeAndType"],
"model": m["smartImportElectricityMeter"]["model"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["model"]
if m["smartExportElectricityMeter"] is not None
else None,
"firmware": m["smartImportElectricityMeter"]["firmwareVersion"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["firmwareVersion"]
if m["smartExportElectricityMeter"] is not None
else None
},
mp["meterPoint"]["meters"]
if "meterPoint" in mp and "meters" in mp["meterPoint"] and mp["meterPoint"]["meters"] is not None
else []
)),
"meters": list(filter(
lambda m: (m["active_from"] is None or m["active_from"] <= local_date) and (m["active_to"] is None or m["active_to"] >= local_date),
map(lambda m: {
"active_from": parse_date(m["activeFrom"]) if m["activeFrom"] is not None else None,
"active_to": parse_date(m["activeTo"]) if m["activeTo"] is not None else None,
"serial_number": m["serialNumber"],
"is_export": m["smartExportElectricityMeter"] is not None,
"is_smart_meter": f'{m["meterType"]}'.startswith("S1") or f'{m["meterType"]}'.startswith("S2"),
"device_id": m["smartImportElectricityMeter"]["deviceId"] if m["smartImportElectricityMeter"] is not None else None,
"manufacturer": m["smartImportElectricityMeter"]["manufacturer"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["manufacturer"]
if m["smartExportElectricityMeter"] is not None
else m["makeAndType"],
"model": m["smartImportElectricityMeter"]["model"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["model"]
if m["smartExportElectricityMeter"] is not None
else None,
"firmware": m["smartImportElectricityMeter"]["firmwareVersion"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["firmwareVersion"]
if m["smartExportElectricityMeter"] is not None
else None
},
mp["meterPoint"]["meters"]
if "meterPoint" in mp and "meters" in mp["meterPoint"] and mp["meterPoint"]["meters"] is not None
else []
)
)
),
"agreements": list(map(lambda a: {
"start": a["validFrom"],
"end": a["validTo"],
Expand All @@ -515,27 +522,31 @@ async def async_get_account(self, account_id):
)),
"gas_meter_points": list(map(lambda mp: {
"mprn": mp["meterPoint"]["mprn"],
"meters": list(map(lambda m: {
"active_from": parse_date(m["activeFrom"]) if m["activeFrom"] is not None else None,
"active_to": parse_date(m["activeTo"]) if m["activeTo"] is not None else None,
"serial_number": m["serialNumber"],
"consumption_units": m["consumptionUnits"],
"is_smart_meter": m["mechanism"] == "S1" or m["mechanism"] == "S2",
"device_id": m["smartGasMeter"]["deviceId"] if m["smartGasMeter"] is not None else None,
"manufacturer": m["smartGasMeter"]["manufacturer"]
if m["smartGasMeter"] is not None
else m["modelName"],
"model": m["smartGasMeter"]["model"]
if m["smartGasMeter"] is not None
else None,
"firmware": m["smartGasMeter"]["firmwareVersion"]
if m["smartGasMeter"] is not None
else None
},
mp["meterPoint"]["meters"]
if "meterPoint" in mp and "meters" in mp["meterPoint"] and mp["meterPoint"]["meters"] is not None
else []
)),
"meters": list(filter(
lambda m: (m["active_from"] is None or m["active_from"] <= local_date) and (m["active_to"] is None or m["active_to"] >= local_date),
map(lambda m: {
"active_from": parse_date(m["activeFrom"]) if m["activeFrom"] is not None else None,
"active_to": parse_date(m["activeTo"]) if m["activeTo"] is not None else None,
"serial_number": m["serialNumber"],
"consumption_units": m["consumptionUnits"],
"is_smart_meter": m["mechanism"] == "S1" or m["mechanism"] == "S2",
"device_id": m["smartGasMeter"]["deviceId"] if m["smartGasMeter"] is not None else None,
"manufacturer": m["smartGasMeter"]["manufacturer"]
if m["smartGasMeter"] is not None
else m["modelName"],
"model": m["smartGasMeter"]["model"]
if m["smartGasMeter"] is not None
else None,
"firmware": m["smartGasMeter"]["firmwareVersion"]
if m["smartGasMeter"] is not None
else None
},
mp["meterPoint"]["meters"]
if "meterPoint" in mp and "meters" in mp["meterPoint"] and mp["meterPoint"]["meters"] is not None
else []
)
)
),
"agreements": list(map(lambda a: {
"start": a["validFrom"],
"end": a["validTo"],
Expand Down

0 comments on commit 9ab2bf7

Please sign in to comment.