Fix for exception when checking if fan without speed is on (#39096)
* Fix for exception when checking if fan without speed is on * Organized imports * Space
This commit is contained in:
parent
00b05d764d
commit
2ec546db3d
2 changed files with 17 additions and 8 deletions
|
@ -6,7 +6,12 @@ from typing import Optional
|
||||||
|
|
||||||
import voluptuous as vol
|
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.config_validation import ( # noqa: F401
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
|
@ -50,7 +55,9 @@ ATTR_DIRECTION = "direction"
|
||||||
def is_on(hass, entity_id: str) -> bool:
|
def is_on(hass, entity_id: str) -> bool:
|
||||||
"""Return if the fans are on based on the statemachine."""
|
"""Return if the fans are on based on the statemachine."""
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
|
if ATTR_SPEED in state.attributes:
|
||||||
return state.attributes[ATTR_SPEED] not in [SPEED_OFF, None]
|
return state.attributes[ATTR_SPEED] not in [SPEED_OFF, None]
|
||||||
|
return state.state == STATE_ON
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass, config: dict):
|
async def async_setup(hass, config: dict):
|
||||||
|
|
|
@ -22,6 +22,8 @@ from homeassistant.const import (
|
||||||
CONF_PAYLOAD_ON,
|
CONF_PAYLOAD_ON,
|
||||||
CONF_STATE,
|
CONF_STATE,
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
|
STATE_OFF,
|
||||||
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
@ -153,7 +155,7 @@ class MqttFan(
|
||||||
def __init__(self, config, config_entry, discovery_data):
|
def __init__(self, config, config_entry, discovery_data):
|
||||||
"""Initialize the MQTT fan."""
|
"""Initialize the MQTT fan."""
|
||||||
self._unique_id = config.get(CONF_UNIQUE_ID)
|
self._unique_id = config.get(CONF_UNIQUE_ID)
|
||||||
self._state = False
|
self._state = STATE_OFF
|
||||||
self._speed = None
|
self._speed = None
|
||||||
self._oscillation = None
|
self._oscillation = None
|
||||||
self._supported_features = 0
|
self._supported_features = 0
|
||||||
|
@ -255,9 +257,9 @@ class MqttFan(
|
||||||
"""Handle new received MQTT message."""
|
"""Handle new received MQTT message."""
|
||||||
payload = templates[CONF_STATE](msg.payload)
|
payload = templates[CONF_STATE](msg.payload)
|
||||||
if payload == self._payload["STATE_ON"]:
|
if payload == self._payload["STATE_ON"]:
|
||||||
self._state = True
|
self._state = STATE_ON
|
||||||
elif payload == self._payload["STATE_OFF"]:
|
elif payload == self._payload["STATE_OFF"]:
|
||||||
self._state = False
|
self._state = STATE_OFF
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
if self._topic[CONF_STATE_TOPIC] is not None:
|
if self._topic[CONF_STATE_TOPIC] is not None:
|
||||||
|
@ -335,7 +337,7 @@ class MqttFan(
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return self._state
|
return self._state == STATE_ON
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
@ -377,7 +379,7 @@ class MqttFan(
|
||||||
if speed:
|
if speed:
|
||||||
await self.async_set_speed(speed)
|
await self.async_set_speed(speed)
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._state = True
|
self._state = STATE_ON
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs) -> None:
|
async def async_turn_off(self, **kwargs) -> None:
|
||||||
|
@ -393,7 +395,7 @@ class MqttFan(
|
||||||
self._config[CONF_RETAIN],
|
self._config[CONF_RETAIN],
|
||||||
)
|
)
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._state = False
|
self._state = STATE_OFF
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_set_speed(self, speed: str) -> None:
|
async def async_set_speed(self, speed: str) -> None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue