Skip to content
Merged
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
22 changes: 22 additions & 0 deletions flexmeasures/cli/data_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import timedelta
from typing import Dict, List, Optional
import json

import pandas as pd
import pytz
Expand Down Expand Up @@ -169,12 +170,33 @@ def new_user(
type=int,
help="Generic asset to assign this sensor to",
)
@click.option(
"--attributes",
required=False,
type=str,
default="{}",
help='Additional attributes. Passed as JSON string, should be a dict. Hint: Currently, for sensors that measures power, use {"capacity_in_mw": 10} to set a capacity of 10 MW',
)
def add_sensor(**args):
"""Add a sensor."""
check_timezone(args["timezone"])
try:
attributes = json.loads(args["attributes"])
except json.decoder.JSONDecodeError as jde:
print(f"Error decoding --attributes. Please check your JSON: {jde}")
raise click.Abort()
del args["attributes"] # not part of schema
check_errors(SensorSchema().validate(args))
args["event_resolution"] = timedelta(minutes=args["event_resolution"])
sensor = Sensor(**args)
if not isinstance(attributes, dict):
print("Attributes should be a dict.")
raise click.Abort()
sensor.attributes = attributes
if sensor.measures_power:
if "capacity_in_mw" not in sensor.attributes:
print("A sensor which measures power needs a capacity (see --attributes).")
raise click.Abort
db.session.add(sensor)
db.session.commit()
print(f"Successfully created sensor with ID {sensor.id}")
Expand Down