Skip to content

Commit

Permalink
Backport PR #176: Fig bug: broken (dev) CLI command for adding a Sens…
Browse files Browse the repository at this point in the history
…or (#176)

Accept either a generic_asset or a generic_asset_id when initializing a Sensor.

* Create draft PR for #175

* Allow passing a GenericAsset or an id

* Changelog entry

Co-authored-by: Flix6x <Flix6x@users.noreply.github.com>
Co-authored-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
3 people committed Sep 7, 2021
1 parent eeb301a commit 8117cc1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -24,6 +24,7 @@ New features
Bugfixes
-----------
* Fix (dev) CLI command for adding a GenericAssetType [see `PR #173 <http://www.github.com/SeitaBV/flexmeasures/pull/173>`_]
* Fix (dev) CLI command for adding a Sensor [see `PR #176 <http://www.github.com/SeitaBV/flexmeasures/pull/176>`_]

Infrastructure / Support
----------------------
Expand Down
16 changes: 14 additions & 2 deletions flexmeasures/data/models/time_series.py
Expand Up @@ -39,10 +39,22 @@ class Sensor(db.Model, tb.SensorDBMixin):
),
)

def __init__(self, name: str, generic_asset: GenericAsset, **kwargs):
def __init__(
self,
name: str,
generic_asset: Optional[GenericAsset] = None,
generic_asset_id: Optional[int] = None,
**kwargs,
):
assert (generic_asset is None) ^ (
generic_asset_id is None
), "Either generic_asset_id or generic_asset must be set."
tb.SensorDBMixin.__init__(self, name, **kwargs)
tb_utils.remove_class_init_kwargs(tb.SensorDBMixin, kwargs)
kwargs["generic_asset"] = generic_asset
if generic_asset is not None:
kwargs["generic_asset"] = generic_asset
else:
kwargs["generic_asset_id"] = generic_asset_id
db.Model.__init__(self, **kwargs)

@property
Expand Down

0 comments on commit 8117cc1

Please sign in to comment.