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

Implement base for MQTT device registry integration #16943

Merged
merged 8 commits into from
Oct 8, 2018

Conversation

OttoWinter
Copy link
Member

@OttoWinter OttoWinter commented Sep 28, 2018

Description:

Now after #16918, we can start implementing the device registry for MQTT platforms. This PR adds a new parameter to the MQTT config of sensor s: device. With this, MQTT-enabled devices can send along a device: section in the MQTT discovery payload which uniquely identifies the device.

This PR only adds the device registry feature to the sensor.mqtt platform as a sample implementation, but others will follow once this PR is done.

A discovery payload could for example look like this:

{
  "name": "...",
  ...
  "unique_id": "veryunique",
  "device": {
    "identifiers": ["my_identifier"],
    "connections": [{
        "type": "mac",
        "identifier": "F7:19:18:29:93:1A"
    }],
    "manufacturer": "espressif",
    "model": "ESP32",
    "name": "Cabinet",
    "sw_version": "1.8.0"
  }
}

Please note that this only works with devices that supply a unique_id and only through MQTT discovery.

Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#6513

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line at end of file

vol.Optional(CONF_CONNECTIONS, default=list):
vol.All(cv.ensure_list, [vol.Schema({
vol.Required(CONF_TYPE): vol.Any(*device_registry.CONNECTION_KEYS),
vol.Required(CONF_IDENTIFIER): cv.string,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like the name of this option (identifier) in this context. Its name (base off of https://developers.home-assistant.io/docs/en/device_registry_index.html) is very close to the identifiers option but it has a totally different format. Should this be renamed to value or should it rather stick to the dev docs' identifier?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a dict to begin with?

Copy link
Member Author

@OttoWinter OttoWinter Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well primarily to make it more consistent with the device registry naming. One option would be:

{
  ...
  "device": {
    "connections": {
        "mac": "F7:19:18:29:93:1A",
        "other": "connection_id"
    }
  }
}

but that would for example not support devices with multiple MAC addresses (probably doesn't come up too often, but I think it's good to support it from the start to prevent breaking changes).

The other option would be:

{
  ...
  "device": {
    "connections": [
      {"mac": "F7:19:18:29:93:1A"},
      {"mac": "E8:2A:29:3A:A4:2B"}
    ]
  }
}

or:

{
  ...
  "device": {
    "connections": [
      ["mac", "F7:19:18:29:93:1A"],
      ["mac", "E8:2A:29:3A:A4:2B"]
    ]
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the last one. That is also how we store it internally.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, updated to that.

homeassistant/helpers/device_registry.py Outdated Show resolved Hide resolved
homeassistant/components/mqtt/__init__.py Show resolved Hide resolved
homeassistant/components/mqtt/__init__.py Show resolved Hide resolved
from homeassistant.core import Event, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, device_registry

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'homeassistant.helpers.device_registry' imported but unused

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

Successfully merging this pull request may close these issues.

6 participants