devbis / ble2mqtt

Bluetooth to MQTT bridge, add your bluetooth-capable (including controllable) devices to your smart home
MIT License
125 stars 26 forks source link

ruuvitag: only include entities that are available #71

Closed QuintinHill closed 7 months ago

QuintinHill commented 7 months ago

I'm not sure how to achieve this. How can I make the set of entities more dynamic. My attempt has resulted in:

Traceback (most recent call last): File "/usr/bin/ble2mqtt", line 8, in sys.exit(main()) ^^^^^^ File "/usr/lib/python3.11/site-packages/ble2mqtt/main.py", line 171, in main File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete File "/usr/lib/python3.11/site-packages/ble2mqtt/main.py", line 127, in amain File "/usr/lib/python3.11/site-packages/ble2mqtt/ble2mqtt.py", line 95, in register File "/usr/lib/python3.11/site-packages/ble2mqtt/devices/base.py", line 504, in init File "/usr/lib/python3.11/site-packages/ble2mqtt/devices/base.py", line 210, in init File "/usr/lib/python3.11/site-packages/ble2mqtt/devices/ruuvitag.py", line 50, in entities if self._state.humidity: ^^^^^^^^^^^ AttributeError: 'RuuviTag' object has no attribute '_state'

I'm trying to avoid warnings from Home Assistant: WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'pressure' when rendering '{{ value_json.pressure }}'

devbis commented 7 months ago

It can be a problem as properties are exported to MQTT HA discovery topics after first device appear. So it has to know what entities it has. So, this dynamic creation will not work. Possible solution is to define all items and populate only available.

https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery

QuintinHill commented 7 months ago

Okay. Another option would be to create subclasses for the two RuuviTag Pro models which override the entities property.

devbis commented 7 months ago

Yes, that would be better in terms of internal structure. 👍

QuintinHill commented 7 months ago

@devbis I've now implemented the subclassing idea. I've tested it and now it works.

devbis commented 7 months ago

Thanks!