Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dev-mypy = [
]
dev-noxfile = ["nox == 2025.11.12", "frequenz-repo-config[lib] == 0.13.8"]
dev-pylint = [
"pylint == 3.3.8",
"pylint == 4.0.4",
# For checking the noxfile, docs/ script, and tests
"frequenz-sdk[dev-mkdocs,dev-noxfile,dev-pytest]",
]
Expand Down
24 changes: 12 additions & 12 deletions src/frequenz/sdk/microgrid/_data_sourcing/microgrid_api_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

_logger = logging.getLogger(__name__)

_MeterDataMethods: dict[Metric | TransitionalMetric, Callable[[MeterData], float]] = {
_METER_DATA_METHODS: dict[Metric | TransitionalMetric, Callable[[MeterData], float]] = {
Metric.AC_ACTIVE_POWER: lambda msg: msg.active_power,
Metric.AC_ACTIVE_POWER_PHASE_1: lambda msg: msg.active_power_per_phase[0],
Metric.AC_ACTIVE_POWER_PHASE_2: lambda msg: msg.active_power_per_phase[1],
Expand All @@ -47,7 +47,7 @@
Metric.AC_REACTIVE_POWER_PHASE_3: lambda msg: msg.reactive_power_per_phase[2],
}

_BatteryDataMethods: dict[
_BATTERY_DATA_METHODS: dict[
Metric | TransitionalMetric, Callable[[BatteryData], float]
] = {
Metric.BATTERY_SOC_PCT: lambda msg: msg.soc,
Expand All @@ -69,7 +69,7 @@
Metric.BATTERY_TEMPERATURE: lambda msg: msg.temperature,
}

_InverterDataMethods: dict[
_INVERTER_DATA_METHODS: dict[
Metric | TransitionalMetric, Callable[[InverterData], float]
] = {
Metric.AC_ACTIVE_POWER: lambda msg: msg.active_power,
Expand Down Expand Up @@ -101,7 +101,7 @@
Metric.AC_REACTIVE_POWER_PHASE_3: lambda msg: msg.reactive_power_per_phase[2],
}

_EVChargerDataMethods: dict[
_EV_CHARGER_DATA_METHODS: dict[
Metric | TransitionalMetric, Callable[[EVChargerData], float]
] = {
Metric.AC_ACTIVE_POWER: lambda msg: msg.active_power,
Expand Down Expand Up @@ -191,7 +191,7 @@ async def _check_battery_request(
for the given battery.
"""
for metric in requests:
if metric not in _BatteryDataMethods:
if metric not in _BATTERY_DATA_METHODS:
err = f"Unknown metric {metric} for Battery id {comp_id}"
_logger.error(err)
raise ValueError(err)
Expand All @@ -216,7 +216,7 @@ async def _check_ev_charger_request(
for the given EV Charger.
"""
for metric in requests:
if metric not in _EVChargerDataMethods:
if metric not in _EV_CHARGER_DATA_METHODS:
err = f"Unknown metric {metric} for EvCharger id {comp_id}"
_logger.error(err)
raise ValueError(err)
Expand All @@ -241,7 +241,7 @@ async def _check_inverter_request(
for the given inverter.
"""
for metric in requests:
if metric not in _InverterDataMethods:
if metric not in _INVERTER_DATA_METHODS:
err = f"Unknown metric {metric} for Inverter id {comp_id}"
_logger.error(err)
raise ValueError(err)
Expand All @@ -266,7 +266,7 @@ async def _check_meter_request(
for the given meter.
"""
for metric in requests:
if metric not in _MeterDataMethods:
if metric not in _METER_DATA_METHODS:
err = f"Unknown metric {metric} for Meter id {comp_id}"
_logger.error(err)
raise ValueError(err)
Expand Down Expand Up @@ -326,13 +326,13 @@ def _get_data_extraction_method(
representing the given metric.
"""
if category == ComponentCategory.BATTERY:
return _BatteryDataMethods[metric]
return _BATTERY_DATA_METHODS[metric]
if category == ComponentCategory.INVERTER:
return _InverterDataMethods[metric]
return _INVERTER_DATA_METHODS[metric]
if category == ComponentCategory.METER:
return _MeterDataMethods[metric]
return _METER_DATA_METHODS[metric]
if category == ComponentCategory.EV_CHARGER:
return _EVChargerDataMethods[metric]
return _EV_CHARGER_DATA_METHODS[metric]
err = f"Unknown component category {category}"
_logger.error(err)
raise ValueError(err)
Expand Down
3 changes: 3 additions & 0 deletions src/frequenz/sdk/microgrid/_old_component_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class MeterData(ComponentData):

@override
@classmethod
# pylint: disable-next=too-many-branches
def from_samples(cls, samples: ComponentDataSamples) -> Self:
"""Create a new instance from a component data object."""
if not samples.metric_samples:
Expand Down Expand Up @@ -700,6 +701,7 @@ class InverterData(ComponentData): # pylint: disable=too-many-instance-attribut

@override
@classmethod
# pylint: disable-next=too-many-branches
def from_samples(cls, samples: ComponentDataSamples) -> Self:
"""Create a new instance from a component data object."""
if not samples.metric_samples:
Expand Down Expand Up @@ -962,6 +964,7 @@ class EVChargerData(ComponentData): # pylint: disable=too-many-instance-attribu

@override
@classmethod
# pylint: disable-next=too-many-branches
def from_samples(cls, samples: ComponentDataSamples) -> Self:
"""Create a new instance from a component data object."""
if not samples.metric_samples:
Expand Down
33 changes: 21 additions & 12 deletions src/frequenz/sdk/microgrid/_power_managing/_matryoshka.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,7 @@ def calculate_target_power(
if not self._validate_component_ids(component_ids, proposal, system_bounds):
return None

if proposal is not None:
bucket = self._component_buckets.setdefault(component_ids, set())
if proposal in bucket:
bucket.remove(proposal)
if (
proposal.preferred_power is not None
or proposal.bounds.lower is not None
or proposal.bounds.upper is not None
):
bucket.add(proposal)
elif not bucket:
del self._component_buckets[component_ids]
self._update_buckets(component_ids, proposal)

# If there has not been any proposal for the given components, don't calculate a
# target power and just return `None`.
Expand Down Expand Up @@ -223,6 +212,26 @@ def calculate_target_power(

return target_power

def _update_buckets(
self, component_ids: frozenset[ComponentId], proposal: Proposal | None
) -> None:
"""Update the component buckets with the given proposal."""
if proposal is None:
return

if proposal is not None:
bucket = self._component_buckets.setdefault(component_ids, set())
if proposal in bucket:
bucket.remove(proposal)
if (
proposal.preferred_power is not None
or proposal.bounds.lower is not None
or proposal.bounds.upper is not None
):
bucket.add(proposal)
elif not bucket:
del self._component_buckets[component_ids]

@override
def get_status(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from ..._internal._constants import MAX_BATTERY_DATA_AGE_SEC
from ...microgrid import connection_manager
from ...microgrid._data_sourcing.microgrid_api_source import (
_BatteryDataMethods,
_InverterDataMethods,
_BATTERY_DATA_METHODS,
_INVERTER_DATA_METHODS,
)
from ...microgrid._old_component_data import (
BatteryData,
Expand Down Expand Up @@ -212,13 +212,13 @@ async def async_new( # noqa: DOC502 (ValueError is raised indirectly super.asyn

@override
def _supported_metrics(self) -> set[Metric | TransitionalMetric]:
return set(_BatteryDataMethods.keys())
return set(_BATTERY_DATA_METHODS.keys())

@override
def _extract_metric(
self, data: BatteryData, metric: Metric | TransitionalMetric
) -> float:
return _BatteryDataMethods[metric](data)
return _BATTERY_DATA_METHODS[metric](data)

@override
def _subscribe(self) -> Receiver[BatteryData]:
Expand Down Expand Up @@ -270,13 +270,13 @@ async def async_new( # noqa: DOC502 (ValueError is raised indirectly by super.a

@override
def _supported_metrics(self) -> set[Metric | TransitionalMetric]:
return set(_InverterDataMethods.keys())
return set(_INVERTER_DATA_METHODS.keys())

@override
def _extract_metric(
self, data: InverterData, metric: Metric | TransitionalMetric
) -> float:
return _InverterDataMethods[metric](data)
return _INVERTER_DATA_METHODS[metric](data)

@override
def _subscribe(self) -> Receiver[InverterData]:
Expand Down
1 change: 1 addition & 0 deletions tests/utils/graph_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def _battery_with_inverter(
],
)

# pylint: disable-next=too-many-branches
def component(
self,
other: ComponentCategory | Component,
Expand Down
Loading