-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Deduplicate code in MQTT basic light pt1: Add add_topic helper #50759
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
Merged
emontnemery
merged 1 commit into
home-assistant:dev
from
emontnemery:mqtt_basic_light_refactor_1
May 17, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -256,6 +256,15 @@ async def _subscribe_topics(self): # noqa: C901 | |
|
|
||
| last_state = await self.async_get_last_state() | ||
|
|
||
| def add_topic(topic, msg_callback): | ||
| """Add a topic.""" | ||
| if self._topic[topic] is not None: | ||
| topics[topic] = { | ||
| "topic": self._topic[topic], | ||
| "msg_callback": msg_callback, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
|
|
||
| @callback | ||
| @log_messages(self.hass, self.entity_id) | ||
| def state_received(msg): | ||
|
|
@@ -298,13 +307,8 @@ def brightness_received(msg): | |
| self._brightness = percent_bright * 255 | ||
| self.async_write_ha_state() | ||
|
|
||
| if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None: | ||
| topics[CONF_BRIGHTNESS_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_BRIGHTNESS_STATE_TOPIC], | ||
| "msg_callback": brightness_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| elif ( | ||
| add_topic(CONF_BRIGHTNESS_STATE_TOPIC, brightness_received) | ||
| if ( | ||
|
MartinHjelmare marked this conversation as resolved.
|
||
| self._optimistic_brightness | ||
| and last_state | ||
| and last_state.attributes.get(ATTR_BRIGHTNESS) | ||
|
|
@@ -327,12 +331,7 @@ def rgb_received(msg): | |
| self._brightness = percent_bright * 255 | ||
| self.async_write_ha_state() | ||
|
|
||
| if self._topic[CONF_RGB_STATE_TOPIC] is not None: | ||
| topics[CONF_RGB_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_RGB_STATE_TOPIC], | ||
| "msg_callback": rgb_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| add_topic(CONF_RGB_STATE_TOPIC, rgb_received) | ||
| if ( | ||
| self._optimistic_rgb | ||
| and last_state | ||
|
|
@@ -354,12 +353,7 @@ def color_temp_received(msg): | |
| self._color_temp = int(payload) | ||
| self.async_write_ha_state() | ||
|
|
||
| if self._topic[CONF_COLOR_TEMP_STATE_TOPIC] is not None: | ||
| topics[CONF_COLOR_TEMP_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_COLOR_TEMP_STATE_TOPIC], | ||
| "msg_callback": color_temp_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| add_topic(CONF_COLOR_TEMP_STATE_TOPIC, color_temp_received) | ||
| if ( | ||
| self._optimistic_color_temp | ||
| and last_state | ||
|
|
@@ -381,12 +375,7 @@ def effect_received(msg): | |
| self._effect = payload | ||
| self.async_write_ha_state() | ||
|
|
||
| if self._topic[CONF_EFFECT_STATE_TOPIC] is not None: | ||
| topics[CONF_EFFECT_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_EFFECT_STATE_TOPIC], | ||
| "msg_callback": effect_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| add_topic(CONF_EFFECT_STATE_TOPIC, effect_received) | ||
| if ( | ||
| self._optimistic_effect | ||
| and last_state | ||
|
|
@@ -410,12 +399,7 @@ def hs_received(msg): | |
| except ValueError: | ||
| _LOGGER.debug("Failed to parse hs state update: '%s'", payload) | ||
|
|
||
| if self._topic[CONF_HS_STATE_TOPIC] is not None: | ||
| topics[CONF_HS_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_HS_STATE_TOPIC], | ||
| "msg_callback": hs_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| add_topic(CONF_HS_STATE_TOPIC, hs_received) | ||
| if ( | ||
| self._optimistic_hs | ||
| and last_state | ||
|
|
@@ -439,13 +423,8 @@ def white_value_received(msg): | |
| self._white_value = percent_white * 255 | ||
| self.async_write_ha_state() | ||
|
|
||
| if self._topic[CONF_WHITE_VALUE_STATE_TOPIC] is not None: | ||
| topics[CONF_WHITE_VALUE_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_WHITE_VALUE_STATE_TOPIC], | ||
| "msg_callback": white_value_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| elif ( | ||
| add_topic(CONF_WHITE_VALUE_STATE_TOPIC, white_value_received) | ||
| if ( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
MartinHjelmare marked this conversation as resolved.
|
||
| self._optimistic_white_value | ||
| and last_state | ||
| and last_state.attributes.get(ATTR_WHITE_VALUE) | ||
|
|
@@ -465,12 +444,7 @@ def xy_received(msg): | |
| self._hs = color_util.color_xy_to_hs(*xy_color) | ||
| self.async_write_ha_state() | ||
|
|
||
| if self._topic[CONF_XY_STATE_TOPIC] is not None: | ||
| topics[CONF_XY_STATE_TOPIC] = { | ||
| "topic": self._topic[CONF_XY_STATE_TOPIC], | ||
| "msg_callback": xy_received, | ||
| "qos": self._config[CONF_QOS], | ||
| } | ||
| add_topic(CONF_XY_STATE_TOPIC, xy_received) | ||
| if ( | ||
| self._optimistic_xy | ||
| and last_state | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
elifwas not correct, we should restore the state ifself._optimistic_brightnessisTrue