Update Wemo state when changing state (#36647)

This commit is contained in:
Adam Belebczuk 2020-06-16 15:14:04 -04:00 committed by GitHub
parent a6a6a7b69c
commit fe03c9da68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 9 deletions

View File

@ -457,7 +457,6 @@ homeassistant/components/watson_tts/* @rutkai
homeassistant/components/weather/* @fabaff
homeassistant/components/webostv/* @bendavid
homeassistant/components/websocket_api/* @home-assistant/core
homeassistant/components/wemo/* @sqldiablo
homeassistant/components/wiffi/* @mampfes
homeassistant/components/withings/* @vangorra
homeassistant/components/wled/* @frenck

View File

@ -304,6 +304,8 @@ class WemoHumidifier(FanEntity):
else:
self.set_speed(speed)
self.schedule_update_ha_state()
def turn_off(self, **kwargs) -> None:
"""Turn the switch off."""
try:
@ -312,6 +314,8 @@ class WemoHumidifier(FanEntity):
_LOGGER.warning("Error while turning off device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
def set_speed(self, speed: str) -> None:
"""Set the fan_mode of the Humidifier."""
try:
@ -322,6 +326,8 @@ class WemoHumidifier(FanEntity):
)
self._available = False
self.schedule_update_ha_state()
def set_humidity(self, humidity: float) -> None:
"""Set the target humidity level for the Humidifier."""
if humidity < 50:
@ -343,6 +349,8 @@ class WemoHumidifier(FanEntity):
)
self._available = False
self.schedule_update_ha_state()
def reset_filter_life(self) -> None:
"""Reset the filter life to 100%."""
try:
@ -352,3 +360,5 @@ class WemoHumidifier(FanEntity):
"Error while resetting filter life on device: %s (%s)", self.name, err
)
self._available = False
self.schedule_update_ha_state()

View File

@ -32,6 +32,10 @@ SUPPORT_WEMO = (
SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_COLOR | SUPPORT_TRANSITION
)
# The WEMO_ constants below come from pywemo itself
WEMO_ON = 1
WEMO_OFF = 0
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up WeMo lights."""
@ -175,21 +179,27 @@ class WemoLight(LightEntity):
if color_temp is not None:
self.wemo.set_temperature(mireds=color_temp, transition=transition_time)
self.wemo.turn_on(**turn_on_kwargs)
if self.wemo.turn_on(**turn_on_kwargs):
self._state["onoff"] = WEMO_ON
except ActionException as err:
_LOGGER.warning("Error while turning on device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn the light off."""
transition_time = int(kwargs.get(ATTR_TRANSITION, 0))
try:
self.wemo.turn_off(transition=transition_time)
if self.wemo.turn_off(transition=transition_time):
self._state["onoff"] = WEMO_OFF
except ActionException as err:
_LOGGER.warning("Error while turning off device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
def _update(self, force_update=True):
"""Synchronize state with bridge."""
try:
@ -200,7 +210,7 @@ class WemoLight(LightEntity):
self._available = False
self.wemo.reconnect_with_device()
else:
self._is_on = self._state.get("onoff") != 0
self._is_on = self._state.get("onoff") != WEMO_OFF
self._brightness = self._state.get("level", 255)
self._color_temp = self._state.get("temperature_mireds")
self._available = True
@ -355,20 +365,27 @@ class WemoDimmer(LightEntity):
brightness = 255
try:
self.wemo.on()
if self.wemo.on():
self._state = WEMO_ON
self.wemo.set_brightness(brightness)
except ActionException as err:
_LOGGER.warning("Error while turning on device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn the dimmer off."""
try:
self.wemo.off()
if self.wemo.off():
self._state = WEMO_OFF
except ActionException as err:
_LOGGER.warning("Error while turning on device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
@property
def available(self):
"""Return if dimmer is available."""

View File

@ -12,5 +12,5 @@
"homekit": {
"models": ["Wemo"]
},
"codeowners": ["@sqldiablo"]
"codeowners": []
}

View File

@ -18,6 +18,7 @@ PARALLEL_UPDATES = 0
_LOGGER = logging.getLogger(__name__)
# The WEMO_ constants below come from pywemo itself
ATTR_SENSOR_STATE = "sensor_state"
ATTR_SWITCH_MODE = "switch_mode"
ATTR_CURRENT_STATE_DETAIL = "state_detail"
@ -191,19 +192,25 @@ class WemoSwitch(SwitchEntity):
def turn_on(self, **kwargs):
"""Turn the switch on."""
try:
self.wemo.on()
if self.wemo.on():
self._state = WEMO_ON
except ActionException as err:
_LOGGER.warning("Error while turning on device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn the switch off."""
try:
self.wemo.off()
if self.wemo.off():
self._state = WEMO_OFF
except ActionException as err:
_LOGGER.warning("Error while turning off device %s (%s)", self.name, err)
self._available = False
self.schedule_update_ha_state()
async def async_added_to_hass(self):
"""Wemo switch added to Home Assistant."""
# Define inside async context so we know our event loop