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

Add support to add multiple devices #13

Closed
fredrike opened this issue Mar 22, 2020 · 11 comments · Fixed by #14
Closed

Add support to add multiple devices #13

fredrike opened this issue Mar 22, 2020 · 11 comments · Fixed by #14

Comments

@fredrike
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Contributor Author

fredrike commented Apr 7, 2020

@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:home-assistant/home-assistant.io#12671 )

def publish_discovery(self, device, type, deviceId, config):
base_topic = self.config('base_topic')
device_name = self.config('device_name')
config.update({
'unique_id': '%s_%s' % (getMacAddr(), deviceId),
'availability_topic': (
'%s/%s/available' % (base_topic, device_name) if base_topic \
else '%s/available' % device_name
),
'device': {
'identifiers': getMacAddr(),
'connections': [['mac', getMacAddr(False)]],
'manufacturer': 'Telldus Technologies',
'model': Board.product(),
'name': device_name,
'sw_version': Board.firmwareVersion()
}
})
self.client.publish(
'%s/config' % self.getDeviceTopic(type, deviceId),
json.dumps(config),
retain = True
)
return (type, str(device.id()), str(deviceId))

What do you think of it?

@quazzie
Copy link
Owner

quazzie commented Apr 7, 2020

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
Copy link
Contributor Author

fredrike commented Apr 7, 2020

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
Copy link
Owner

quazzie commented Apr 7, 2020

MQTT_Homeassistant-0.85.zip

@fredrike
Copy link
Contributor Author

fredrike commented Apr 7, 2020

Do you want me to make this optional via configuration?

@fredrike
Copy link
Contributor Author

fredrike commented Apr 7, 2020

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
Copy link
Owner

quazzie commented Apr 7, 2020

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

@fredrike
Copy link
Contributor Author

fredrike commented Apr 7, 2020

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
Copy link
Contributor Author

fredrike commented Apr 7, 2020

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

@fredrike
Copy link
Contributor Author

fredrike commented Apr 7, 2020

I've updated the code in my PR..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants