flexmeasures.data.schemas.generic_assets
Functions
- flexmeasures.data.schemas.generic_assets.extract_sensors_from_flex_config(plot: dict) tuple[list[Sensor], list[dict]]
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 in its asset context. For child assets, names are unique among siblings. For account-owned root assets, names are unique within the account. For public root assets, names are unique globally. This is also checked by db indexes. Here, we can only check if we have all information (a full form), which usually is at creation time or when the existing asset is in context.
- validate_parent_asset(parent_asset_id: int | None, **kwargs)
Validate the parent_asset_id.
Ensures the referenced parent GenericAsset exists. When editing an existing asset (available in self.context.get(“asset”)), also ensures the parent belongs to the same Account as the asset being edited.
Parameters
- parent_asset_idint | None
ID of the parent GenericAsset, or None.
Raises
- ValidationError
If the parent asset does not exist, or if the parent belongs to a different account than the current asset.
- 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(*args, allow_missing_asset_flex_field: bool = False, **kwargs)
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]}]}
A dictionary with reference to a flex-model field, e.g.: {“asset”:10,”flex-model”: “soc-min”} (flex-context fields also work)
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’.
- 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
A list of sensor IDs: [1, 2]
A dictionary with a sensor key: {“sensor”: 3}
A dictionary with a sensors key: {“sensors”: [1, 2]}
A dictionary with a plots key, containing a list of dictionaries, each with a sensor or sensors key: {“plots”: [{“sensor”: 4}, {“sensors”: [5, 6]}]}
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”}}
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