Manage lock via python API

I am writing a tkinter gui to manage my locks using the python API:

from homeassistant_api import Client

I can successfully grab all of my locks and read the various attributes to produce my display:

locks = {key: value for key, value in client.get_entities()["lock"]}["entities"]

But none of the API documentation seems to be correct for locking or unlocking the locks using this particular API. I would expect that I would /get/ the entity of an individual lock and call its own .lock() or .unlock() method, but those don’t work. When I try to call certain items that are said to exist in the lock entity (such as the is_locked attribute) they’re never found. Additionally, when I do something like this:

l = client.get_entity(entity_id=eid)
print(l.state)

it prints out the whole entity object, not just the state attribute.

So I’m kind of at a loss of where to go here. I’m simply trying to write a toggle function where I pass it the entity_id, determine if it’s locked or unlocked, then execute the opposite method.

Looking at the code, i think you need to use…

client.trigger_service('lock', 'lock', {"target": {"entity_id": l.entity_id}})

And to get an entity state…

l.get_state()

The api does not return a link to the actual HA entity but an entity class within this custom api wrapper which only has a few methods/properties. It broadly follows how you would use the actual HA api but has different method names.

Where did you get the syntax for the third (**service_data) argument, the JSON string? I was unable to determine that by looking through the API code other than service_data is to be a dict.

The service data is just what is passed to the service call as the data key. Actually, this maybe wrong (unless it works) and should maybe just be…

{"entity_id": l.entity_id}

Based on the HA rest api service call docs (which is all this is calling).

I still cannot get this working. I’ve looked through the reference docs at https://homeassistantapi.readthedocs.io numerous times and tried rewriting my code to have the unlock service triggered from any object that could possible trigger it but everything errors.

As my OP states, I’m importing the homeassistant_api from the pip install. Then I have the following code:

    try:
        with Client(api_url, token) as client:
            d = client.trigger_service("lock","unlock", {"target": {"entity_id": "lock.back_door"}})
            

Which produces this error:

TypeError: RawClient.trigger_service() takes 3 positional arguments but 4 were given

This error is not special to the trigger_service method. Any time I try to run any method that works on a service I get the same error (just with different numbers depending on how many arguments the method takes).

Any ideas? I’m literally at a loss…

Quick update: I verified that the REST API works fine using curl, so the problem is something with this python API. Also, I tried simply passing ‘{“entity_id”: “lock.back_door”}’ as the third argument (just as I do with the curl call) and still got the same TypeError.