Skip to content

Add unique_id to MQTT switch#13719

Merged
syssi merged 9 commits intohome-assistant:devfrom
thepotoo:dev
May 6, 2018
Merged

Add unique_id to MQTT switch#13719
syssi merged 9 commits intohome-assistant:devfrom
thepotoo:dev

Conversation

@thepotoo
Copy link
Copy Markdown
Contributor

@thepotoo thepotoo commented Apr 6, 2018

Description:

Add unique_id to MQTT switch for use with discovery. This enables easier management of large numbers of devices using discovery.

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

This feature is helpful precisely because you don't need to edit configuration.yaml; you can use discovery for almost all devices instead.

Here is an example json discovery topic that works:
{"state_topic":"homeassistant/relay/fishroom/520E4E","unique_id":"relay_520E4E","command_topic":"homeassistant/fishroom/520E4E","availability_topic":"homeassistant/online/fishroom/520E4E","payload_available": "online","payload_not_available": "offline","payload_on":"on","payload_off":"off","optimistic":"false","assumed_state":"false","qos":"0","retain":"true"}

On the backend (in the Arduino IDE), the unique_id is generated using switch_type (in this case, 'relay') + ESP.getChipId();

As long as each ESP only controls one switch, the id will be truly unique.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
    Is there a guideline for what to use for tests? I'm happy to put something together, but I'm not really sure where to start.

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

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

This should not be necessary in this case.

If the code does not interact with devices:

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

Is there a guideline for what to use for tests? I'm happy to put something together, but I'm not really sure where to start.

thepotoo added 2 commits April 6, 2018 10:24
Added unique_id to MQTT Switch for easier discovery and firmware management
@homeassistant
Copy link
Copy Markdown
Contributor

Hi @thepotoo,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Comment thread homeassistant/components/switch/mqtt.py Outdated
state_topic, command_topic, availability_topic,
qos, retain, payload_on, payload_off, optimistic,
payload_available, payload_not_available, value_template):
payload_available, payload_not_available, unique_id: Optional[str], value_template):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (101 > 79 characters)

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 6, 2018

Duplicate of #13318

@syssi syssi marked this as a duplicate of #13318 Apr 6, 2018
@thepotoo
Copy link
Copy Markdown
Contributor Author

thepotoo commented Apr 6, 2018

@syssi This isn't a duplicate - #13318 is for the mqtt sensor, this one is for a mqtt switch.

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 7, 2018

You are totally right, sorry!

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 7, 2018

The PR looks good. Please care about the lint issue and update the docs (new config parameter).

@fabaff
Copy link
Copy Markdown
Member

fabaff commented Apr 7, 2018

Will also require a test.

Pretty much a straight copy from tests/components/sensor/test_mqtt.py
Comment thread tests/components/switch/test_mqtt.py Outdated

state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 8, 2018

Unfortunately your introduced test is failing.

thepotoo added 2 commits April 8, 2018 16:02
Changed assert in test to use async_entity_ids instead of self.hass.states.all
@thepotoo
Copy link
Copy Markdown
Contributor Author

thepotoo commented Apr 8, 2018

@syssi Yeah, I see that - If I'm reading the log correctly, the test code I copied/modified from sensor assert len(self.hass.states.all()) == 1 is failing because the switches have groups and sensors don't (?)

If I change this assert to assert len(self.hass.states.all()) == 2, would that make this a valid test? I tried changing it to async_entity_ids instead of all, but that still picked up group.all_switches.

Sorry for the total noob questions.

Comment thread tests/components/switch/test_mqtt.py Outdated
fire_mqtt_message(self.hass, 'test-topic', 'payload')
self.hass.block_till_done()
assert len(self.hass.states.async_entity_ids()) == 2
#all switches group is 1, unique id created is 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

block comment should start with '# '

Copy link
Copy Markdown
Contributor

@dgomes dgomes left a comment

Choose a reason for hiding this comment

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

LGTM

@syssi syssi merged commit 2bb1a95 into home-assistant:dev May 6, 2018
@OttoWinter
Copy link
Copy Markdown
Member

Needs docs

@balloob balloob mentioned this pull request May 28, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants