Restores switch state, case the switch is optimistic (#14151)

* Add restore_state to optimistic switch

* no need to schedule update

* test added

* lint

* new async syntax

* lint
This commit is contained in:
Diogo Gomes 2018-05-02 22:29:07 +01:00 committed by Paulus Schoutsen
parent 64b9fbd8d9
commit c851dfa2c7
2 changed files with 26 additions and 13 deletions

View file

@ -16,9 +16,10 @@ from homeassistant.components.mqtt import (
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__)
@ -112,6 +113,12 @@ class MqttSwitch(MqttAvailability, SwitchDevice):
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)
if last_state:
self._state = last_state.state == STATE_ON
@property
def should_poll(self):
"""Return the polling state."""