Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error in init client #16

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/enerbitdso/enerbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import ssl
import urllib
import urllib.parse

from typing import Optional

import httpx
import pydantic
import truststore
Expand All @@ -24,22 +25,22 @@ class ScheduleUsageRecord(pydantic.BaseModel):
meter_serial: str
time_start: dt.datetime
time_end: dt.datetime
active_energy_imported: float
active_energy_exported: float
reactive_energy_imported: float
reactive_energy_exported: float
active_energy_imported: Optional[float] = None
active_energy_exported: Optional[float] = None
reactive_energy_imported: Optional[float] = None
reactive_energy_exported: Optional[float] = None


class ScheduleMeasurementRecord(pydantic.BaseModel):
frt_code: str
meter_serial: str
time_local_utc: dt.datetime
voltage_multiplier: float
current_multiplier: float
active_energy_imported: float
active_energy_exported: float
reactive_energy_imported: float
reactive_energy_exported: float
voltage_multiplier: Optional[float] = None
current_multiplier: Optional[float] = None
active_energy_imported: Optional[float] = None
active_energy_exported: Optional[float] = None
reactive_energy_imported: Optional[float] = None
reactive_energy_exported: Optional[float] = None


def get_auth_token(base_url: str, username: str, password: pydantic.SecretStr) -> str:
Expand Down Expand Up @@ -69,19 +70,19 @@ def get_client(

def scale_measurement_records(records: list[ScheduleMeasurementRecord], scale: float):
for r in records:
r.active_energy_imported = r.active_energy_imported * scale
r.active_energy_exported = r.active_energy_exported * scale
r.reactive_energy_imported = r.reactive_energy_imported * scale
r.reactive_energy_exported = r.reactive_energy_exported * scale
r.active_energy_imported = r.active_energy_imported * scale if r.active_energy_imported is not None else None
r.active_energy_exported = r.active_energy_exported * scale if r.active_energy_exported is not None else None
r.reactive_energy_imported = r.reactive_energy_imported * scale if r.reactive_energy_imported is not None else None
r.reactive_energy_exported = r.reactive_energy_exported * scale if r.reactive_energy_exported is not None else None
return records


def scale_usage_records(records: list[ScheduleUsageRecord], scale: float):
for r in records:
r.active_energy_imported = r.active_energy_imported * scale
r.active_energy_exported = r.active_energy_exported * scale
r.reactive_energy_imported = r.reactive_energy_imported * scale
r.reactive_energy_exported = r.reactive_energy_exported * scale
r.active_energy_imported = r.active_energy_imported * scale if r.active_energy_imported is not None else None
r.active_energy_exported = r.active_energy_exported * scale if r.active_energy_exported is not None else None
r.reactive_energy_imported = r.reactive_energy_imported * scale if r.reactive_energy_imported is not None else None
r.reactive_energy_exported = r.reactive_energy_exported * scale if r.reactive_energy_exported is not None else None
return records


Expand All @@ -106,7 +107,6 @@ def get_schedule_usage_records(
logger.error(f"Failed to fetch usage records: {e}")
logger.error(f"Response: {response.text}")
raise
response.raise_for_status()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ingcristianamaya I think this line should be kept, the null value fix you did is not the only possible error that can occur there, and since we are not dealing with the possible error we should raise it to the caller.

records = response.json()
records = sorted(records, key=lambda r: r["time_start"])
usage_records = [ScheduleUsageRecord.model_validate(r) for r in records]
Expand Down
Loading