Skip to content

Commit 2e84232

Browse files
committed
http: api: devices: add assign blueprint
1 parent fb28bac commit 2e84232

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import argparse
2+
import json
3+
4+
from enapter import cli, http
5+
6+
7+
class DeviceAssignBlueprintCommand(cli.Command):
8+
9+
@staticmethod
10+
def register(parent: cli.Subparsers) -> None:
11+
parser = parent.add_parser(
12+
"assign-blueprint", formatter_class=argparse.ArgumentDefaultsHelpFormatter
13+
)
14+
parser.add_argument("id", type=str, help="ID or slug of the device to update")
15+
parser.add_argument(
16+
"-b",
17+
"--blueprint-id",
18+
help="Blueprint ID to assign to the device",
19+
required=True,
20+
)
21+
22+
@staticmethod
23+
async def run(args: argparse.Namespace) -> None:
24+
async with http.api.Client(http.api.Config.from_env()) as client:
25+
device = await client.devices.assign_blueprint(
26+
device_id=args.id, blueprint_id=args.blueprint_id
27+
)
28+
print(json.dumps(device.to_dto()))

src/enapter/cli/http/api/device_command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from enapter import cli
44

5+
from .device_assign_blueprint_command import DeviceAssignBlueprintCommand
56
from .device_create_standalone_command import DeviceCreateStandaloneCommand
67
from .device_delete_command import DeviceDeleteCommand
78
from .device_generate_communication_config_command import (
@@ -23,6 +24,7 @@ def register(parent: cli.Subparsers) -> None:
2324
dest="http_api_device_command", required=True
2425
)
2526
for command in [
27+
DeviceAssignBlueprintCommand,
2628
DeviceCreateStandaloneCommand,
2729
DeviceDeleteCommand,
2830
DeviceGenerateCommunicationConfigCommand,
@@ -35,6 +37,8 @@ def register(parent: cli.Subparsers) -> None:
3537
@staticmethod
3638
async def run(args: argparse.Namespace) -> None:
3739
match args.http_api_device_command:
40+
case "assign-blueprint":
41+
await DeviceAssignBlueprintCommand.run(args)
3842
case "create-standalone":
3943
await DeviceCreateStandaloneCommand.run(args)
4044
case "delete":

src/enapter/http/api/devices/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ async def delete(self, device_id: str) -> None:
5959
response = await self._client.delete(url)
6060
api.check_error(response)
6161

62+
async def assign_blueprint(self, device_id: str, blueprint_id: str) -> Device:
63+
url = f"v3/devices/{device_id}/assign_blueprint"
64+
response = await self._client.post(url, json={"blueprint_id": blueprint_id})
65+
api.check_error(response)
66+
return Device.from_dto(response.json()["device"])
67+
6268
async def generate_communication_config(
6369
self, device_id: str, mqtt_protocol: MQTTProtocol
6470
) -> CommunicationConfig:

0 commit comments

Comments
 (0)