Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDRMR] Possible conversion to an integration #10

Open
cpyarger opened this issue May 22, 2022 · 15 comments
Open

[SDRMR] Possible conversion to an integration #10

cpyarger opened this issue May 22, 2022 · 15 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@cpyarger
Copy link
Owner

So, While this current version works, we are currently unable to give any added sensors a unique_id in home assistant. This leads to an issue where we are unable to use the built in methodologies for changing the sensor names to something a bit more user friendly.

this is due to a limitation in the rest api which is used by addons

Integrations do not have this limitation

Any thoughts on this?

@cpyarger cpyarger added enhancement New feature or request help wanted Extra attention is needed labels May 22, 2022
@cpyarger cpyarger changed the title Possible conversion to an integration [SDRMR] Possible conversion to an integration May 22, 2022
@pasyn
Copy link
Collaborator

pasyn commented May 22, 2022

I did some research and came to the following conclusion:

To convert this addon to an integration we would have to rewrite it in JS along with the functions required from rtl-sdr and rtlamr. This would require a lot of work to say the least.

Another option would be to split the function between an addon and an integration. The addon would be essential what it is currently while the integration would act as an intermediary and write entity values to the registry. This option is messy and complicates setup significantly.

A possible third option would be to use the templating function of the REST api. As far as I can tell, since templates are technically an integration, they have the ability to set unique ids. This would only require some tweaks in the json output to HA.

@cpyarger
Copy link
Owner Author

I don't have any knowledge of the third option, Can you point me to some documentation or examples of what you mean?

@pasyn
Copy link
Collaborator

pasyn commented May 23, 2022

https://developers.home-assistant.io/docs/api/rest
/api/template

https://www.home-assistant.io/docs/configuration/templating

https://www.home-assistant.io/integrations/template/

Upon further reading, it is now my understanding that the template integration is not the same thing as "templating" or "rendering a template" as referred to in the rest api docs. I wish this was clearer in the docs as the naming convention would imply otherwise.

But I did find that by adding the following to my config.yaml, I could set the UniqueID of my meter sensor and rename it from the UI.

template:
  - sensor:
    - name: "23876447"
      unique_id: "23876447"
      state: ''

My understanding from the community forum is that the template integration in config.yaml sort of preallocates/preregisters the entity allowing the Unique ID to be set in the registry. The addon will simply update the state of the already existing entity.

https://community.home-assistant.io/t/ha-core-rest-api-post-unique-id-for-entities/216122/2

@cpyarger
Copy link
Owner Author

Sigh, template sensors ugh. Doing my best to make it so those are never needed lol. I guess, The fourth option is to allow a map between sensor ID and sensor name in the addon, Maybe with a default based on sensor type?

@pasyn
Copy link
Collaborator

pasyn commented May 23, 2022

I understand. I wish there was a solution that only uses the REST API.

Im not sure that I completely follow what you mean by mapping in the addon. Are talking about adding the ability to change the name of the entity from within the addon?

Addressing the template method again, it should be possible to automate the editing of the config in such a manner that the user never has to. For example, the addon would create /config/packages/sdrmr.yaml where it would maintain a template sensor for each meter id being read by the addon.

@cpyarger
Copy link
Owner Author

Im not sure that I completely follow what you mean by mapping in the addon. Are talking about adding the ability to change the name of the entity from within the addon?

yes

Addressing the template method again, it should be possible to automate the editing of the config in such a manner that the user never has to. For example, the addon would create /config/packages/sdrmr.yaml where it would maintain a template sensor for each meter id being read by the addon.

I understand that's a valid method, but not one I want to be using. getting away from template sensors is the whole reason I built this addon

@pasyn
Copy link
Collaborator

pasyn commented May 23, 2022

Goatcha! That should be straightforward enough to implement. Do you prefer prefixes in the entity names such as gas_meter-387327906 or water_sdrmr-1540485337? Or would you like to go the route of specifying an entity name for every meter id entered in the addon options?

Prefixes have the benefit of working when meter id is not specified and all in range meters are reported. And they can be changed from the default value in the addon options. Although I do not see reading all in range meters as a valid use case. If anything it should be discouraged for the sake of privacy.

@cpyarger
Copy link
Owner Author

Legally, Privacy is not an issue here, It falls under the heading of yelling out your window (:
Prefixes would be better, but honestly, I think I should look into making the changes in the rest api. or seeing if there are any plans to.

Any idea if there is a method using apparmor?

@pasyn
Copy link
Collaborator

pasyn commented May 24, 2022

Apparmor would not be applicable. It is a kernel security module that controls access to operating system resources. Similar to SELinux AFAIK.

I agree with you about checking into the future of the rest api. I also think prefixes would be a good short term solution.

I found something else interesting while searching through the HA developer docs. The documentation is vague and limited but it could provide another route to registering entities. The supervisor api provides access to mysql and mqtt services.
https://developers.home-assistant.io/docs/add-ons/communication#services-api

This is what I am currently researching:

Option 1 - Addon can post mqtt topic to HA mqtt integration. The mqtt integration should be able to set Unique_ID. Downside is that it requires the mqtt integration to be set up.

Option 2 - Addon posts entity directly to HA mysql database. No integration setup required. I don't know if this is even possible.

I will update when I learn more.

@cpyarger
Copy link
Owner Author

I lean more towards option 2 being the most viable method. Second biggest reason I made this addon was to get away from MQTT, While I know a lot of people love it, I hate having to have yet another service and thing to worry about.

Maybe something like this as a workflow

RTLSDR->REST Post -> if RETURNs 201 -> SQL add unique_id
RTLSDR->REST Post -> if RETURNs 200 -> Do nothing

@pasyn
Copy link
Collaborator

pasyn commented May 25, 2022

The workflow is solid but I'm still not 100% clear on how entities and devices are recorded internally within HA. Is the database only for the recording of events or is it also used for entity registration? Anyway, the mysql part of the supervisor api only works with addons that provide a database service such as the mariaDB addon. The api will not work with the internal sqlite DB. You could use the mariaDB addon for the database backend for the HA recorder but thats just extra steps. You could also edit the HA sqlite DB directly without the api since it is stored in the config directory. Still not even sure if that is where the entity registry is located. And at that point you may as well go back to the template approach which is less than ideal but arguably the method most inline with intent of the HA core devs.

Lets implement prefixes for the time being and poke the HA devs about improving the rest api.

@cpyarger
Copy link
Owner Author

Lets hold off on adding prefixes for the time being. I'd rather not have to reset everything up lol.

I greatly appreciate your thought and time put into this. Life's been busy so thank you.

@cpyarger
Copy link
Owner Author

Yeah, I posted a request to add the ability to add a unique id to sensors created with rest on creation,
home-assistant/architecture#769
apparently never going to happen.

@pasyn
Copy link
Collaborator

pasyn commented May 31, 2022

Okay. I read through it. I understand the reasoning as to why the api will not implement entities although I wish there was an alternative solution available for this scenario. For example, if integrations could be written in languages other than purely Python, we could make a Python wrapper for sdrmr.

@cpyarger
Copy link
Owner Author

I was thinking something similar. It would be nice if we could package it so that when someone installs or updates the addon. The integration also gets installed/upgraded. Then when the addon is started we can have any new meter show up as a discovered device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants