appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

Feature request: get entity from name

Open TD22057 opened this issue 6 years ago • 3 comments

It would be nice to have a utility method to get an entity from a name "a.b.c". I find myself doing this a lot in my apps where I want to keep a handle to the entity object.

def get_entity(self, name):
    # Take 'foo.bar.baz' and return self.entities.foo.bar.baz.
    # This makes it easy to convert a string to an arbitrary entity.
    elems = name.split(".")
    obj = self.entities
    for e in elems:
        obj = getattr(obj, e)

    return obj

TD22057 avatar Jan 14 '19 02:01 TD22057

if you do things a lot you can create a general function for it. it seems to me that you use entities as object and you got self.entities in appdaemon entities are no objects and there is no self.entities, so then its hard to create something like that for general use.

ReneTode avatar Jan 14 '19 11:01 ReneTode

Sorry, I'm confused. The page below seems to say to use self.entities as the correct way to access an entity (which is an object) https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html There are already similar utility methods on the hass plugin base class like verifying that an entity is real. Rather than checking if an entity exists (which is a hass base class method), it could just return the entity it none of it doesn't exist.

TD22057 avatar Jan 14 '19 14:01 TD22057

yeah sorry, sometimes i hang a bit in the past. i still use get_state ;)

i would probably use

device, entity = self.split_entity(name)
self.entities[device][entity]

ReneTode avatar Jan 14 '19 15:01 ReneTode