Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create groups #1071

Open
goossensbas opened this issue Feb 9, 2025 · 3 comments
Open

create groups #1071

goossensbas opened this issue Feb 9, 2025 · 3 comments

Comments

@goossensbas
Copy link

goossensbas commented Feb 9, 2025

Hello all,

I am using the python-matter-server docker container as a stand-alone application.
I wrote an interface in python to test the capabilities of Matter.

The documentation of the server is somewhat limited. Basic functions like onoff and commissioning work,
but other features like binding a switch to a light is not documented at all.
(i managed to do this by writing the values in the correct cluster attributes)

I want to create a group for 2 or more light devices, but i can't assemble the correct command.

my code:

async def add_node_to_group(client, group_id, node_id):
    res = await client.read_attribute(node_id,f"1/4/0")
    print(f"{res}")
    current_membership = res.get("1/4/0", 0)  # Extracting the attribute value
    print(f"Current Membership: {current_membership}")

    # Define the AddGroup command
    command = clusters.Groups.Commands.AddGroup(
        groupId=group_id,  # Replace with your desired group ID
        groupName=f"group{group_id}"  # Replace with your desired group name
    )
    payload = dataclass_to_dict(command)

    add_group_command = json.dumps({
        "message_id": "example",
        "command": "device_command",
        "args": {
            "endpoint_id": 1,
            "node_id": node_id,
            "payload": payload,
            "cluster_id": command.cluster_id,
            "command_name": "AddGroup"
        }
    })
    # Write the updated group membership attribute
    write_res = await client.send_device_command(node_id, 1, add_group_command)
    print(f"Write Attribute Response: {write_res}")

gives the error:

Error during Matter client operation: Groups.Commands.AddGroup.__init__() got an unexpected keyword argument 'GroupId'
Disconnected from Matter server.

I also tried GroupId and GroupName with capital letters, but it gives the same error.

command = clusters.Groups.Commands.AddGroup(
        GroupId=group_id,  # Replace with your desired group ID
        GroupName=f"group{group_id}"  # Replace with your desired group name
    )

How can i create a group and add nodes to it?
How do i send group commands?

Thanks in advance for the help!

@goossensbas
Copy link
Author

UPDATE

I read through the matter documentation, and i figured out i need a group key first, so i wrote one and bound it to a group ID.

Adding the key and binding it was successful, but i still can't add an endpoint to the group.


async def write_group_key(client, node_id, group_id):
    group_key_set_id = group_id
    group_key_security_policy = 0
    # Randomly generate a 16-byte (128-bit) key
    # group_key = secrets.token_bytes(16)
    group_key=bytes.fromhex("d0d1d2d3d4d5d6d7d8d9dadbdcdddedf")
    group_start_time = 2220000

    group_key_set = clusters.GroupKeyManagement.Structs.GroupKeySetStruct(
        groupKeySetID=group_key_set_id,
        groupKeySecurityPolicy=group_key_security_policy,
        epochKey0=group_key,
        epochStartTime0=group_start_time
    )

    # Add key to device
    command = clusters.GroupKeyManagement.Commands.KeySetWrite(group_key_set)
    res = await send_command_to_node(client, node_id, 0, command)
    print(f"Response: {res}")  

    # bind key to group
    binding_entry = [{"groupId": group_id, "groupKeySetID": group_key_set_id}]
    res = await client.write_attribute(node_id,f"0/63/0", binding_entry)
    print(f"Response: {res}")

result:

Command GroupKeyManagement.Commands.KeySetWrite(groupKeySet=GroupKeyManagement.Structs.GroupKeySetStruct(groupKeySetID=2052, groupKeySecurityPolicy=0, epochKey0=b'\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf', epochStartTime0=2220000, epochKey1=Null, epochStartTime1=Null, epochKey2=Null, epochStartTime2=Null)) sent to node 1, endpoint 0, cluster 63
Response: None
Response: [{'Path': {'EndpointId': 0, 'ClusterId': 63, 'AttributeId': 0}, 'Status': 0}]

Then i wanted to write the group ID to an endpoint, but that doesn't work.


async def add_node_to_group(client, node_id, group_id):    
    # Define the AddGroup command
    command = clusters.Groups.Commands.AddGroup(
        groupID=int(group_id),  # Replace with your desired group ID
        groupName=f"group{group_id}"  # Replace with your desired group name
    )
    try:
        res = await send_command_to_node(client, node_id, 1, command)
        print(f"Write Attribute Response: {res}")
    except Exception as e:
        print(f"Error during AddGroup command: {e}")

result:

Command Groups.Commands.AddGroup(groupID=2052, groupName='group2052') sent to node 1, endpoint 1, cluster 4
Write Attribute Response: None

When i read the group info with this function:

async def read_group(client, node_id, group_id):
    
    try:
        # Define the AddGroup command
        command = clusters.Groups.Commands.GetGroupMembership()
        res = await send_command_to_node(client, node_id, 1, command)
        print(f"Write Attribute Response: {res}")
    except Exception as e:
        print(f"Error during GetGroupMembership command: {e}")

    try:    
        command = clusters.Groups.Commands.ViewGroup(groupID=group_id)
        res = await send_command_to_node(client, node_id, 1, command)
        print(f"Write Attribute Response: {res}")
    except Exception as e:
        print(f"Error during ViewGroup command: {e}")

I get the result:

Command Groups.Commands.GetGroupMembership(groupList=[]) sent to node 1, endpoint 1, cluster 4
Write Attribute Response: None
Command Groups.Commands.ViewGroup(groupID=2052) sent to node 1, endpoint 1, cluster 4
Write Attribute Response: None

@marcelveldt
Copy link
Contributor

group management is not yet supported, its on the list to add to the features.

@goossensbas
Copy link
Author

@marcelveldt Thank you for your feedback. now i know that my script works :-)

Would it be possible to do some adjustments myself so it works?
Where could i find the source code of what's running in the container?

thanks in advance, and have a nice weekend!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants