AppDaemon / appdaemon

:page_facing_up: Python Apps for Home Automation
Other
832 stars 417 forks source link

Cannot create entity with namespace #883

Closed pirakleous closed 4 years ago

pirakleous commented 4 years ago

Hello,

I am trying to create a custom entity which fetches some data from the API and I am using the following API call to save the entity with namespace but it gives an error. If it uses the default namespace if the entity does not exists it creates it automatically.

This is the code i am using to save/create the entity self.set_state(x.entity_id(), state=x.state(), attributes=x.device_state_attributes(), namespace=NAMESPACE)

2020-02-09 17:18:52.662807 WARNING publicTransport: publicTransport: Entity mytransport.live_information_for_bus_stop_1992 not found in namespace transport 2020-02-09 17:18:52.664402 WARNING publicTransport: ------------------------------------------------------------ 2020-02-09 17:18:52.664774 WARNING publicTransport: Unexpected error running initialize() for publicTransport 2020-02-09 17:18:52.665078 WARNING publicTransport: ------------------------------------------------------------ 2020-02-09 17:18:52.666170 WARNING publicTransport: Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/appdaemon/app_management.py", line 145, in initialize_app await utils.run_in_executor(self, init) File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 276, in run_in_executor response = future.result() File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/config/appdaemon/apps/public_transport/public_transport.py", line 45, in initialize self.set_state(x.entity_id(), state=x.state(), attributes=x.device_state_attributes(), namespace=NAMESPACE) File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 191, in inner_sync_wrapper f = run_coroutine_threadsafe(self, coro(self, *args, **kwargs)) File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 285, in run_coroutine_threadsafe result = future.result(self.AD.internal_function_timeout) File "/usr/lib/python3.8/concurrent/futures/_base.py", line 439, in result return self.__get_result() File "/usr/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result raise self._exception File "/usr/lib/python3.8/site-packages/appdaemon/adapi.py", line 1360, in set_state return await self.AD.state.set_state(self.name, namespace, entity_id, **kwargs) File "/usr/lib/python3.8/site-packages/appdaemon/state.py", line 412, in set_state if entity_id in self.state[namespace]: KeyError: 'transport'

ReneTode commented 4 years ago

namespaces need to be created in appdaemon.yaml if you have the HASS plugin setup without a namespace it will get the namespace default.

if you create an entity in the hass namespace it will show up in hass. if you create an entity in a custom namespace it will be internal in AD and not show up in HASS.

pirakleous commented 4 years ago

Thanks for the info @ReneTode

I am using HASS plugin without namespace however, i set the namespace on my app using: self.set_namespace(NAMESPACE) I want each app to use a different namespace if possible to keep a datastore for each app

ReneTode commented 4 years ago

namespaces can only be created in appdaemon.yaml https://appdaemon.readthedocs.io/en/latest/CONFIGURE.html#advanced-appdaemon-configuration so you would need to configure AD for every namespace you want to use.

but .... HASS entities will always be in the HASS namespace (which is "default" when you didnt set it)

self.set_namespace set the default namespace for the commands that follow after that.

if you want to work with HASS entities, you cant save that by using another namespace. only entities that are created in a custom namespace will be stored (and they dont show up in HASS, because they are AD internal)

pirakleous commented 4 years ago

Thank you so much for that.

dongore commented 3 years ago

Hi, I am trying something similar and maybe someone here can help.

I created a custom namespace in appdaemon.yaml:

namespaces:
  speak:
    writeback: hybrid

But the namespace doesn't seem to exist in my app:

2021-07-26 21:41:42.500511 WARNING hello_world: Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/appdaemon/app_management.py", line 161, in initialize_app
    await utils.run_in_executor(self, init)
  File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 299, in run_in_executor
    response = future.result()
  File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/appdaemon/apps/hello.py", line 14, in initialize
    self.set_state("speak.messages", state= "on", attributes = {"msg1": "Test 1", "msg2": "Test 2"}, namespace = "speak")
  File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 197, in inner_sync_wrapper
    f = run_coroutine_threadsafe(self, coro(self, *args, **kwargs))
  File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 308, in run_coroutine_threadsafe
    result = future.result(self.AD.internal_function_timeout)
  File "/usr/lib/python3.8/concurrent/futures/_base.py", line 444, in result
    return self.__get_result()
  File "/usr/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "/usr/lib/python3.8/site-packages/appdaemon/adapi.py", line 1530, in set_state
    return await self.AD.state.set_state(self.name, namespace, entity, **kwargs)
  File "/usr/lib/python3.8/site-packages/appdaemon/state.py", line 599, in set_state
    if entity in self.state[namespace]:
KeyError: 'speak'

I'm trying to set a state like this:

self.set_state("speak.messages", state= "on", attributes = {"msg1": "Test 1", "msg2": "Test 2"}, namespace = "speak")

ReneTode commented 3 years ago

did you restart appdaemon? and did you look in the logs after restart if the namespace is created?

dongore commented 3 years ago

Thanks for your quick reply. Of course in the end it was only a stupid indentation error. It's working now.