Skip to content

Regression on set_state api call #2464

@jfparis

Description

@jfparis

What happened?

Hello

I am updating some small home assistant script that were previously running smoothly in 4.4.x

When I use the set_state api call, the calls are successful but it messes up with the boolean that are passed as attributes:

  • attributes set to False are dropped and will not appear in the entity
  • attributes set to True are changed to a string "true" in home assistant

This was not the case in the 4.4.x where all attributes would be transmitted with the correct type

Look like this might be linked to the work done in #2425

Version

4.5.12

Installation type

Home Assistant add-on

Relevant log output

Nothing

Relevant code in the app or config file that caused the issue

attributes = {
        "friendly_name": "Octopus Agile Rates",
        "unit_of_measurement": "GBP/kWh",
        "plunge": False,
        "peak": True,
    }
    self.set_state(
        "event.test_entity",
        state=1,
        attributes=attributes,
        replace = True,
    )

Anything else?

Doing a quick hatchet job in utils.py (as per below), I can obtain the correct behavior, but this would likely fixed the other bug you were addressing

def clean_kwargs(val: Any, *, http: bool = False) -> Any:
    match val:
        #case True if http:                                                                                                                                                  
        #    return "true"                                                                                                                                                   
        case str() | int() | float() | bool() | None:
            return val
        case datetime():
            return val.isoformat()
        case Mapping():
            return {k: clean_kwargs(v, http=http) for k, v in val.items()}
        case Iterable():
            return [clean_kwargs(v, http=http) for v in val]
        case _:
            return str(val)

def clean_http_kwargs(val: Any) -> Any:
    """Recursively cleans the kwarg dict to prepare it for use in HTTP requests."""
    cleaned = clean_kwargs(val, http=True)
    pruned = remove_literals(cleaned, (None,)) # Dropped False from the list
    return pruned

Metadata

Metadata

Assignees

Labels

bugissueSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions