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

FR: option to publish the device availability topic as a binary sensor #206

Open
erkr opened this issue Nov 25, 2023 · 1 comment
Open
Labels
selected for development Feature will be implemented in the next version

Comments

@erkr
Copy link

erkr commented Nov 25, 2023

Hi,

Thanks for this great library. As an experiment, I ported one of my projects (a board with 6 relays with both a MQTT and a web interface) to this library. Reason: mainly the Discovery feature, as my projects require manual MQTT configurations.

In my projects I always configure the availability topic as a binary "connection" sensor. But in this library, that topic is not configured as a sensor for HA. I still have to add it manually like this:

binary_sensor:
  - name: connection
    unique_id: RELAIS_CTRL_166864_connection
    state_topic: "aha/RELAIS-CTRL_166864/avty_t"
    payload_on: "online"
    payload_off: "offline"
    device_class: connectivity
    device:
      name: RELAIS-CTRL
      identifiers: 
       - RELAIS-CTRL_166864

It would be great when this binary_sensor could be added automatically via discovery as well

Best Eric

@erkr
Copy link
Author

erkr commented Nov 27, 2023

Update; I created a "ugly' but 'simple' workaround to get the config discovery topic for an Availability binary sensor published without modifying the library.
I derived a new small new device type from the base device type class. Just instantiate one instance and it will published the config (works only if shared availablity is configured).
The header part:

class HAConnectionBinarySensor : HABaseDeviceType
{
public:
  HAConnectionBinarySensor(const char *name);

protected:
  virtual void buildSerializer() override;
  virtual void onMqttConnected() override;
};

And the cpp part:

//////////////////////////
// Special discovery device type only for publishing the config (doesn't do anything else)
// for a binary sensor that represents the connection state 
HAConnectionBinarySensor::HAConnectionBinarySensor(const char* uniqueId) : HABaseDeviceType( AHATOFSTR(HAComponentBinarySensor), uniqueId )
{
}

void HAConnectionBinarySensor::buildSerializer()
{
    const HADevice* device = mqtt()->getDevice();
    if ( !device ||
         !(device->isSharedAvailabilityEnabled()) || 
         _serializer || 
         !uniqueId()  ) {
        return;
    }
    // publish a binary sensor that represents the availablity of this device
    _serializer = new HASerializer(this, 7); 
    _serializer->set(AHATOFSTR(HANameProperty),uniqueId());
    _serializer->set(AHATOFSTR(HAUniqueIdProperty), _configUniqueID);
    _serializer->set(AHATOFSTR(HADeviceClassProperty), "connectivity");
    _serializer->set(AHATOFSTR("pl_on"),"online");
    _serializer->set(AHATOFSTR("pl_off"),"offline"); // payload_off was not defined in lib
    _serializer->set(AHATOFSTR(HAStateTopic), device->getAvailabilityTopic());
    _serializer->set(HASerializer::WithDevice);
}

void HAConnectionBinarySensor::onMqttConnected() {
    if (!uniqueId()) {
        return;
    }
    publishConfig();
}

Example of the binary sensor configuration published in the MQTT broker:
The topic: homeassistant/binary_sensor/RCTRL_198e31/Connection/config
The payload:

{
  "name": "Connection",
  "uniq_id": "RCTRL_198e31_Connection",
  "dev_cla": "connectivity",
  "pl_on": "online",
  "pl_off": "offline",
  "stat_t": "aha/RCTRL_198e31/avty_t",
  "dev": {
    "ids": "RCTRL_198e31",
    "name": "RCTRL",
    "sw": "v1.2",
    "mdl": "ESP8266",
  }
}

And what is added to MQTT device in Home assistant:
image

Have fun :-)

@dawidchyrzynski dawidchyrzynski added the selected for development Feature will be implemented in the next version label Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
selected for development Feature will be implemented in the next version
Projects
None yet
Development

No branches or pull requests

2 participants