Error creating device profile via API

Hello. I’m working on a couple of python scripts to automate the device management via gRPC API. My local development instance is running the latest version of the “chirpstack/chirpstack:4” Docker image.

I’m getting a ValueError whenever I’m trying to create a device profile with measurements. If I’m passing an empty measurement dictionary to the Python API, everything works fine though.

This is my code:

import grpc
from chirpstack_api import api as chirpstack

SERVER = "localhost:8080"
API_TOKEN = "ey..."
TAGS = {
    "Foo": "Foo",
    "Bar": "Bar"
}
MEASUREMENTS = {
    "Foo": {
        "name": "Foo measurement",
        "kind": 4
    },
    "Bar": {
        "name": "Bar measurement",
        "kind": 4
    }
}

def main():
    # Connect without using TLS.
    channel = grpc.insecure_channel(SERVER)

    # Define the API key meta-data.
    auth_token = [("authorization", "Bearer %s" % API_TOKEN)]

    # Create device profile.
    try:
        client = chirpstack.DeviceProfileServiceStub(channel)
        req = chirpstack.CreateDeviceProfileRequest()
        req.device_profile.name = "Test Device Profile"
        req.device_profile.tenant_id = "52f14cd4-c6f1-4fbd-8f87-4025e1d49242"
        req.device_profile.description = "This is a test with tags and measurements"
        req.device_profile.region = 0
        req.device_profile.mac_version = 3
        req.device_profile.reg_params_revision = 0
        req.device_profile.adr_algorithm_id = "default"
        req.device_profile.payload_codec_runtime = 0
        req.device_profile.payload_codec_script = ""
        req.device_profile.flush_queue_on_activate = True
        req.device_profile.uplink_interval = 3600
        req.device_profile.device_status_req_interval = 1
        req.device_profile.supports_otaa = True
        req.device_profile.supports_class_b = False
        req.device_profile.supports_class_c = False
        req.device_profile.class_b_timeout = 0
        req.device_profile.class_c_timeout = 0
        req.device_profile.tags.update(TAGS)
        req.device_profile.measurements.update(MEASUREMENTS)
        req.device_profile.auto_detect_measurements = True
        resp = client.Create(req, metadata=auth_token)
        print(resp)
    except grpc.RpcError as err:
        print(err)

if __name__ == '__main__':
    main()

With an empty measurements dictionary it works fine, for example:

req.device_profile.measurements.update({})

But using the example above, I’m getting this error message:

Traceback (most recent call last):
  File "/Users/.../Projects/chirpstack-api-automation/createdevice-profile.py", line 58, in <module>
    main()
  File "/Users/.../Projects/chirpstack-api-automation/createdevice-profile.py", line 50, in main
    req.device_profile.measurements.update(MEASUREMENTS)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/_collections_abc.py", line 834, in update
    self[key] = other[key]
ValueError: Message objects may not be assigned

I suppose I need to build the Measurement object first, but I have no idea how to do that. Any help would be much appreciated.

This topic was automatically closed after 90 days. New replies are no longer allowed.