dawidchyrzynski / arduino-home-assistant

ArduinoHA allows to integrate an Arduino/ESP based device with Home Assistant using MQTT.
https://dawidchyrzynski.github.io/arduino-home-assistant/
GNU Affero General Public License v3.0
490 stars 118 forks source link

Bug with the maximum number of device types #190

Closed martaisty closed 8 months ago

martaisty commented 1 year ago

The bug:

The configured maximum number of device types _maxDevicesTypesNb doesn't represent the actual maximum of device types you can add. The exact number of devices is always one less than the configured value.

Root of the bug:

The condition that checks if it is possible to add more devices is incorrect.

The condition: https://github.com/dawidchyrzynski/arduino-home-assistant/blob/0fc32a5bad861ca76767ff13187f8afabcc4f9a0/src/HAMqtt.cpp#L188-L190

Initial values: https://github.com/dawidchyrzynski/arduino-home-assistant/blob/0fc32a5bad861ca76767ff13187f8afabcc4f9a0/src/HAMqtt.cpp#L21-L23

Simple example to understand why it's incorrect

Let's say I want only 1 device type, so I pass maxDevicesTypesNb = 1 to the HAMqtt's constructor. Then I won't be able to add the device due to the incorrect if-check above. 0 + 1 >= 1 => true. The same is true for other numbers, having a maximum set to 7, you can add only 6 devices.