Skip to content

Commit

Permalink
Fix for exception when checking if fan without speed is on (home-assi…
Browse files Browse the repository at this point in the history
…stant#39096)

* Fix for exception when checking if fan without speed is on

* Organized imports

* Space
  • Loading branch information
kbickar authored and leikoilja committed Sep 6, 2020
1 parent 6c6b7d7 commit 3ef44ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 9 additions & 2 deletions homeassistant/components/fan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

import voluptuous as vol

from homeassistant.const import SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.const import (
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_ON,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA,
Expand Down Expand Up @@ -50,7 +55,9 @@
def is_on(hass, entity_id: str) -> bool:
"""Return if the fans are on based on the statemachine."""
state = hass.states.get(entity_id)
return state.attributes[ATTR_SPEED] not in [SPEED_OFF, None]
if ATTR_SPEED in state.attributes:
return state.attributes[ATTR_SPEED] not in [SPEED_OFF, None]
return state.state == STATE_ON


async def async_setup(hass, config: dict):
Expand Down
14 changes: 8 additions & 6 deletions homeassistant/components/mqtt/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
CONF_PAYLOAD_ON,
CONF_STATE,
CONF_UNIQUE_ID,
STATE_OFF,
STATE_ON,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -153,7 +155,7 @@ class MqttFan(
def __init__(self, config, config_entry, discovery_data):
"""Initialize the MQTT fan."""
self._unique_id = config.get(CONF_UNIQUE_ID)
self._state = False
self._state = STATE_OFF
self._speed = None
self._oscillation = None
self._supported_features = 0
Expand Down Expand Up @@ -255,9 +257,9 @@ def state_received(msg):
"""Handle new received MQTT message."""
payload = templates[CONF_STATE](msg.payload)
if payload == self._payload["STATE_ON"]:
self._state = True
self._state = STATE_ON
elif payload == self._payload["STATE_OFF"]:
self._state = False
self._state = STATE_OFF
self.async_write_ha_state()

if self._topic[CONF_STATE_TOPIC] is not None:
Expand Down Expand Up @@ -335,7 +337,7 @@ def assumed_state(self):
@property
def is_on(self):
"""Return true if device is on."""
return self._state
return self._state == STATE_ON

@property
def name(self) -> str:
Expand Down Expand Up @@ -377,7 +379,7 @@ async def async_turn_on(self, speed: str = None, **kwargs) -> None:
if speed:
await self.async_set_speed(speed)
if self._optimistic:
self._state = True
self._state = STATE_ON
self.async_write_ha_state()

async def async_turn_off(self, **kwargs) -> None:
Expand All @@ -393,7 +395,7 @@ async def async_turn_off(self, **kwargs) -> None:
self._config[CONF_RETAIN],
)
if self._optimistic:
self._state = False
self._state = STATE_OFF
self.async_write_ha_state()

async def async_set_speed(self, speed: str) -> None:
Expand Down

0 comments on commit 3ef44ae

Please sign in to comment.