2019-04-03 15:40:03 +00:00
|
|
|
"""Support for MQTT fans."""
|
2016-09-04 10:15:55 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-10-08 12:59:04 +00:00
|
|
|
from homeassistant.components import fan, mqtt
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.components.fan import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_SPEED,
|
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_OSCILLATE,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_OPTIMISTIC,
|
|
|
|
CONF_PAYLOAD_OFF,
|
|
|
|
CONF_PAYLOAD_ON,
|
|
|
|
CONF_STATE,
|
|
|
|
)
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DISCOVERY_HASH,
|
|
|
|
CONF_COMMAND_TOPIC,
|
|
|
|
CONF_QOS,
|
|
|
|
CONF_RETAIN,
|
|
|
|
CONF_STATE_TOPIC,
|
|
|
|
CONF_UNIQUE_ID,
|
|
|
|
MqttAttributes,
|
|
|
|
MqttAvailability,
|
|
|
|
MqttDiscoveryUpdate,
|
|
|
|
MqttEntityDeviceInfo,
|
|
|
|
subscription,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
from .discovery import MQTT_DISCOVERY_NEW, clear_discovery_hash
|
|
|
|
|
2016-09-04 10:15:55 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_STATE_VALUE_TEMPLATE = "state_value_template"
|
|
|
|
CONF_SPEED_STATE_TOPIC = "speed_state_topic"
|
|
|
|
CONF_SPEED_COMMAND_TOPIC = "speed_command_topic"
|
|
|
|
CONF_SPEED_VALUE_TEMPLATE = "speed_value_template"
|
|
|
|
CONF_OSCILLATION_STATE_TOPIC = "oscillation_state_topic"
|
|
|
|
CONF_OSCILLATION_COMMAND_TOPIC = "oscillation_command_topic"
|
|
|
|
CONF_OSCILLATION_VALUE_TEMPLATE = "oscillation_value_template"
|
|
|
|
CONF_PAYLOAD_OSCILLATION_ON = "payload_oscillation_on"
|
|
|
|
CONF_PAYLOAD_OSCILLATION_OFF = "payload_oscillation_off"
|
|
|
|
CONF_PAYLOAD_OFF_SPEED = "payload_off_speed"
|
|
|
|
CONF_PAYLOAD_LOW_SPEED = "payload_low_speed"
|
|
|
|
CONF_PAYLOAD_MEDIUM_SPEED = "payload_medium_speed"
|
|
|
|
CONF_PAYLOAD_HIGH_SPEED = "payload_high_speed"
|
|
|
|
CONF_SPEED_LIST = "speeds"
|
|
|
|
|
|
|
|
DEFAULT_NAME = "MQTT Fan"
|
|
|
|
DEFAULT_PAYLOAD_ON = "ON"
|
|
|
|
DEFAULT_PAYLOAD_OFF = "OFF"
|
2016-09-04 10:15:55 +00:00
|
|
|
DEFAULT_OPTIMISTIC = False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
OSCILLATE_ON_PAYLOAD = "oscillate_on"
|
|
|
|
OSCILLATE_OFF_PAYLOAD = "oscillate_off"
|
|
|
|
|
|
|
|
OSCILLATION = "oscillation"
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = (
|
|
|
|
mqtt.MQTT_RW_PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
|
|
|
vol.Optional(CONF_OSCILLATION_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
|
|
|
vol.Optional(CONF_OSCILLATION_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_OSCILLATION_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_PAYLOAD_HIGH_SPEED, default=SPEED_HIGH): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_LOW_SPEED, default=SPEED_LOW): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_MEDIUM_SPEED, default=SPEED_MEDIUM): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_OFF_SPEED, default=SPEED_OFF): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
|
|
|
|
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_PAYLOAD_OSCILLATION_OFF, default=OSCILLATE_OFF_PAYLOAD
|
|
|
|
): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_PAYLOAD_OSCILLATION_ON, default=OSCILLATE_ON_PAYLOAD
|
|
|
|
): cv.string,
|
|
|
|
vol.Optional(CONF_SPEED_COMMAND_TOPIC): mqtt.valid_publish_topic,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_SPEED_LIST,
|
|
|
|
default=[SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH],
|
|
|
|
): cv.ensure_list,
|
|
|
|
vol.Optional(CONF_SPEED_STATE_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_SPEED_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_STATE_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
|
|
|
|
.extend(mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
|
|
|
|
):
|
2018-10-08 12:59:04 +00:00
|
|
|
"""Set up MQTT fan through configuration.yaml."""
|
2019-01-08 15:51:03 +00:00
|
|
|
await _async_setup_entity(config, async_add_entities)
|
2017-09-17 09:32:22 +00:00
|
|
|
|
2018-09-25 17:32:31 +00:00
|
|
|
|
2018-10-08 12:59:04 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up MQTT fan dynamically through MQTT discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-10-08 12:59:04 +00:00
|
|
|
async def async_discover(discovery_payload):
|
|
|
|
"""Discover and add a MQTT fan."""
|
2019-01-08 15:51:03 +00:00
|
|
|
try:
|
2019-01-29 17:29:02 +00:00
|
|
|
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
|
2019-01-08 15:51:03 +00:00
|
|
|
config = PLATFORM_SCHEMA(discovery_payload)
|
2019-07-31 19:25:30 +00:00
|
|
|
await _async_setup_entity(
|
|
|
|
config, async_add_entities, config_entry, discovery_hash
|
|
|
|
)
|
2019-01-08 15:51:03 +00:00
|
|
|
except Exception:
|
|
|
|
if discovery_hash:
|
|
|
|
clear_discovery_hash(hass, discovery_hash)
|
|
|
|
raise
|
2018-10-08 12:59:04 +00:00
|
|
|
|
|
|
|
async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, MQTT_DISCOVERY_NEW.format(fan.DOMAIN, "mqtt"), async_discover
|
|
|
|
)
|
2018-10-08 12:59:04 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def _async_setup_entity(
|
|
|
|
config, async_add_entities, config_entry=None, discovery_hash=None
|
|
|
|
):
|
2018-10-08 12:59:04 +00:00
|
|
|
"""Set up the MQTT fan."""
|
2019-01-26 18:46:41 +00:00
|
|
|
async_add_entities([MqttFan(config, config_entry, discovery_hash)])
|
2018-11-27 10:22:55 +00:00
|
|
|
|
|
|
|
|
2019-01-21 00:42:17 +00:00
|
|
|
# pylint: disable=too-many-ancestors
|
2019-07-31 19:25:30 +00:00
|
|
|
class MqttFan(
|
|
|
|
MqttAttributes,
|
|
|
|
MqttAvailability,
|
|
|
|
MqttDiscoveryUpdate,
|
|
|
|
MqttEntityDeviceInfo,
|
|
|
|
FanEntity,
|
|
|
|
):
|
2018-11-27 10:22:55 +00:00
|
|
|
"""A MQTT fan component."""
|
|
|
|
|
2019-01-26 18:46:41 +00:00
|
|
|
def __init__(self, config, config_entry, discovery_hash):
|
2018-11-27 10:22:55 +00:00
|
|
|
"""Initialize the MQTT fan."""
|
2018-11-30 15:53:14 +00:00
|
|
|
self._unique_id = config.get(CONF_UNIQUE_ID)
|
2018-11-27 10:22:55 +00:00
|
|
|
self._state = False
|
|
|
|
self._speed = None
|
|
|
|
self._oscillation = None
|
|
|
|
self._supported_features = 0
|
|
|
|
self._sub_state = None
|
|
|
|
|
|
|
|
self._topic = None
|
|
|
|
self._payload = None
|
|
|
|
self._templates = None
|
|
|
|
self._optimistic = None
|
|
|
|
self._optimistic_oscillation = None
|
|
|
|
self._optimistic_speed = None
|
|
|
|
|
|
|
|
# Load config
|
|
|
|
self._setup_from_config(config)
|
|
|
|
|
|
|
|
device_config = config.get(CONF_DEVICE)
|
|
|
|
|
2019-01-21 00:42:17 +00:00
|
|
|
MqttAttributes.__init__(self, config)
|
2019-01-15 22:26:37 +00:00
|
|
|
MqttAvailability.__init__(self, config)
|
2019-07-31 19:25:30 +00:00
|
|
|
MqttDiscoveryUpdate.__init__(self, discovery_hash, self.discovery_update)
|
2019-01-26 18:46:41 +00:00
|
|
|
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
|
2018-11-27 10:22:55 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to MQTT events."""
|
2018-11-28 12:16:43 +00:00
|
|
|
await super().async_added_to_hass()
|
2018-11-27 10:22:55 +00:00
|
|
|
await self._subscribe_topics()
|
|
|
|
|
|
|
|
async def discovery_update(self, discovery_payload):
|
|
|
|
"""Handle updated discovery message."""
|
|
|
|
config = PLATFORM_SCHEMA(discovery_payload)
|
|
|
|
self._setup_from_config(config)
|
2019-01-21 00:42:17 +00:00
|
|
|
await self.attributes_discovery_update(config)
|
2018-11-27 10:22:55 +00:00
|
|
|
await self.availability_discovery_update(config)
|
2019-01-26 18:46:41 +00:00
|
|
|
await self.device_info_discovery_update(config)
|
2018-11-27 10:22:55 +00:00
|
|
|
await self._subscribe_topics()
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2018-11-27 10:22:55 +00:00
|
|
|
|
|
|
|
def _setup_from_config(self, config):
|
|
|
|
"""(Re)Setup the entity."""
|
2018-11-30 15:53:14 +00:00
|
|
|
self._config = config
|
2018-11-27 10:22:55 +00:00
|
|
|
self._topic = {
|
2019-07-31 19:25:30 +00:00
|
|
|
key: config.get(key)
|
|
|
|
for key in (
|
2016-09-04 10:15:55 +00:00
|
|
|
CONF_STATE_TOPIC,
|
|
|
|
CONF_COMMAND_TOPIC,
|
|
|
|
CONF_SPEED_STATE_TOPIC,
|
|
|
|
CONF_SPEED_COMMAND_TOPIC,
|
|
|
|
CONF_OSCILLATION_STATE_TOPIC,
|
|
|
|
CONF_OSCILLATION_COMMAND_TOPIC,
|
|
|
|
)
|
2018-11-27 10:22:55 +00:00
|
|
|
}
|
|
|
|
self._templates = {
|
2016-09-04 10:15:55 +00:00
|
|
|
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE),
|
|
|
|
ATTR_SPEED: config.get(CONF_SPEED_VALUE_TEMPLATE),
|
2019-07-31 19:25:30 +00:00
|
|
|
OSCILLATION: config.get(CONF_OSCILLATION_VALUE_TEMPLATE),
|
2018-11-27 10:22:55 +00:00
|
|
|
}
|
|
|
|
self._payload = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"STATE_ON": config[CONF_PAYLOAD_ON],
|
|
|
|
"STATE_OFF": config[CONF_PAYLOAD_OFF],
|
|
|
|
"OSCILLATE_ON_PAYLOAD": config[CONF_PAYLOAD_OSCILLATION_ON],
|
|
|
|
"OSCILLATE_OFF_PAYLOAD": config[CONF_PAYLOAD_OSCILLATION_OFF],
|
|
|
|
"SPEED_LOW": config[CONF_PAYLOAD_LOW_SPEED],
|
|
|
|
"SPEED_MEDIUM": config[CONF_PAYLOAD_MEDIUM_SPEED],
|
|
|
|
"SPEED_HIGH": config[CONF_PAYLOAD_HIGH_SPEED],
|
|
|
|
"SPEED_OFF": config[CONF_PAYLOAD_OFF_SPEED],
|
2018-11-27 10:22:55 +00:00
|
|
|
}
|
2019-04-07 14:08:47 +00:00
|
|
|
optimistic = config[CONF_OPTIMISTIC]
|
2018-11-27 10:22:55 +00:00
|
|
|
self._optimistic = optimistic or self._topic[CONF_STATE_TOPIC] is None
|
2016-09-11 08:01:46 +00:00
|
|
|
self._optimistic_oscillation = (
|
2019-07-31 19:25:30 +00:00
|
|
|
optimistic or self._topic[CONF_OSCILLATION_STATE_TOPIC] is None
|
|
|
|
)
|
2016-09-11 08:01:46 +00:00
|
|
|
self._optimistic_speed = (
|
2019-07-31 19:25:30 +00:00
|
|
|
optimistic or self._topic[CONF_SPEED_STATE_TOPIC] is None
|
|
|
|
)
|
2018-11-27 10:22:55 +00:00
|
|
|
|
2016-09-04 10:15:55 +00:00
|
|
|
self._supported_features = 0
|
2019-07-31 19:25:30 +00:00
|
|
|
self._supported_features |= (
|
|
|
|
self._topic[CONF_OSCILLATION_STATE_TOPIC] is not None and SUPPORT_OSCILLATE
|
|
|
|
)
|
|
|
|
self._supported_features |= (
|
|
|
|
self._topic[CONF_SPEED_STATE_TOPIC] is not None and SUPPORT_SET_SPEED
|
|
|
|
)
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2018-11-27 10:22:55 +00:00
|
|
|
async def _subscribe_topics(self):
|
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
topics = {}
|
2017-02-22 08:43:22 +00:00
|
|
|
templates = {}
|
|
|
|
for key, tpl in list(self._templates.items()):
|
2016-09-28 04:29:55 +00:00
|
|
|
if tpl is None:
|
|
|
|
templates[key] = lambda value: value
|
|
|
|
else:
|
2017-02-22 08:43:22 +00:00
|
|
|
tpl.hass = self.hass
|
|
|
|
templates[key] = tpl.async_render_with_possible_json_value
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2019-03-13 19:58:20 +00:00
|
|
|
def state_received(msg):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Handle new received MQTT message."""
|
2019-03-13 19:58:20 +00:00
|
|
|
payload = templates[CONF_STATE](msg.payload)
|
2019-07-31 19:25:30 +00:00
|
|
|
if payload == self._payload["STATE_ON"]:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._state = True
|
2019-07-31 19:25:30 +00:00
|
|
|
elif payload == self._payload["STATE_OFF"]:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._state = False
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2016-09-04 10:15:55 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_STATE_TOPIC] is not None:
|
2018-11-27 10:22:55 +00:00
|
|
|
topics[CONF_STATE_TOPIC] = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"topic": self._topic[CONF_STATE_TOPIC],
|
|
|
|
"msg_callback": state_received,
|
|
|
|
"qos": self._config[CONF_QOS],
|
|
|
|
}
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2019-03-13 19:58:20 +00:00
|
|
|
def speed_received(msg):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Handle new received MQTT message for the speed."""
|
2019-03-13 19:58:20 +00:00
|
|
|
payload = templates[ATTR_SPEED](msg.payload)
|
2019-07-31 19:25:30 +00:00
|
|
|
if payload == self._payload["SPEED_LOW"]:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._speed = SPEED_LOW
|
2019-07-31 19:25:30 +00:00
|
|
|
elif payload == self._payload["SPEED_MEDIUM"]:
|
2017-01-21 06:21:28 +00:00
|
|
|
self._speed = SPEED_MEDIUM
|
2019-07-31 19:25:30 +00:00
|
|
|
elif payload == self._payload["SPEED_HIGH"]:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._speed = SPEED_HIGH
|
2019-07-31 19:25:30 +00:00
|
|
|
elif payload == self._payload["SPEED_OFF"]:
|
2019-04-19 03:59:41 +00:00
|
|
|
self._speed = SPEED_OFF
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2016-09-04 10:15:55 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_SPEED_STATE_TOPIC] is not None:
|
2018-11-27 10:22:55 +00:00
|
|
|
topics[CONF_SPEED_STATE_TOPIC] = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"topic": self._topic[CONF_SPEED_STATE_TOPIC],
|
|
|
|
"msg_callback": speed_received,
|
|
|
|
"qos": self._config[CONF_QOS],
|
|
|
|
}
|
2017-10-27 14:04:18 +00:00
|
|
|
self._speed = SPEED_OFF
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2017-02-22 08:43:22 +00:00
|
|
|
@callback
|
2019-03-13 19:58:20 +00:00
|
|
|
def oscillation_received(msg):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Handle new received MQTT message for the oscillation."""
|
2019-03-13 19:58:20 +00:00
|
|
|
payload = templates[OSCILLATION](msg.payload)
|
2019-07-31 19:25:30 +00:00
|
|
|
if payload == self._payload["OSCILLATE_ON_PAYLOAD"]:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._oscillation = True
|
2019-07-31 19:25:30 +00:00
|
|
|
elif payload == self._payload["OSCILLATE_OFF_PAYLOAD"]:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._oscillation = False
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2016-09-04 10:15:55 +00:00
|
|
|
|
|
|
|
if self._topic[CONF_OSCILLATION_STATE_TOPIC] is not None:
|
2018-11-27 10:22:55 +00:00
|
|
|
topics[CONF_OSCILLATION_STATE_TOPIC] = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"topic": self._topic[CONF_OSCILLATION_STATE_TOPIC],
|
|
|
|
"msg_callback": oscillation_received,
|
|
|
|
"qos": self._config[CONF_QOS],
|
|
|
|
}
|
2017-10-27 14:04:18 +00:00
|
|
|
self._oscillation = False
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2018-11-27 10:22:55 +00:00
|
|
|
self._sub_state = await subscription.async_subscribe_topics(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._sub_state, topics
|
|
|
|
)
|
2018-11-27 10:22:55 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Unsubscribe when removed."""
|
2018-12-19 13:05:24 +00:00
|
|
|
self._sub_state = await subscription.async_unsubscribe_topics(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._sub_state
|
|
|
|
)
|
2019-01-21 00:42:17 +00:00
|
|
|
await MqttAttributes.async_will_remove_from_hass(self)
|
2018-11-27 10:22:55 +00:00
|
|
|
await MqttAvailability.async_will_remove_from_hass(self)
|
|
|
|
|
2016-09-04 10:15:55 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed for a MQTT fan."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return true if we do optimistic updates."""
|
|
|
|
return self._optimistic
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Get entity name."""
|
2019-04-07 14:08:47 +00:00
|
|
|
return self._config[CONF_NAME]
|
2016-09-04 10:15:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def speed_list(self) -> list:
|
|
|
|
"""Get the list of available speeds."""
|
2019-04-07 14:08:47 +00:00
|
|
|
return self._config[CONF_SPEED_LIST]
|
2016-09-04 10:15:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._supported_features
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed(self):
|
|
|
|
"""Return the current speed."""
|
|
|
|
return self._speed
|
|
|
|
|
|
|
|
@property
|
|
|
|
def oscillating(self):
|
|
|
|
"""Return the oscillation state."""
|
|
|
|
return self._oscillation
|
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_turn_on(self, speed: str = None, **kwargs) -> None:
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Turn on the entity.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
mqtt.async_publish(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
self._topic[CONF_COMMAND_TOPIC],
|
|
|
|
self._payload["STATE_ON"],
|
|
|
|
self._config[CONF_QOS],
|
|
|
|
self._config[CONF_RETAIN],
|
|
|
|
)
|
2017-01-23 21:20:54 +00:00
|
|
|
if speed:
|
2018-04-28 23:26:20 +00:00
|
|
|
await self.async_set_speed(speed)
|
2019-04-19 03:59:41 +00:00
|
|
|
if self._optimistic:
|
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Turn off the entity.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
mqtt.async_publish(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
self._topic[CONF_COMMAND_TOPIC],
|
|
|
|
self._payload["STATE_OFF"],
|
|
|
|
self._config[CONF_QOS],
|
|
|
|
self._config[CONF_RETAIN],
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
if self._optimistic:
|
|
|
|
self._state = False
|
|
|
|
self.async_write_ha_state()
|
2017-02-22 08:43:22 +00:00
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_set_speed(self, speed: str) -> None:
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Set the speed of the fan.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
if self._topic[CONF_SPEED_COMMAND_TOPIC] is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if speed == SPEED_LOW:
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_payload = self._payload["SPEED_LOW"]
|
2017-02-22 08:43:22 +00:00
|
|
|
elif speed == SPEED_MEDIUM:
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_payload = self._payload["SPEED_MEDIUM"]
|
2017-02-22 08:43:22 +00:00
|
|
|
elif speed == SPEED_HIGH:
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_payload = self._payload["SPEED_HIGH"]
|
2019-04-19 03:59:41 +00:00
|
|
|
elif speed == SPEED_OFF:
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_payload = self._payload["SPEED_OFF"]
|
2017-02-22 08:43:22 +00:00
|
|
|
else:
|
|
|
|
mqtt_payload = speed
|
|
|
|
|
|
|
|
mqtt.async_publish(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
self._topic[CONF_SPEED_COMMAND_TOPIC],
|
|
|
|
mqtt_payload,
|
|
|
|
self._config[CONF_QOS],
|
|
|
|
self._config[CONF_RETAIN],
|
|
|
|
)
|
2017-02-22 08:43:22 +00:00
|
|
|
|
|
|
|
if self._optimistic_speed:
|
2016-09-04 10:15:55 +00:00
|
|
|
self._speed = speed
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2016-09-04 10:15:55 +00:00
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_oscillate(self, oscillating: bool) -> None:
|
2017-02-22 08:43:22 +00:00
|
|
|
"""Set oscillation.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
if self._topic[CONF_OSCILLATION_COMMAND_TOPIC] is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if oscillating is False:
|
2019-07-31 19:25:30 +00:00
|
|
|
payload = self._payload["OSCILLATE_OFF_PAYLOAD"]
|
2017-02-22 08:43:22 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
payload = self._payload["OSCILLATE_ON_PAYLOAD"]
|
2017-02-22 08:43:22 +00:00
|
|
|
|
|
|
|
mqtt.async_publish(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
self._topic[CONF_OSCILLATION_COMMAND_TOPIC],
|
|
|
|
payload,
|
|
|
|
self._config[CONF_QOS],
|
|
|
|
self._config[CONF_RETAIN],
|
|
|
|
)
|
2017-02-22 08:43:22 +00:00
|
|
|
|
|
|
|
if self._optimistic_oscillation:
|
|
|
|
self._oscillation = oscillating
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2018-09-29 19:22:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|