quazzie / tellstick-plugin-mqtt-hass

Plugin for tellstick, connect to homeassistant via mqtt with autodiscovery.
39 stars 7 forks source link

Add support to add multiple devices #13

Closed fredrike closed 4 years ago

fredrike commented 4 years ago

The way Home-Assistant works with devices is the following:

That means that in the Telldus environment each sensor should be its own device and this device should be nested under the Tellstick-device.

This is how the sensor "Vit Inne" in the Tellstick-live integration (Mjövik is the name of my Tellstick-Lite):

Screenshot 2020-03-22 at 9 32 54 Screenshot 2020-03-22 at 9 42 03

I therefore suggest that this plugin sends a binary_sensor that represents the Tellstick like this:

homeassistant/binary_sensor/telldus/ACCA5401B5EA/config

{"name": "Telldus", "payload_off": "offline", "payload_on": "online", "device": {"sw_version": "1.3.1", "name": "telldus", "identifiers": "ACCA5401B5EA", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "model": "tellstick-znet-lite-v2", "manufacturer": "Telldus Technologies"}, "device_class": "connectivity", "state_topic": "telldus/telldus/available", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_main"}

And that this entities (posted to homeassistant/sensor/telldus/6_1_0/config, homeassistant/sensor/telldus/6_2_0/config & homeassistant/sensor/telldus/ACCA5401B5EA_6_battery/config:

{"unit_of_measurement": "\u00b0C", "json_attributes_topic": "home/sensor/telldus/6_1_0/state", "name": "Vit Inne temp - \u00b0C", "value_template": "{{ value_json.value }}", "device": {"sw_version": "1.3.1", "name": "telldus", "identifiers": "ACCA5401B5EA", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "model": "tellstick-znet-lite-v2", "manufacturer": "Telldus Technologies"}, "device_class": "temperature", "state_topic": "home/sensor/telldus/6_1_0/state", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_6_1_0"}
{"unit_of_measurement": "%", "json_attributes_topic": "home/sensor/telldus/6_2_0/state", "name": "Vit Inne humidity - %", "value_template": "{{ value_json.value }}", "device": {"sw_version": "1.3.1", "name": "telldus", "identifiers": "ACCA5401B5EA", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "model": "tellstick-znet-lite-v2", "manufacturer": "Telldus Technologies"}, "device_class": "humidity", "state_topic": "home/sensor/telldus/6_2_0/state", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_6_2_0"}
{"unit_of_measurement": "%", "device": {"sw_version": "1.3.1", "name": "telldus", "identifiers": "ACCA5401B5EA", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "model": "tellstick-znet-lite-v2", "manufacturer": "Telldus Technologies"}, "name": "Vit Inne - Battery", "device_class": "battery", "state_topic": "home/sensor/telldus/ACCA5401B5EA_6_battery/state", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_ACCA5401B5EA_6_battery"}

Are changed to:

{"unit_of_measurement": "\u00b0C", "json_attributes_topic": "home/sensor/telldus/6_1_0/state", "name": "Vit Inne temp - \u00b0C", "value_template": "{{ value_json.value }}", "device": {"name": "Vit Inne", "identifiers": "ACCA5401B5EA-vit_inne", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "via_device": "ACCA5401B5EA"}, "device_class": "temperature", "state_topic": "home/sensor/telldus/6_1_0/state", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_6_1_0"}
{"unit_of_measurement": "%", "json_attributes_topic": "home/sensor/telldus/6_2_0/state", "name": "Vit Inne humidity - %", "value_template": "{{ value_json.value }}", "device": {"name": "Vit Inne", "identifiers": "ACCA5401B5EA-vit_inne", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "via_device": "ACCA5401B5EA"}, "device_class": "humidity", "state_topic": "home/sensor/telldus/6_2_0/state", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_6_2_0"}
{"unit_of_measurement": "%", "device": {"name": "Vit Inne", "identifiers": "ACCA5401B5EA-vit_inne", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "via_device": "ACCA5401B5EA"}, "name": "Vit Inne - Battery", "device_class": "battery", "state_topic": "home/sensor/telldus/ACCA5401B5EA_6_battery/state", "availability_topic": "telldus/telldus/available", "unique_id": "ACCA5401B5EA_ACCA5401B5EA_6_battery"}

I don't know if "model" & "manufacturer" is available at the plugin level but perhaps "model" could represent the type of device.

fredrike commented 4 years ago

ACCA5401B5EA-vit_inne could be created by using slugify just as done here: https://github.com/home-assistant/core/blob/master/homeassistant/util/__init__.py#L47

i.e.


from slugify import slugify

ident = f"{mac}-{slugify(name, separator='_'}"
fredrike commented 4 years ago

@quazzie: your code really looks amazing, I think I might be able to wrap multiple devices together myself..

My idea is to add a helper function for publish_discovery() that uses via_device (I've even documented it now:https://github.com/home-assistant/home-assistant.io/pull/12671 ) https://github.com/quazzie/tellstick-plugin-mqtt-hass/blob/8b3d33af29b7b3a8a0d242048f04d6ad758d9c19/hass_client/Client.py#L414-L437

What do you think of it?

quazzie commented 4 years ago

Thanks! Sorry i have not had time to look at this.

I tried you pr on my dev tellstick, seems to work as you describe. Maybe a problem on HA's end but i think this is a lot more confusing :) I can't see all my tellstick devices in one place, having a hard time finding them all, is it suppose to be like that?

Before i could just go to devices - tellstick and get all my sensors/devices.

fredrike commented 4 years ago

The idea is to go to Integrations -> MQTT and there you can see all Devices..

From one Device you should be able to see Entites and parents..

Look at the Related tab (and hopefully there should be a hierarchy). Screenshot 2020-04-07 at 11 22 27

Could you upload the test version so I can test it?

quazzie commented 4 years ago

MQTT_Homeassistant-0.85.zip

fredrike commented 4 years ago

Do you want me to make this optional via configuration?

fredrike commented 4 years ago

This is not right:

homeassistant/<type 'type'>/telldus/hub/config {"availability_topic": "telldus/telldus/available", "name": "telldus", "payload_on": "online", "device": {"sw_version": "1.3.1", "name": "telldus", "identifiers": "ACCA5401B5EA", "connections": [["mac", "AC:CA:54:01:B5:EA"]], "model": "tellstick-znet-lite-v2", "manufacturer": "Telldus Technologies"}, "device_class": "connectivity", "state_topic": "telldus/telldus/available", "payload_off": "offline", "unique_id": "ACCA5401B5EA_hub"}

Hm, the hierarchy does not seem to work either..

quazzie commented 4 years ago

MQTT_Homeassistant-0.85.1.zip Fixed <type 'type'> to binary_sensor

fredrike commented 4 years ago

I do think this is the intended way Devices should work. There is hierarchy but it is not shown in the frontend (yet).

What do you think about setting Device.protocol().title() as device.manufacturer?

Also, the device_name used in topic must be sluggified according to: https://www.home-assistant.io/docs/mqtt/discovery/ (node_id)

fredrike commented 4 years ago

Vi kanske ska skippa min unique_id ändring också, det kommer ju ändra alla devices och bli en "Breaking change" på gamla entiteter.

fredrike commented 4 years ago

I've updated the code in my PR..