Fix fan platform after merge

This commit is contained in:
Pierre Ståhl
2020-09-10 12:07:59 +02:00
parent d2281c4368
commit 50cbde330b

View File

@@ -15,78 +15,68 @@ fan:
"""
import logging
import requests
import voluptuous as vol
from homeassistant.components.fan import (SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH,
FanEntity, SUPPORT_SET_SPEED,
SUPPORT_OSCILLATE, SUPPORT_DIRECTION, PLATFORM_SCHEMA)
from homeassistant.const import STATE_OFF
"""from . import DATA_TUYA, TuyaDevice"""
from homeassistant.const import (CONF_HOST, CONF_ID, CONF_FRIENDLY_NAME, CONF_ICON, CONF_NAME)
from homeassistant.components.fan import (
SPEED_LOW,
SPEED_MEDIUM,
SPEED_HIGH,
FanEntity,
SUPPORT_SET_SPEED,
SUPPORT_OSCILLATE,
SUPPORT_DIRECTION,
PLATFORM_SCHEMA,
)
from homeassistant.const import CONF_ID, CONF_FRIENDLY_NAME, STATE_OFF
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__)
DEFAULT_NAME = 'localtuyafan'
REQUIREMENTS = ['pytuya>=7.1.0']
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(BASE_PLATFORM_SCHEMA)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_ICON): cv.icon,
vol.Required(CONF_HOST): cv.string,
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 flow_schema(dps):
"""Return schema used in config flow."""
return {}
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up of the Tuya switch."""
from . import pytuya
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Setup a Tuya fan based on a config entry."""
device, entities_to_setup = prepare_setup_entities(config_entry, "fan", FanDevice)
if not entities_to_setup:
return
fans = []
localtuyadevice = pytuya.FanDevice(config.get(CONF_DEVICE_ID), config.get(CONF_HOST), config.get(CONF_LOCAL_KEY))
localtuyadevice.set_version(float(config.get(CONF_PROTOCOL_VERSION)))
_LOGGER.debug("localtuya fan: setup_platform: %s", localtuyadevice)
fan_device = localtuyadevice
fans.append(
for device_config in entities_to_setup:
fans.append(
TuyaDevice(
fan_device,
config.get(CONF_NAME),
config.get(CONF_FRIENDLY_NAME),
config.get(CONF_ICON),
config.get(CONF_ID),
device,
device_config[CONF_FRIENDLY_NAME],
device_config[CONF_ID],
)
)
_LOGGER.info("Setup localtuya fan %s with device ID %s ", config.get(CONF_FRIENDLY_NAME), config.get(CONF_DEVICE_ID) )
)
async_add_entities(fans, True)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up of the Tuya fan."""
return import_from_yaml(hass, config, "fan")
add_entities(fans, True)
class TuyaDevice(FanEntity):
"""A demonstration fan component."""
# def __init__(self, hass, name: str, supported_features: int) -> None:
def __init__(self, device, name, friendly_name, icon, switchid):
def __init__(self, device, friendly_name, switchid):
"""Initialize the entity."""
self._device = device
self._name = friendly_name
self._available = False
self._friendly_name = friendly_name
self._icon = icon
self._switch_id = switchid
self._status = self._device.status()
self._state = False
@@ -97,7 +87,7 @@ class TuyaDevice(FanEntity):
@property
def oscillating(self):
"""Return current oscillating status."""
#if self._speed == STATE_OFF:
# if self._speed == STATE_OFF:
# return False
_LOGGER.debug("localtuya fan: oscillating = %s", self._oscillating)
return self._oscillating
@@ -137,7 +127,7 @@ class TuyaDevice(FanEntity):
def turn_off(self, **kwargs) -> None:
"""Turn off the entity."""
_LOGGER.debug("localtuya fan: turn_off")
self._device.set_status(False, '1')
self._device.set_status(False, "1")
self._state = False
self.schedule_update_ha_state()
@@ -147,14 +137,14 @@ class TuyaDevice(FanEntity):
self._speed = speed
# self.schedule_update_ha_state()
if speed == STATE_OFF:
self._device.set_status(False, '1')
self._device.set_status(False, "1")
self._state = False
elif speed == SPEED_LOW:
self._device.set_value('2', '1')
self._device.set_value("2", "1")
elif speed == SPEED_MEDIUM:
self._device.set_value('2', '2')
self._device.set_value("2", "2")
elif speed == SPEED_HIGH:
self._device.set_value('2', '3')
self._device.set_value("2", "3")
self.schedule_update_ha_state()
# def set_direction(self, direction: str) -> None:
@@ -165,7 +155,7 @@ class TuyaDevice(FanEntity):
def oscillate(self, oscillating: bool) -> None:
"""Set oscillation."""
self._oscillating = oscillating
self._device.set_value('8', oscillating)
self._device.set_value("8", oscillating)
self.schedule_update_ha_state()
# @property
@@ -176,8 +166,7 @@ class TuyaDevice(FanEntity):
@property
def unique_id(self):
"""Return unique device identifier."""
_LOGGER.debug("localtuya fan unique_id = %s", self._device)
return self._device.id
return f"local_{self._device.id}_{self._switch_id}"
@property
def available(self):
@@ -197,20 +186,20 @@ class TuyaDevice(FanEntity):
try:
status = self._device.status()
_LOGGER.debug("localtuya fan: status = %s", status)
self._state = status['dps']['1']
if status['dps']['1'] == False:
self._state = status["dps"]["1"]
if status["dps"]["1"] == False:
self._speed = STATE_OFF
elif int(status['dps']['2']) == 1:
elif int(status["dps"]["2"]) == 1:
self._speed = SPEED_LOW
elif int(status['dps']['2']) == 2:
elif int(status["dps"]["2"]) == 2:
self._speed = SPEED_MEDIUM
elif int(status['dps']['2']) == 3:
elif int(status["dps"]["2"]) == 3:
self._speed = SPEED_HIGH
# self._speed = status['dps']['2']
self._oscillating = status['dps']['8']
self._oscillating = status["dps"]["8"]
success = True
except ConnectionError:
if i+1 == 3:
if i + 1 == 3:
success = False
raise ConnectionError("Failed to update status.")
self._available = success