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

Simplify entity attribute updates #9

Open
zehnm opened this issue Nov 9, 2023 · 0 comments
Open

Simplify entity attribute updates #9

zehnm opened this issue Nov 9, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@zehnm
Copy link
Contributor

zehnm commented Nov 9, 2023

At the moment one has to use the ucapi.configured_entities.update_attributes method to change an entity attribute.

With the new entity command handler, this is a bit akward. The received entity object should allow direct attribute updates, which also trigger a change event. Directly modifying an attribute on the received entity will not trigger any events.

Proposed changes:

  • add an update_attribute method to the entity class
    • either a generic method for all subclassed entities, or a concrete method per entity.
    • for most use cases, a single attribute update is enough. But there are cases where multiple attributes must be updated at the same time, e.g. for light color changes.
      • Either add an additional update_attributes method, or design a single method which allows both.
  • changed attributes will trigger a change event

This could then be used in a command handler:

async def switch_cmd_handler(
    entity: ucapi.Switch, cmd_id: str, _params: dict[str, Any] | None
) -> ucapi.StatusCodes:
    print(f"Got {entity.id} command request: {cmd_id}")

    state = None
    if cmd_id == switch.Commands.ON:
        # implement ON logic...
        state = switch.States.ON
    elif cmd_id == switch.Commands.OFF:
        # implement OFF logic...
        state = switch.States.OFF

    if state:
        entity.update_attribute(switch.Attributes.STATE, state)

    return ucapi.StatusCodes.OK
@zehnm zehnm added the enhancement New feature or request label Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant