From ff79bd04cd5af5a16e91be4a16c18771eca9302b Mon Sep 17 00:00:00 2001 From: rospogrigio Date: Thu, 1 Oct 2020 12:40:48 +0200 Subject: [PATCH] Fixed postlund's comments. Unified open_close config options --- custom_components/localtuya/__init__.py | 8 +- custom_components/localtuya/const.py | 7 +- custom_components/localtuya/cover.py | 78 +++++++++---------- custom_components/localtuya/switch.py | 6 +- .../localtuya/translations/en.json | 6 +- 5 files changed, 47 insertions(+), 58 deletions(-) diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index 2b55f2a..6ca523f 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -20,9 +20,11 @@ localtuya: - platform: cover friendly_name: Device Cover id: 2 - open_cmd: "on" # Optional - close_cmd: "off" # Optional - stop_cmd: "stop" # Optional + open_close_cmds: ["on_off","open_close"] # Optional, default: "on_off" + positioning_mode: ["none","position","fake"] # Optional, default: "none" + 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 friendly_name: Device Fan diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index 7994f2d..b36b218 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -14,15 +14,10 @@ CONF_CURRENT_CONSUMPTION = "current_consumption" CONF_VOLTAGE = "voltage" # cover -CONF_OPEN_CMD = "open_cmd" -CONF_CLOSE_CMD = "close_cmd" -CONF_STOP_CMD = "stop_cmd" +CONF_OPENCLOSE_CMDS = "open_close_cmds" CONF_POSITIONING_MODE = "positioning_mode" CONF_CURRPOS = "currpos_dps" CONF_SETPOS = "setpos_dps" -CONF_MODE_NONE = "none" -CONF_MODE_POSITION = "position" -CONF_MODE_FAKE = "fake" CONF_SPAN_TIME = "span_time" # sensor diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index 50179fe..4df67f2 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -16,41 +16,42 @@ from homeassistant.components.cover import ( from homeassistant.const import CONF_ID from .const import ( - CONF_OPEN_CMD, - CONF_CLOSE_CMD, - CONF_STOP_CMD, + CONF_OPENCLOSE_CMDS, CONF_CURRPOS, CONF_SETPOS, CONF_POSITIONING_MODE, - CONF_MODE_NONE, - CONF_MODE_POSITION, - CONF_MODE_FAKE, CONF_SPAN_TIME, ) from .common import LocalTuyaEntity, prepare_setup_entities _LOGGER = logging.getLogger(__name__) -DEFAULT_OPEN_CMD = "on" -DEFAULT_CLOSE_CMD = "off" -DEFAULT_STOP_CMD = "stop" -DEFAULT_POSITIONING_MODE = CONF_MODE_NONE +COVER_ONOFF_CMDS = "on_off" +COVER_OPENCLOSE_CMDS = "open_close" +COVER_STOP_CMD = "stop" +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 def flow_schema(dps): """Return schema used in config flow.""" return { - vol.Optional(CONF_OPEN_CMD, default=DEFAULT_OPEN_CMD): vol.In(["on", "open"]), - vol.Optional(CONF_CLOSE_CMD, default=DEFAULT_CLOSE_CMD): vol.In( - ["off", "close"] + vol.Optional(CONF_OPENCLOSE_CMDS, default=DEFAULT_OPENCLOSE_CMDS): vol.In( + [COVER_ONOFF_CMDS, COVER_OPENCLOSE_CMDS] ), 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_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.""" super().__init__(device, config_entry, switchid, **kwargs) self._state = None - self._current_cover_position = 50 - self._config[CONF_STOP_CMD] = DEFAULT_STOP_CMD - print( - "Initialized cover [{}] with status [{}] and state [{}]".format( - self.name, self._status, self._state - ) - ) + self._current_cover_position = None + self._open_cmd = self._config[CONF_OPENCLOSE_CMDS].split("_")[0] + self._close_cmd = self._config[CONF_OPENCLOSE_CMDS].split("_")[1] + print("Initialized cover [{}]".format(self.name)) @property def supported_features(self): """Flag supported features.""" 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 return supported_features @@ -113,34 +111,32 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): def is_opening(self): """Return if cover is opening.""" state = self._state - return state == self._config[CONF_OPEN_CMD] + return state == self._open_cmd @property def is_closing(self): """Return if cover is closing.""" state = self._state - return state == self._config[CONF_CLOSE_CMD] + return state == self._close_cmd @property def is_open(self): """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 - else: - return self._current_cover_position == 100 + return self._current_cover_position == 100 @property def is_closed(self): """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 - else: - return self._current_cover_position == 0 + return self._current_cover_position == 0 def set_cover_position(self, **kwargs): """Move the cover to a specific 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]) currpos = self.current_cover_position @@ -157,28 +153,30 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): self._current_cover_position = 50 _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]) - 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]) def open_cover(self, **kwargs): """Open the cover.""" - _LOGGER.debug("Launching command %s to cover ", self._config[CONF_OPEN_CMD]) - self._device.set_dps(self._config[CONF_OPEN_CMD], self._dps_id) + _LOGGER.debug("Launching command %s to cover ", self._open_cmd) + self._device.set_dps(self._open_cmd, self._dps_id) def close_cover(self, **kwargs): """Close cover.""" - _LOGGER.debug("Launching command %s to cover ", self._config[CONF_CLOSE_CMD]) - self._device.set_dps(self._config[CONF_CLOSE_CMD], self._dps_id) + _LOGGER.debug("Launching command %s to cover ", self._close_cmd) + self._device.set_dps(self._close_cmd, self._dps_id) def stop_cover(self, **kwargs): """Stop the cover.""" - _LOGGER.debug("Launching command %s to cover ", self._config[CONF_STOP_CMD]) - self._device.set_dps(self._config[CONF_STOP_CMD], self._dps_id) + _LOGGER.debug("Launching command %s to cover ", COVER_STOP_CMD) + self._device.set_dps(COVER_STOP_CMD, self._dps_id) def status_updated(self): """Device status was updated.""" self._state = self.dps(self._dps_id) if self.has_config(CONF_CURRPOS): self._current_cover_position = self.dps(self._config[CONF_CURRPOS]) + else: + self._current_cover_position = 50 diff --git a/custom_components/localtuya/switch.py b/custom_components/localtuya/switch.py index 7c88584..2e8c1f0 100644 --- a/custom_components/localtuya/switch.py +++ b/custom_components/localtuya/switch.py @@ -65,11 +65,7 @@ class LocaltuyaSwitch(LocalTuyaEntity, SwitchEntity): """Initialize the Tuya switch.""" super().__init__(device, config_entry, switchid, **kwargs) self._state = None - print( - "Initialized switch [{}] with status [{}] and state [{}]".format( - self.name, self._status, self._state - ) - ) + print("Initialized switch [{}]".format(self.name)) @property def is_on(self): diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 88dfe70..78f1098 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -46,8 +46,7 @@ "current": "Current", "current_consumption": "Current Consumption", "voltage": "Voltage", - "open_cmd": "Open Command", - "close_cmd": "Close Command", + "open_close_cmds": "Open/Close Commands", "positioning_mode": "Positioning mode", "currpos_dps": "Current 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_consumption": "Current Consumption", "voltage": "Voltage", - "open_cmd": "Open Command", - "close_cmd": "Close Command", + "open_close_cmds": "Open/Close Commands", "positioning_mode": "Positioning mode", "currpos_dps": "Current Position dps (Required only if Positioning Mode is 'position')", "setpos_dps": "Set Position dps (Required only if Positioning Mode is 'position')",