-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Restores switch state, case the switch is optimistic #14151
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/switch.mqtt/ | ||
| """ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
@@ -17,9 +16,10 @@ | |
| from homeassistant.components.switch import SwitchDevice | ||
| from homeassistant.const import ( | ||
| CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF, | ||
| CONF_PAYLOAD_ON, CONF_ICON) | ||
| CONF_PAYLOAD_ON, CONF_ICON, STATE_ON) | ||
| import homeassistant.components.mqtt as mqtt | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.restore_state import async_get_last_state | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -39,8 +39,7 @@ | |
| }).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema) | ||
|
|
||
|
|
||
| @asyncio.coroutine | ||
| def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | ||
| async def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | ||
| """Set up the MQTT switch.""" | ||
| if discovery_info is not None: | ||
| config = PLATFORM_SCHEMA(discovery_info) | ||
|
|
@@ -88,10 +87,9 @@ def __init__(self, name, icon, | |
| self._optimistic = optimistic | ||
| self._template = value_template | ||
|
|
||
| @asyncio.coroutine | ||
| def async_added_to_hass(self): | ||
| async def async_added_to_hass(self): | ||
| """Subscribe to MQTT events.""" | ||
| yield from super().async_added_to_hass() | ||
| await super().async_added_to_hass() | ||
|
|
||
| @callback | ||
| def state_message_received(topic, payload, qos): | ||
|
|
@@ -110,10 +108,16 @@ def state_message_received(topic, payload, qos): | |
| # Force into optimistic mode. | ||
| self._optimistic = True | ||
| else: | ||
| yield from mqtt.async_subscribe( | ||
| await mqtt.async_subscribe( | ||
| self.hass, self._state_topic, state_message_received, | ||
| self._qos) | ||
|
|
||
| if self._optimistic: | ||
| last_state = await async_get_last_state(self.hass, | ||
| self.entity_id) | ||
|
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. continuation line over-indented for visual indent |
||
| if last_state: | ||
| self._state = last_state.state == STATE_ON | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """Return the polling state.""" | ||
|
|
@@ -139,8 +143,7 @@ def icon(self): | |
| """Return the icon.""" | ||
| return self._icon | ||
|
|
||
| @asyncio.coroutine | ||
| def async_turn_on(self, **kwargs): | ||
| async def async_turn_on(self, **kwargs): | ||
| """Turn the device on. | ||
|
|
||
| This method is a coroutine. | ||
|
|
@@ -153,8 +156,7 @@ def async_turn_on(self, **kwargs): | |
| self._state = True | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| @asyncio.coroutine | ||
| def async_turn_off(self, **kwargs): | ||
| async def async_turn_off(self, **kwargs): | ||
| """Turn the device off. | ||
|
|
||
| This method is a coroutine. | ||
|
|
||
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
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.
line too long (85 > 79 characters)