From 032fceac2e7c879255924047b2ee30ba880020f7 Mon Sep 17 00:00:00 2001 From: rospogrigio <49229287+rospogrigio@users.noreply.github.com> Date: Wed, 21 Oct 2020 10:23:00 +0200 Subject: [PATCH] Cover uppercase and new commands (#81) * Introduced uppercase detection for cover commands * Added FZ_ZZ as open_close available commands for covers * Added FZ_ZZ in the YAML config example * Introduced invert_position Option for cover * Cover slider inverted properly * Fixed wrong sample YAML config for cover * Fixed postlund's remark --- README.md | 2 -- custom_components/localtuya/__init__.py | 3 +- custom_components/localtuya/const.py | 1 + custom_components/localtuya/cover.py | 30 ++++++++++++++----- .../localtuya/translations/en.json | 2 ++ info.md | 2 -- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9280914..21d227b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ ![logo](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/logo-small.png) -# localtuya-homeassistant - A Home Assistant custom Integration for local handling of Tuya-based devices. Device status is updated receiving push updates from the device instead of polling, so status updates are extremely fast (even if manually operated). diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index f4a89b2..be6a8db 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -20,11 +20,12 @@ localtuya: - platform: cover friendly_name: Device Cover id: 2 - open_close_stop_cmds: # Optional, default: "on_off_stop" + commands_set: # Optional, default: "on_off_stop" ["on_off_stop","open_close_stop","fz_zz_stop","1_2_3"] positioning_mode: ["none","position","fake"] # Optional, default: "none" currpos_dp: 3 # Optional, required only for "position" mode setpos_dp: 4 # Optional, required only for "position" mode + position_inverted: [True,False] # Optional, default: False span_time: 25 # Full movement time: Optional, required only for "fake" mode - platform: fan diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index e1ffad9..4a6195b 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -22,6 +22,7 @@ CONF_COMMANDS_SET = "commands_set" CONF_POSITIONING_MODE = "positioning_mode" CONF_CURRENT_POSITION_DP = "current_position_dp" CONF_SET_POSITION_DP = "set_position_dp" +CONF_POSITION_INVERTED = "position_inverted" CONF_SPAN_TIME = "span_time" # sensor diff --git a/custom_components/localtuya/cover.py b/custom_components/localtuya/cover.py index f0fc50a..2b65009 100644 --- a/custom_components/localtuya/cover.py +++ b/custom_components/localtuya/cover.py @@ -20,6 +20,7 @@ from .const import ( CONF_CURRENT_POSITION_DP, CONF_SET_POSITION_DP, CONF_POSITIONING_MODE, + CONF_POSITION_INVERTED, CONF_SPAN_TIME, ) from .common import LocalTuyaEntity, async_setup_entry @@ -50,6 +51,7 @@ def flow_schema(dps): ), vol.Optional(CONF_CURRENT_POSITION_DP): vol.In(dps), vol.Optional(CONF_SET_POSITION_DP): vol.In(dps), + vol.Optional(CONF_POSITION_INVERTED, default=False): bool, vol.Optional(CONF_SPAN_TIME, default=DEFAULT_SPAN_TIME): vol.All( vol.Coerce(float), vol.Range(min=1.0, max=300.0) ), @@ -70,12 +72,12 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): super().__init__(device, config_entry, switchid, **kwargs) self._state = None self._current_cover_position = None - command_set = DEFAULT_COMMANDS_SET + commands_set = DEFAULT_COMMANDS_SET if self.has_config(CONF_COMMANDS_SET): - command_set = self._config[CONF_COMMANDS_SET] - self._open_cmd = command_set.split("_")[0] - self._close_cmd = command_set.split("_")[1] - self._stop_cmd = command_set.split("_")[2] + commands_set = self._config[CONF_COMMANDS_SET] + self._open_cmd = commands_set.split("_")[0] + self._close_cmd = commands_set.split("_")[1] + self._stop_cmd = commands_set.split("_")[2] print("Initialized cover [{}]".format(self.name)) @property @@ -108,6 +110,7 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): """Return if the cover is open or not.""" if self._config[CONF_POSITIONING_MODE] != COVER_MODE_POSITION: return None + return self._current_cover_position == 100 @property @@ -115,6 +118,7 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): """Return if the cover is closed or not.""" if self._config[CONF_POSITIONING_MODE] != COVER_MODE_POSITION: return None + return self._current_cover_position == 0 async def async_set_cover_position(self, **kwargs): @@ -139,6 +143,9 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): elif self._config[CONF_POSITIONING_MODE] == COVER_MODE_POSITION: converted_position = int(kwargs[ATTR_POSITION]) + if self._config[CONF_POSITION_INVERTED]: + converted_position = 100 - converted_position + if 0 <= converted_position <= 100 and self.has_config(CONF_SET_POSITION_DP): await self._device.set_dp( converted_position, self._config[CONF_SET_POSITION_DP] @@ -162,10 +169,17 @@ class LocaltuyaCover(LocalTuyaEntity, CoverEntity): def status_updated(self): """Device status was updated.""" self._state = self.dps(self._dp_id) + if self._state.isupper(): + self._open_cmd = self._open_cmd.upper() + self._close_cmd = self._close_cmd.upper() + self._stop_cmd = self._stop_cmd.upper() + if self.has_config(CONF_CURRENT_POSITION_DP): - self._current_cover_position = self.dps( - self._config[CONF_CURRENT_POSITION_DP] - ) + curr_pos = self.dps_conf(CONF_CURRENT_POSITION_DP) + if self._config[CONF_POSITION_INVERTED]: + self._current_cover_position = 100 - curr_pos + else: + self._current_cover_position = curr_pos else: self._current_cover_position = 50 diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index f8dbc94..d0b4d45 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -50,6 +50,7 @@ "positioning_mode": "Positioning mode", "current_position_dp": "Current Position (when Position mode is *position*)", "set_position_dp": "Set Position (when Position Mode is *position*)", + "position_inverted": "Invert 0-100 position (when Position Mode is *position*)", "span_time": "Full opening time, in secs. (when Position Mode is fake*)", "unit_of_measurement": "Unit of Measurement", "device_class": "Device Class", @@ -89,6 +90,7 @@ "positioning_mode": "Positioning mode", "current_position_dp": "Current Position (for *position* mode only)", "set_position_dp": "Set Position (for *position* mode only)", + "position_inverted": "Invert 0-100 position (when Position Mode is *position*)", "span_time": "Full opening time, in secs. (for *fake* mode only)", "unit_of_measurement": "Unit of Measurement", "device_class": "Device Class", diff --git a/info.md b/info.md index 3b3e7f6..d0a2c76 100644 --- a/info.md +++ b/info.md @@ -4,8 +4,6 @@ ![logo](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/logo-small.png) -# localtuya-homeassistant - A Home Assistant custom Integration for local handling of Tuya-based devices. Device status is updated receiving push updates from the device instead of polling, so status updates are extremely fast (even if manually operated).