helgeerbe / OpenDTU-OnBattery

Software for ESP32 to talk to Hoymiles Inverters and Victrons MPPT battery chargers (Ve.Direct)
GNU General Public License v2.0
297 stars 62 forks source link

improvement: add base class for hass discovery #1137

Closed AndreasBoehm closed 1 month ago

AndreasBoehm commented 2 months ago

I started to refactor how we build hass discovery as an outcome of this comment: https://github.com/helgeerbe/OpenDTU-OnBattery/pull/1136#issuecomment-2253279029

My idea is to create MqttHassPublisher as a library with static methods that can be used to publish sensors, buttons, fields and helpers like createDeviceInfo.

AndreasBoehm commented 2 months ago

@schlimmchen I already added createDeviceInfo and publish as an example in this MR to check with you if i am going into the right direction.

AndreasBoehm commented 2 months ago

I also added publishBinarySensor2 in MqttHandleBatteryHassClass as a start to get the publish*Sensor methods into the library. Its a rough draft but it would be great if you can give your input on that method as well.

To make it work with even less code we would need to change MqttHassPublisher (with a better name of course) to be the base class for all MqttHandle*HassClass-es and make them implement a couple of methods like configTopicPrefix(), uniqueIdentifier() and deviceInfo() that we can then use inside publish*Sensor without repeating the same code over and over.

I would also suggest a strategy switch to type sensorIds manually instead of generating them from the caption, this will also allow us to rename captions without breaking the hass entities.

github-actions[bot] commented 1 week ago

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new discussion or issue for related concerns.