Add config flow support for fan
This commit is contained in:
@@ -20,4 +20,4 @@ CONF_STOP_CMD = 'stop_cmd'
|
|||||||
DOMAIN = "localtuya"
|
DOMAIN = "localtuya"
|
||||||
|
|
||||||
# Platforms in this list must support config flows
|
# Platforms in this list must support config flows
|
||||||
PLATFORMS = ["cover", "light", "switch"]
|
PLATFORMS = ["cover", "fan", "light", "switch"]
|
||||||
|
@@ -15,78 +15,68 @@ fan:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.fan import (SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH,
|
from homeassistant.components.fan import (
|
||||||
FanEntity, SUPPORT_SET_SPEED,
|
SPEED_LOW,
|
||||||
SUPPORT_OSCILLATE, SUPPORT_DIRECTION, PLATFORM_SCHEMA)
|
SPEED_MEDIUM,
|
||||||
|
SPEED_HIGH,
|
||||||
from homeassistant.const import STATE_OFF
|
FanEntity,
|
||||||
"""from . import DATA_TUYA, TuyaDevice"""
|
SUPPORT_SET_SPEED,
|
||||||
from homeassistant.const import (CONF_HOST, CONF_ID, CONF_FRIENDLY_NAME, CONF_ICON, CONF_NAME)
|
SUPPORT_OSCILLATE,
|
||||||
|
SUPPORT_DIRECTION,
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
|
)
|
||||||
|
from homeassistant.const import CONF_ID, CONF_FRIENDLY_NAME, STATE_OFF
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
from . import BASE_PLATFORM_SCHEMA, prepare_setup_entities, import_from_yaml
|
||||||
|
from .pytuya import FanDevice
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = 'localtuyafan'
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(BASE_PLATFORM_SCHEMA)
|
||||||
|
|
||||||
REQUIREMENTS = ['pytuya==7.0.9']
|
|
||||||
|
|
||||||
CONF_DEVICE_ID = 'device_id'
|
|
||||||
CONF_LOCAL_KEY = 'local_key'
|
|
||||||
CONF_PROTOCOL_VERSION = 'protocol_version'
|
|
||||||
|
|
||||||
DEFAULT_ID = '1'
|
|
||||||
DEFAULT_PROTOCOL_VERSION = 3.3
|
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
def flow_schema(dps):
|
||||||
vol.Optional(CONF_ICON): cv.icon,
|
"""Return schema used in config flow."""
|
||||||
vol.Required(CONF_HOST): cv.string,
|
return {}
|
||||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
|
||||||
vol.Required(CONF_LOCAL_KEY): cv.string,
|
|
||||||
vol.Required(CONF_NAME): cv.string,
|
|
||||||
vol.Required(CONF_FRIENDLY_NAME): cv.string,
|
|
||||||
vol.Required(CONF_PROTOCOL_VERSION, default=DEFAULT_PROTOCOL_VERSION): vol.Coerce(float),
|
|
||||||
vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up of the Tuya switch."""
|
"""Setup a Tuya fan based on a config entry."""
|
||||||
from . import pytuya
|
device, entities_to_setup = prepare_setup_entities(config_entry, "fan", FanDevice)
|
||||||
fans = []
|
if not entities_to_setup:
|
||||||
localtuyadevice = pytuya.FanDevice(config.get(CONF_DEVICE_ID), config.get(CONF_HOST), config.get(CONF_LOCAL_KEY))
|
return
|
||||||
localtuyadevice.set_version(float(config.get(CONF_PROTOCOL_VERSION)))
|
|
||||||
_LOGGER.debug("localtuya fan: setup_platform: %s", localtuyadevice)
|
|
||||||
|
|
||||||
fan_device = localtuyadevice
|
lights = []
|
||||||
fans.append(
|
for device_config in entities_to_setup:
|
||||||
|
lights.append(
|
||||||
TuyaDevice(
|
TuyaDevice(
|
||||||
fan_device,
|
device,
|
||||||
config.get(CONF_NAME),
|
device_config[CONF_FRIENDLY_NAME],
|
||||||
config.get(CONF_FRIENDLY_NAME),
|
device_config[CONF_ID],
|
||||||
config.get(CONF_ICON),
|
|
||||||
config.get(CONF_ID),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
_LOGGER.info("Setup localtuya fan %s with device ID %s ", config.get(CONF_FRIENDLY_NAME), config.get(CONF_DEVICE_ID) )
|
|
||||||
|
|
||||||
add_entities(fans, True)
|
async_add_entities(lights, True)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
"""Set up of the Tuya fan."""
|
||||||
|
return import_from_yaml(hass, config, "fan")
|
||||||
|
|
||||||
|
|
||||||
class TuyaDevice(FanEntity):
|
class TuyaDevice(FanEntity):
|
||||||
"""A demonstration fan component."""
|
"""A demonstration fan component."""
|
||||||
|
|
||||||
# def __init__(self, hass, name: str, supported_features: int) -> None:
|
def __init__(self, device, friendly_name, switchid):
|
||||||
def __init__(self, device, name, friendly_name, icon, switchid):
|
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._name = friendly_name
|
self._name = friendly_name
|
||||||
self._available = False
|
self._available = False
|
||||||
self._friendly_name = friendly_name
|
self._friendly_name = friendly_name
|
||||||
self._icon = icon
|
|
||||||
self._switch_id = switchid
|
self._switch_id = switchid
|
||||||
self._status = self._device.status()
|
self._status = self._device.status()
|
||||||
self._state = False
|
self._state = False
|
||||||
@@ -137,7 +127,7 @@ class TuyaDevice(FanEntity):
|
|||||||
def turn_off(self, **kwargs) -> None:
|
def turn_off(self, **kwargs) -> None:
|
||||||
"""Turn off the entity."""
|
"""Turn off the entity."""
|
||||||
_LOGGER.debug("localtuya fan: turn_off")
|
_LOGGER.debug("localtuya fan: turn_off")
|
||||||
self._device.set_status(False, '1')
|
self._device.set_status(False, "1")
|
||||||
self._state = False
|
self._state = False
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
@@ -147,14 +137,14 @@ class TuyaDevice(FanEntity):
|
|||||||
self._speed = speed
|
self._speed = speed
|
||||||
# self.schedule_update_ha_state()
|
# self.schedule_update_ha_state()
|
||||||
if speed == STATE_OFF:
|
if speed == STATE_OFF:
|
||||||
self._device.set_status(False, '1')
|
self._device.set_status(False, "1")
|
||||||
self._state = False
|
self._state = False
|
||||||
elif speed == SPEED_LOW:
|
elif speed == SPEED_LOW:
|
||||||
self._device.set_value('2', '1')
|
self._device.set_value("2", "1")
|
||||||
elif speed == SPEED_MEDIUM:
|
elif speed == SPEED_MEDIUM:
|
||||||
self._device.set_value('2', '2')
|
self._device.set_value("2", "2")
|
||||||
elif speed == SPEED_HIGH:
|
elif speed == SPEED_HIGH:
|
||||||
self._device.set_value('2', '3')
|
self._device.set_value("2", "3")
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
# def set_direction(self, direction: str) -> None:
|
# def set_direction(self, direction: str) -> None:
|
||||||
@@ -165,7 +155,7 @@ class TuyaDevice(FanEntity):
|
|||||||
def oscillate(self, oscillating: bool) -> None:
|
def oscillate(self, oscillating: bool) -> None:
|
||||||
"""Set oscillation."""
|
"""Set oscillation."""
|
||||||
self._oscillating = oscillating
|
self._oscillating = oscillating
|
||||||
self._device.set_value('8', oscillating)
|
self._device.set_value("8", oscillating)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
# @property
|
# @property
|
||||||
@@ -176,8 +166,7 @@ class TuyaDevice(FanEntity):
|
|||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return unique device identifier."""
|
"""Return unique device identifier."""
|
||||||
_LOGGER.debug("localtuya fan unique_id = %s", self._device)
|
return f"local_{self._device.id}_{self._switch_id}"
|
||||||
return self._device.id
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
@@ -197,17 +186,17 @@ class TuyaDevice(FanEntity):
|
|||||||
try:
|
try:
|
||||||
status = self._device.status()
|
status = self._device.status()
|
||||||
_LOGGER.debug("localtuya fan: status = %s", status)
|
_LOGGER.debug("localtuya fan: status = %s", status)
|
||||||
self._state = status['dps']['1']
|
self._state = status["dps"]["1"]
|
||||||
if status['dps']['1'] == False:
|
if status["dps"]["1"] == False:
|
||||||
self._speed = STATE_OFF
|
self._speed = STATE_OFF
|
||||||
elif int(status['dps']['2']) == 1:
|
elif int(status["dps"]["2"]) == 1:
|
||||||
self._speed = SPEED_LOW
|
self._speed = SPEED_LOW
|
||||||
elif int(status['dps']['2']) == 2:
|
elif int(status["dps"]["2"]) == 2:
|
||||||
self._speed = SPEED_MEDIUM
|
self._speed = SPEED_MEDIUM
|
||||||
elif int(status['dps']['2']) == 3:
|
elif int(status["dps"]["2"]) == 3:
|
||||||
self._speed = SPEED_HIGH
|
self._speed = SPEED_HIGH
|
||||||
# self._speed = status['dps']['2']
|
# self._speed = status['dps']['2']
|
||||||
self._oscillating = status['dps']['8']
|
self._oscillating = status["dps"]["8"]
|
||||||
success = True
|
success = True
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
if i + 1 == 3:
|
if i + 1 == 3:
|
||||||
|
Reference in New Issue
Block a user