Add unique_id to MQTT switch#13719
Conversation
Added unique_id to MQTT Switch for easier discovery and firmware management
| 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): |
There was a problem hiding this comment.
line too long (101 > 79 characters)
|
Duplicate of #13318 |
|
You are totally right, sorry! |
|
The PR looks good. Please care about the lint issue and update the docs (new config parameter). |
|
Will also require a test. |
Pretty much a straight copy from tests/components/sensor/test_mqtt.py
|
|
||
| state = self.hass.states.get('switch.test') | ||
| self.assertEqual(STATE_ON, state.state) | ||
|
|
|
Unfortunately your introduced test is failing. |
Changed assert in test to use async_entity_ids instead of self.hass.states.all
|
@syssi Yeah, I see that - If I'm reading the log correctly, the test code I copied/modified from sensor If I change this assert to Sorry for the total noob questions. |
| 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 |
There was a problem hiding this comment.
block comment should start with '# '
|
Needs docs |
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:
tox. Your PR cannot be merged unless tests passIs 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:
REQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.This should not be necessary in this case.
If the code does not interact with devices:
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.