Add unique_id to MQTT fan (#16949)

This commit is contained in:
Otto Winter authored and Paulus Schoutsen committed 2018-09-29 21:22:57 +02:00
1 parent caaf4f5694
commit 45fdda3f5d
2 files changed
+39 -3

No files matched your search

+12 -1
View File
@@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation
https://home-assistant.io/components/fan.mqtt/
"""
import logging
from typing import Optional
import voluptuous as vol
@@ -41,6 +42,7 @@ 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'
CONF_UNIQUE_ID = 'unique_id'
DEFAULT_NAME = 'MQTT Fan'
DEFAULT_PAYLOAD_ON = 'ON'
@@ -74,6 +76,7 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
default=[SPEED_OFF, SPEED_LOW,
SPEED_MEDIUM, SPEED_HIGH]): cv.ensure_list,
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_UNIQUE_ID): cv.string,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
@@ -120,6 +123,7 @@ async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
config.get(CONF_AVAILABILITY_TOPIC),
config.get(CONF_PAYLOAD_AVAILABLE),
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
config.get(CONF_UNIQUE_ID),
discovery_hash,
)])
@@ -129,7 +133,8 @@ class MqttFan(MqttAvailability, MqttDiscoveryUpdate, FanEntity):
def __init__(self, name, topic, templates, qos, retain, payload,
speed_list, optimistic, availability_topic, payload_available,
payload_not_available, discovery_hash):
payload_not_available, unique_id: Optional[str],
discovery_hash):
"""Initialize the MQTT fan."""
MqttAvailability.__init__(self, availability_topic, qos,
payload_available, payload_not_available)
@@ -154,6 +159,7 @@ class MqttFan(MqttAvailability, MqttDiscoveryUpdate, FanEntity):
is not None and SUPPORT_OSCILLATE)
self._supported_features |= (topic[CONF_SPEED_STATE_TOPIC]
is not None and SUPPORT_SET_SPEED)
self._unique_id = unique_id
self._discovery_hash = discovery_hash
async def async_added_to_hass(self):
@@ -323,3 +329,8 @@ class MqttFan(MqttAvailability, MqttDiscoveryUpdate, FanEntity):
if self._optimistic_oscillation:
self._oscillation = oscillating
self.async_schedule_update_ha_state()
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id