Support for passing 'attribute' argument to get_state
insertjokehere opened this issue ยท 5 comments
get_state
supports an attribute
kwarg (https://appdaemon.readthedocs.io/en/latest/AD_API_REFERENCE.html#state-operations) but when running automations that use this under the test framework, I get an error:
TypeError: <lambda>() got an unexpected keyword argument 'attribute'
Good catch!
It is indeed not supported, the mock function taking care of mocking get_state
only takes one single argument: https://github.com/FlorianKempenich/Appdaemon-Test-Framework/blob/347a02a05d9f6367efa909d6a6517e747df8f318/appdaemontestframework/given_that.py#L12
- I could add a quick fix to allow multiple arguments, but ignore all except the
entity_id
. - Or we could find a way to actually mock the behavior.
Going the second path, we would need to clarify what exactly is expected from these 2 extra options: attribute
and namespace
.
Could you provide me with examples of how you use them in your automations?
I use them in a couple of different places, the easiest example is with plant entities - the way this behaves is that the state is either ok
or problem
, to work out what the actual issue is you need to look at the problems
attribute, so a minimal automation looks something like:
class PlantMonitor:
def initialize(self):
self.listen_state(self.on_state_change, "plant.some_plant", attribute="problems")
def on_state_change(self, entity, attribute, old, new, kwargs):
if 'low battery' in new:
self.call_service("notify/some_device", ....)
if 'low moisture' in new:
self.call_service("notify/some_device", ....)
Strictly, state attributes and state are two different properties of an entity and can exist at the same time, so it might be worth adding something to the given_that().state_of(...).is_set_to(...)
structure to allow this to be set.
Will see what I can to putting a patch together
I use it to poll
def get_average_change(self):
stats_sensor = self.get_state(
self.args["stats_sensor"], attribute="average_change")
if (stats_sensor == 'unknown' or stats_sensor is None):
return 0
return int(float(stats_sensor))```
Where stats sensor is a https://www.home-assistant.io/components/sensor.statistics/ sensor. Ran into the same error, trying to write tests for it. It's part of my hygrostat app.
Sorry for the late response, I've been slightly overwhelmed Q1 of this year. I finally found the time to add features I had in the works for quite some time:
Definitely check them out, they're pretty neat ๐
I imported this from a separate project and most of the work was to figure out how to use a 'dev' version of a library in said project (git submodules FTW). This wasn't really related with the issue, but I thought I'd share.
That previous part being done, I then took a look at this attributes
issue. . . and it turns out it's an easy fix ==> Version v2.3.0 now support passing 'attribute' to get_state
๐- See Doc
Thanks for the examples, they were really helpful for me to understand the use-case.
I'd love if you could let me know if the implementation matches your need ๐
Thanks for checking out my project ๐
@FlorianKempenich Thanks! Will give it a go over the weekend