flexmeasures.data.schemas.generic_assets

Functions

flexmeasures.data.schemas.generic_assets.extract_sensors_from_flex_config(plot: dict) list[Sensor]

Extracts a consolidated list of sensors from an asset based on flex-context or flex-model definitions provided in a plot dictionary.

Classes

class flexmeasures.data.schemas.generic_assets.GenericAssetIdField(status_if_not_found: HTTPStatus | None = None, *args, **kwargs)

Field that deserializes to a GenericAsset and serializes back to an integer.

__init__(status_if_not_found: HTTPStatus | None = None, *args, **kwargs)
_deserialize(value: Any, attr, data, **kwargs) GenericAsset

Turn a generic asset id into a GenericAsset.

_serialize(value: GenericAsset, attr, obj, **kwargs) int

Turn a GenericAsset into a generic asset id.

class flexmeasures.data.schemas.generic_assets.GenericAssetSchema(*args, **kwargs)

GenericAsset schema, with validations.

class Meta
model

alias of GenericAsset

opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
validate_attributes(attributes: dict, **kwargs)

Validate sensors_to_show if sent within attributes. Deprecated, as this is now its own field on the model. Can be deleted once we stop supporting storing them under here.

validate_name_is_unique_under_parent(data, **kwargs)

Validate that name is unique among siblings. This is also checked by a db constraint. Here, we can only check if we have all information (a full form), which usually is at creation time.

class flexmeasures.data.schemas.generic_assets.GenericAssetTypeSchema(*args, **kwargs)

GenericAssetType schema, with validations.

class Meta
model

alias of GenericAssetType

opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
class flexmeasures.data.schemas.generic_assets.SensorKPIFieldSchema(*args, **kwargs)
opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
class flexmeasures.data.schemas.generic_assets.SensorsToShowAsKPIsSchema(*args, **kwargs)
opts: LoadInstanceMixin.Opts = <flask_marshmallow.sqla.SQLAlchemySchemaOpts object>
class flexmeasures.data.schemas.generic_assets.SensorsToShowSchema(*, load_default: ~typing.Any = <marshmallow.missing>, missing: ~typing.Any = <marshmallow.missing>, dump_default: ~typing.Any = <marshmallow.missing>, default: ~typing.Any = <marshmallow.missing>, data_key: str | None = None, attribute: str | None = None, validate: ~typing.Callable[[~typing.Any], ~typing.Any] | ~typing.Iterable[~typing.Callable[[~typing.Any], ~typing.Any]] | None = None, required: bool = False, allow_none: bool | None = None, load_only: bool = False, dump_only: bool = False, error_messages: dict[str, str] | None = None, metadata: ~typing.Mapping[str, ~typing.Any] | None = None, **additional_metadata)

Schema for validating and deserializing the sensors_to_show attribute of a GenericAsset.

The sensors_to_show attribute defines which sensors should be displayed for a particular asset. It supports various input formats, which are standardized into a list of dictionaries, each containing a title (optional) and a plots list, this list then consist of dictionaries with keys such as sensor, asset or sensors.

  • A single sensor ID (int): 42 -> {“title”: None, “plots”: [{“sensor”: 42}]}

  • A list of sensor IDs (list of ints): [42, 43] -> {“title”: None, “plots”: [{“sensors”: [42, 43]}]}

  • A dictionary with a title and sensor: {“title”: “Temperature”, “sensor”: 42} -> {“title”: “Temperature”, “plots”: [{“sensor”: 42}]}

  • A dictionary with a title and sensors: {“title”: “Pressure”, “sensors”: [42, 43]} -> {“title”: “Pressure”, “plots”: [{“sensors”: [42, 43]}]}

Validation ensures that: - The input is either a list, integer, or dictionary. - If the input is a dictionary, it must contain either sensor (int), sensors (list of ints) or plots (list of dicts). - All sensor IDs must be valid integers.

Example Inputs: - [{“title”: “Test”, “plots”: [{“sensor”: 1}, {“sensor”: 2}]}, {“title”: “Another Test”, “plots”: [{“sensors”: [3, 4]}]}, 5] - [{“title”: “Test”, “sensors”: [1, 2]}, {“title”: None, “sensors”: [3, 4]}, 5] (Older format but still compatible)

Example Output (Standardized): - [{“title”: “Test”, “plots”: [{“sensors”: [1, 2]}]}, {“title”: None, “plots”: [{“sensors”: [3, 4]}]}, {“title”: None, “plots”: [{“sensor”: 5}]}]

_standardize_dict_item(item: dict) dict

Transform a dictionary-based sensor configuration into a standardized ‘plots’ structure. Ensures ‘title’ is a string and processes ‘sensor’, ‘sensors’, or direct ‘plots’ keys.

_standardize_item(item) dict

Normalize various input formats (int, list, or dict) into a standard plot dictionary.

_validate_asset_in_plot(plot)

Validate plots that reference a GenericAsset. Ensures flex-config schemas are respected when an asset is provided.

_validate_flex_config_field_is_valid_choice(plot_config, field_name, valid_collection)

Verify that the chosen flex-config field exists on the specific asset and matches allowed schema keys.

_validate_single_plot(plot)

Perform structural validation on an individual plot dictionary. Requires at least one of: ‘sensor’, ‘sensors’, or ‘asset’.

deserialize(value, **kwargs) list

Validate and deserialize the input value.

classmethod flatten(nested_list: list) list[int] | list[Sensor]

Flatten a nested list of sensor IDs into a unique list. Also works for Sensor objects.

This method processes the following formats for each entry in the list: 1. A single sensor ID:

3

  1. A list of sensor IDs: [1, 2]

  2. A dictionary with a sensor key: {“sensor”: 3}

  3. A dictionary with a sensors key: {“sensors”: [1, 2]}

  4. A dictionary with a plots key, containing a list of dictionaries, each with a sensor or sensors key: {“plots”: [{“sensor”: 4}, {“sensors”: [5, 6]}]}

  5. A dictionary under the plots key containing the asset key together with a flex-model or flex-context key, containing a field name or a list of field names:

    {“plots”: [{“asset”: 100, “flex-model”: [“consumption-capacity”, “production-capacity”], “flex-context”: “site-power-capacity”}}

  6. Mixed formats: [{“title”: “Temperature”, “sensors”: [1, 2]}, {“title”: “Pressure”, “sensor”: 3}, {“title”: “Pressure”, “plots”: [{“sensor”: 4}, {“sensors”: [5, 6]}]}]

Example: >>> SensorsToShowSchema.flatten([1, [2, 20, 6], 10, [6, 2], {“title”: None,”sensors”: [10, 15]}, 15, {“plots”: [{“sensor”: 1}, {“sensors”: [20, 8]}]}]) [1, 2, 20, 6, 10, 15, 8]

Parameters:

nested_list – A list containing sensor IDs, or dictionaries with sensors or sensor keys.

Returns:

A unique list of sensor IDs, or a unique list of Sensors