pyscript
pyscript copied to clipboard
additional kwarg in trigger, strange errors
I have a state trigger like this:
@state_trigger(f"{entity_id}",kwargs={'taak':taak})
def waskot_switch(**kwargs):
log.info(f"kwargs={kwargs}")
....
wich I have created by trigger closure... It works. BUT, as you see I added kwargs={'taak':taak}
Now I get an error in another script! run_coro: got exception Traceback (most recent call last): File "/config/custom_components/pyscript/eval.py", line 726, in call raise TypeError(f"{self.name}() called with unexpected keyword arguments: {unexpected}") TypeError: waskot_auto_light_off() called with unexpected keyword arguments: taak
I had to search for waskot_auto_light_off() and it's in a complete other source and the even the word "taak" is not used in that source.
Very strange. To be sure: So, The "waskot_Auto_light_off()" function is NOT used in the pyscript source where I added the kwargs={'taak':taak} and the word "taak" is not used in the source where there's a function "waskot_auto_light_off()
any idea?
You compressed all these arguments into **kwargs
, so the kwargs you expect are actually in kwargs['kwargs']
This would be a cleaner way to define your functions:
@state_trigger(
f"trigger",
f"one more optional trigger", # optional
watch=[], # optional
state_hold_false=None, # optional
state_hold=None, # optional
state_check_now=False, # optional
kwargs=dict( # <- these kwargs →→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→↓↓↓
my_kwarg='my_kwarg_value',
), # ↓↓↓
)
# ↓ are here
def my_func(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
log.debug(kwargs['my_kwarg'])
Also, I hope your variable taak
exists.
Looks like #512, which is fixed but not released. Can you run the latest to confirm?
Yes my variable 'taak' exists.
Looks like #512, which is fixed but not released. Can you run the latest to confirm?
Can I wait for the release?
f"trigger", f"one more optional trigger", # optional watch=[], # optional state_hold_false=None, # optional state_hold=None, # optional state_check_now=False, # optional kwargs=dict( # <- these kwargs →→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→↓↓↓ my_kwarg='my_kwarg_value', ),
Yes, it's a little bit different from what i did. And my version works too. The thing is, a complete other trigger function, in another source!, gives an error about the var "taak". As you can see, " waskot_auto_light_off() called with unexpected keyword arguments: taak" The source where waskot_auto_light_off is defined and used, is in another source. That's so strange. And when I remove the argument "taak", in my current source, the error disappears...
Another strange thing
registered_triggers = list()
@time_trigger('startup')
def create_trigger():
log.info("create trigger")
taak='testtaak'
@state_trigger('sensor.aqara_presence_sensor_fp1_presence_event',
kwargs=dict(my_kwarg=taak,)
,)
def light_sensor_1_trigger(**kwargs):
log.info(f"state trigger on triggered with kwargs {kwargs}")
@state_trigger('sensor.aqara_presence_sensor_fp1_presence_event',
kwargs={'taak':taak})
def light_sensor_2_trigger(**kwargs):
log.info(f"state trigger off triggered with kwargs {kwargs}")
registered_triggers.append(light_sensor_1_trigger)
registered_triggers.append(light_sensor_2_trigger)
in the logging i see: 2023-10-23 14:39:43.987 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/scripts/myscripts/testtriggerwithadditionalparm.py 2023-10-23 14:39:43.989 INFO (MainThread) [custom_components.pyscript.scripts.myscripts.testtriggerwithadditionalparm.create_trigger] create trigger 2023-10-23 14:39:50.569 INFO (MainThread) [custom_components.pyscript.scripts.myscripts.testtriggerwithadditionalparm.light_sensor_1_trigger] state trigger on triggered with kwargs {'trigger_type': 'state', 'var_name': 'sensor.aqara_presence_sensor_fp1_presence_event', 'value': 'approach', 'old_value': 'away', 'context': <homeassistant.core.Context object at 0x7f681460c0>, 'my_kwarg': 'testtaak'} 2023-10-23 14:39:50.570 INFO (MainThread) [custom_components.pyscript.scripts.myscripts.testtriggerwithadditionalparm.light_sensor_2_trigger] state trigger off triggered with kwargs {'trigger_type': 'state', 'var_name': 'sensor.aqara_presence_sensor_fp1_presence_event', 'value': 'approach', 'old_value': 'away', 'context': <homeassistant.core.Context object at 0x7f681460c0>, 'my_kwarg': 'testtaak', 'taak': 'testtaak'}
The last log line here:; the "my kwarg" appears again??? (this is in the same source of course.) My previous problem from above, there was for some reason, a kwargs param added to another trigger routine in another source (by pyscript)