Hello!
I got here from HASS Issue #87182 (Error: Platform iotawatt does not generate unique IDs. · Issue #87182 · home-assistant/core · GitHub)
I agree that having the name as ID for outputs should be OK.
But, I got curious of what could be a way to implement really unique identifiers for outputs.
I looked at the way IoTaWatt saves the outputs, and as you already mentioned there is call to fetch the file http://<iotawatt.local>/config.txt when the user loads the UI, and when they create an output the endpoint /edit is called with multipart/form-data with the whole configuration data which I suppose is overridden. The relevant part of that configuration related to outputs is as following:
outputs: [
{
name: "Home",
units: "Watts",
script: "@1+@2"
}, {
name: "T2",
units: "Watts",
script: "@3+@4"
}, {
name: "T3",
units: "Watts",
script: "@5+@6"
}, {
name: "Washing",
units: "Watts",
script: "@12+@13"
}
]
Maybe a way to implement UUID could be web UI handled as following:
- When the user creates a new output, along with the name, units and scripts, a uuid field is added. We may use Javascript’s crypto.randomUUID() call, for example:
> crypto.randomUUID()
'5347261a-19f7-44cf-89e2-ff267cb8bd8b'
- The configuration is sent and stored. For example:
outputs: [
{
name: "Home",
units: "Watts",
script: "@1+@2",
uuid: "f66afed4-9935-4af7-aed6-a6fb78946eff"
}, {
name: "T2",
units: "Watts",
script: "@3+@4",
uuid: "70a112c6-a138-42c0-8207-c33551051235"
}, {
name: "T3",
units: "Watts",
script: "@5+@6",
uuid: "ec335946-0d69-411d-8e83-d791a1d68d9a"
}, {
name: "Washing",
units: "Watts",
script: "@12+@13",
uuid: "d92e39e0-8ca5-4223-a6ee-f0403b796875"
}
]
-
Special care is taken when a user is editing an entry. Since the UI knows that we are editing an entry (name, units, script is loaded, and there is a delete button) we just need to maintain that UUID and not generate a new one.
-
The query endpoint is updated to now include that new field. Totally backward compatible.
For the IoTaWatt firmware there is no difference at all, its only responsibility is to store the value, which could really be anything actually.
There must be a way to make this backward compatible with older firmwares. So, when the config is about to be saved again (/edit), check if there is any output without UUID and assign one.
I looked quickly in the frontend, and found that maybeee here (really, not sure at all) this logic could reside: IoTaWatt/index.htm at ca0a36849c1c833de40aac833e73e3f573db3f8a · boblemaire/IoTaWatt · GitHub
After that the iotawattpy package could be updated to generate unique sensors ID, and then the integration could use them.
What do you think? Terrible idea? Good idea? Lazy idea?
Regards!