Simplify entity attribute updates
Opened this issue · 0 comments
zehnm commented
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.
- Either add an additional
- 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