Fixed postlund's comments. Unified open_close config options
This commit is contained in:
@@ -20,9 +20,11 @@ localtuya:
|
|||||||
- platform: cover
|
- platform: cover
|
||||||
friendly_name: Device Cover
|
friendly_name: Device Cover
|
||||||
id: 2
|
id: 2
|
||||||
open_cmd: "on" # Optional
|
open_close_cmds: ["on_off","open_close"] # Optional, default: "on_off"
|
||||||
close_cmd: "off" # Optional
|
positioning_mode: ["none","position","fake"] # Optional, default: "none"
|
||||||
stop_cmd: "stop" # Optional
|
currpos_dps: 3 # Optional, required only for "position" mode
|
||||||
|
setpos_dps: 4 # Optional, required only for "position" mode
|
||||||
|
span_time: 25 # Full movement time: Optional, required only for "fake" mode
|
||||||
|
|
||||||
- platform: fan
|
- platform: fan
|
||||||
friendly_name: Device Fan
|
friendly_name: Device Fan
|
||||||
|
@@ -14,15 +14,10 @@ CONF_CURRENT_CONSUMPTION = "current_consumption"
|
|||||||
CONF_VOLTAGE = "voltage"
|
CONF_VOLTAGE = "voltage"
|
||||||
|
|
||||||
# cover
|
# cover
|
||||||
CONF_OPEN_CMD = "open_cmd"
|
CONF_OPENCLOSE_CMDS = "open_close_cmds"
|
||||||
CONF_CLOSE_CMD = "close_cmd"
|
|
||||||
CONF_STOP_CMD = "stop_cmd"
|
|
||||||
CONF_POSITIONING_MODE = "positioning_mode"
|
CONF_POSITIONING_MODE = "positioning_mode"
|
||||||
CONF_CURRPOS = "currpos_dps"
|
CONF_CURRPOS = "currpos_dps"
|
||||||
CONF_SETPOS = "setpos_dps"
|
CONF_SETPOS = "setpos_dps"
|
||||||
CONF_MODE_NONE = "none"
|
|
||||||
CONF_MODE_POSITION = "position"
|
|
||||||
CONF_MODE_FAKE = "fake"
|
|
||||||
CONF_SPAN_TIME = "span_time"
|
CONF_SPAN_TIME = "span_time"
|
||||||
|
|
||||||
# sensor
|
# sensor
|
||||||
|
@@ -16,41 +16,42 @@ from homeassistant.components.cover import (
|
|||||||
from homeassistant.const import CONF_ID
|
from homeassistant.const import CONF_ID
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_OPEN_CMD,
|
CONF_OPENCLOSE_CMDS,
|
||||||
CONF_CLOSE_CMD,
|
|
||||||
CONF_STOP_CMD,
|
|
||||||
CONF_CURRPOS,
|
CONF_CURRPOS,
|
||||||
CONF_SETPOS,
|
CONF_SETPOS,
|
||||||
CONF_POSITIONING_MODE,
|
CONF_POSITIONING_MODE,
|
||||||
CONF_MODE_NONE,
|
|
||||||
CONF_MODE_POSITION,
|
|
||||||
CONF_MODE_FAKE,
|
|
||||||
CONF_SPAN_TIME,
|
CONF_SPAN_TIME,
|
||||||
)
|
)
|
||||||
from .common import LocalTuyaEntity, prepare_setup_entities
|
from .common import LocalTuyaEntity, prepare_setup_entities
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_OPEN_CMD = "on"
|
COVER_ONOFF_CMDS = "on_off"
|
||||||
DEFAULT_CLOSE_CMD = "off"
|
COVER_OPENCLOSE_CMDS = "open_close"
|
||||||
DEFAULT_STOP_CMD = "stop"
|
COVER_STOP_CMD = "stop"
|
||||||
DEFAULT_POSITIONING_MODE = CONF_MODE_NONE
|
COVER_MODE_NONE = "none"
|
||||||
|
COVER_MODE_POSITION = "position"
|
||||||
|
COVER_MODE_FAKE = "fake"
|
||||||
|
|
||||||
|
DEFAULT_OPENCLOSE_CMDS = "on_off"
|
||||||
|
DEFAULT_POSITIONING_MODE = COVER_MODE_NONE
|
||||||
DEFAULT_SPAN_TIME = 25.0
|
DEFAULT_SPAN_TIME = 25.0
|
||||||
|
|
||||||
|
|
||||||
def flow_schema(dps):
|
def flow_schema(dps):
|
||||||
"""Return schema used in config flow."""
|
"""Return schema used in config flow."""
|
||||||
return {
|
return {
|
||||||
vol.Optional(CONF_OPEN_CMD, default=DEFAULT_OPEN_CMD): vol.In(["on", "open"]),
|
vol.Optional(CONF_OPENCLOSE_CMDS, default=DEFAULT_OPENCLOSE_CMDS): vol.In(
|
||||||
vol.Optional(CONF_CLOSE_CMD, default=DEFAULT_CLOSE_CMD): vol.In(
|
[COVER_ONOFF_CMDS, COVER_OPENCLOSE_CMDS]
|
||||||
["off", "close"]
|
|
||||||
),
|
),
|
||||||
vol.Optional(CONF_POSITIONING_MODE, default=DEFAULT_POSITIONING_MODE): vol.In(
|
vol.Optional(CONF_POSITIONING_MODE, default=DEFAULT_POSITIONING_MODE): vol.In(
|
||||||
[CONF_MODE_NONE, CONF_MODE_POSITION, CONF_MODE_FAKE]
|
[COVER_MODE_NONE, COVER_MODE_POSITION, COVER_MODE_FAKE]
|
||||||
),
|
),
|
||||||
vol.Optional(CONF_CURRPOS): vol.In(dps),
|
vol.Optional(CONF_CURRPOS): vol.In(dps),
|
||||||
vol.Optional(CONF_SETPOS): vol.In(dps),
|
vol.Optional(CONF_SETPOS): vol.In(dps),
|
||||||
vol.Optional(CONF_SPAN_TIME, default=DEFAULT_SPAN_TIME): float,
|
vol.Optional(CONF_SPAN_TIME, default=DEFAULT_SPAN_TIME): vol.All(
|
||||||
|
vol.Coerce(float), vol.Range(min=1.0, max=300.0)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -88,19 +89,16 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
|
|||||||
"""Initialize a new LocaltuyaCover."""
|
"""Initialize a new LocaltuyaCover."""
|
||||||
super().__init__(device, config_entry, switchid, **kwargs)
|
super().__init__(device, config_entry, switchid, **kwargs)
|
||||||
self._state = None
|
self._state = None
|
||||||
self._current_cover_position = 50
|
self._current_cover_position = None
|
||||||
self._config[CONF_STOP_CMD] = DEFAULT_STOP_CMD
|
self._open_cmd = self._config[CONF_OPENCLOSE_CMDS].split("_")[0]
|
||||||
print(
|
self._close_cmd = self._config[CONF_OPENCLOSE_CMDS].split("_")[1]
|
||||||
"Initialized cover [{}] with status [{}] and state [{}]".format(
|
print("Initialized cover [{}]".format(self.name))
|
||||||
self.name, self._status, self._state
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
|
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
|
||||||
if self._config[CONF_POSITIONING_MODE] != CONF_MODE_NONE:
|
if self._config[CONF_POSITIONING_MODE] != COVER_MODE_NONE:
|
||||||
supported_features = supported_features | SUPPORT_SET_POSITION
|
supported_features = supported_features | SUPPORT_SET_POSITION
|
||||||
return supported_features
|
return supported_features
|
||||||
|
|
||||||
@@ -113,34 +111,32 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
|
|||||||
def is_opening(self):
|
def is_opening(self):
|
||||||
"""Return if cover is opening."""
|
"""Return if cover is opening."""
|
||||||
state = self._state
|
state = self._state
|
||||||
return state == self._config[CONF_OPEN_CMD]
|
return state == self._open_cmd
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_closing(self):
|
def is_closing(self):
|
||||||
"""Return if cover is closing."""
|
"""Return if cover is closing."""
|
||||||
state = self._state
|
state = self._state
|
||||||
return state == self._config[CONF_CLOSE_CMD]
|
return state == self._close_cmd
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_open(self):
|
def is_open(self):
|
||||||
"""Return if the cover is open or not."""
|
"""Return if the cover is open or not."""
|
||||||
if self._config[CONF_POSITIONING_MODE] != CONF_MODE_POSITION:
|
if self._config[CONF_POSITIONING_MODE] != COVER_MODE_POSITION:
|
||||||
return None
|
return None
|
||||||
else:
|
return self._current_cover_position == 100
|
||||||
return self._current_cover_position == 100
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_closed(self):
|
def is_closed(self):
|
||||||
"""Return if the cover is closed or not."""
|
"""Return if the cover is closed or not."""
|
||||||
if self._config[CONF_POSITIONING_MODE] != CONF_MODE_POSITION:
|
if self._config[CONF_POSITIONING_MODE] != COVER_MODE_POSITION:
|
||||||
return None
|
return None
|
||||||
else:
|
return self._current_cover_position == 0
|
||||||
return self._current_cover_position == 0
|
|
||||||
|
|
||||||
def set_cover_position(self, **kwargs):
|
def set_cover_position(self, **kwargs):
|
||||||
"""Move the cover to a specific position."""
|
"""Move the cover to a specific position."""
|
||||||
_LOGGER.debug("Setting cover position: %r", kwargs[ATTR_POSITION])
|
_LOGGER.debug("Setting cover position: %r", kwargs[ATTR_POSITION])
|
||||||
if self._config[CONF_POSITIONING_MODE] == CONF_MODE_FAKE:
|
if self._config[CONF_POSITIONING_MODE] == COVER_MODE_FAKE:
|
||||||
newpos = float(kwargs[ATTR_POSITION])
|
newpos = float(kwargs[ATTR_POSITION])
|
||||||
|
|
||||||
currpos = self.current_cover_position
|
currpos = self.current_cover_position
|
||||||
@@ -157,28 +153,30 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity):
|
|||||||
self._current_cover_position = 50
|
self._current_cover_position = 50
|
||||||
_LOGGER.debug("Done")
|
_LOGGER.debug("Done")
|
||||||
|
|
||||||
elif self._config[CONF_POSITIONING_MODE] == CONF_MODE_POSITION:
|
elif self._config[CONF_POSITIONING_MODE] == COVER_MODE_POSITION:
|
||||||
converted_position = int(kwargs[ATTR_POSITION])
|
converted_position = int(kwargs[ATTR_POSITION])
|
||||||
if converted_position in range(0, 101) and self.has_config(CONF_SETPOS):
|
if 0 <= converted_position <= 100 and self.has_config(CONF_SETPOS):
|
||||||
self._device.set_dps(converted_position, self._config[CONF_SETPOS])
|
self._device.set_dps(converted_position, self._config[CONF_SETPOS])
|
||||||
|
|
||||||
def open_cover(self, **kwargs):
|
def open_cover(self, **kwargs):
|
||||||
"""Open the cover."""
|
"""Open the cover."""
|
||||||
_LOGGER.debug("Launching command %s to cover ", self._config[CONF_OPEN_CMD])
|
_LOGGER.debug("Launching command %s to cover ", self._open_cmd)
|
||||||
self._device.set_dps(self._config[CONF_OPEN_CMD], self._dps_id)
|
self._device.set_dps(self._open_cmd, self._dps_id)
|
||||||
|
|
||||||
def close_cover(self, **kwargs):
|
def close_cover(self, **kwargs):
|
||||||
"""Close cover."""
|
"""Close cover."""
|
||||||
_LOGGER.debug("Launching command %s to cover ", self._config[CONF_CLOSE_CMD])
|
_LOGGER.debug("Launching command %s to cover ", self._close_cmd)
|
||||||
self._device.set_dps(self._config[CONF_CLOSE_CMD], self._dps_id)
|
self._device.set_dps(self._close_cmd, self._dps_id)
|
||||||
|
|
||||||
def stop_cover(self, **kwargs):
|
def stop_cover(self, **kwargs):
|
||||||
"""Stop the cover."""
|
"""Stop the cover."""
|
||||||
_LOGGER.debug("Launching command %s to cover ", self._config[CONF_STOP_CMD])
|
_LOGGER.debug("Launching command %s to cover ", COVER_STOP_CMD)
|
||||||
self._device.set_dps(self._config[CONF_STOP_CMD], self._dps_id)
|
self._device.set_dps(COVER_STOP_CMD, self._dps_id)
|
||||||
|
|
||||||
def status_updated(self):
|
def status_updated(self):
|
||||||
"""Device status was updated."""
|
"""Device status was updated."""
|
||||||
self._state = self.dps(self._dps_id)
|
self._state = self.dps(self._dps_id)
|
||||||
if self.has_config(CONF_CURRPOS):
|
if self.has_config(CONF_CURRPOS):
|
||||||
self._current_cover_position = self.dps(self._config[CONF_CURRPOS])
|
self._current_cover_position = self.dps(self._config[CONF_CURRPOS])
|
||||||
|
else:
|
||||||
|
self._current_cover_position = 50
|
||||||
|
@@ -65,11 +65,7 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity):
|
|||||||
"""Initialize the Tuya switch."""
|
"""Initialize the Tuya switch."""
|
||||||
super().__init__(device, config_entry, switchid, **kwargs)
|
super().__init__(device, config_entry, switchid, **kwargs)
|
||||||
self._state = None
|
self._state = None
|
||||||
print(
|
print("Initialized switch [{}]".format(self.name))
|
||||||
"Initialized switch [{}] with status [{}] and state [{}]".format(
|
|
||||||
self.name, self._status, self._state
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
|
@@ -46,8 +46,7 @@
|
|||||||
"current": "Current",
|
"current": "Current",
|
||||||
"current_consumption": "Current Consumption",
|
"current_consumption": "Current Consumption",
|
||||||
"voltage": "Voltage",
|
"voltage": "Voltage",
|
||||||
"open_cmd": "Open Command",
|
"open_close_cmds": "Open/Close Commands",
|
||||||
"close_cmd": "Close Command",
|
|
||||||
"positioning_mode": "Positioning mode",
|
"positioning_mode": "Positioning mode",
|
||||||
"currpos_dps": "Current Position dps (Required only if Positioning Mode is 'position')",
|
"currpos_dps": "Current Position dps (Required only if Positioning Mode is 'position')",
|
||||||
"setpos_dps": "Set Position dps (Required only if Positioning Mode is 'position')",
|
"setpos_dps": "Set Position dps (Required only if Positioning Mode is 'position')",
|
||||||
@@ -82,8 +81,7 @@
|
|||||||
"current": "Current",
|
"current": "Current",
|
||||||
"current_consumption": "Current Consumption",
|
"current_consumption": "Current Consumption",
|
||||||
"voltage": "Voltage",
|
"voltage": "Voltage",
|
||||||
"open_cmd": "Open Command",
|
"open_close_cmds": "Open/Close Commands",
|
||||||
"close_cmd": "Close Command",
|
|
||||||
"positioning_mode": "Positioning mode",
|
"positioning_mode": "Positioning mode",
|
||||||
"currpos_dps": "Current Position dps (Required only if Positioning Mode is 'position')",
|
"currpos_dps": "Current Position dps (Required only if Positioning Mode is 'position')",
|
||||||
"setpos_dps": "Set Position dps (Required only if Positioning Mode is 'position')",
|
"setpos_dps": "Set Position dps (Required only if Positioning Mode is 'position')",
|
||||||
|
Reference in New Issue
Block a user