Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tuya cover, fix Up/down position #59858

Merged
merged 6 commits into from
Dec 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions homeassistant/components/tuya/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TuyaCoverEntityDescription(CoverEntityDescription):

current_state: DPCode | None = None
current_state_inverse: bool = False
current_position: DPCode | None = None
current_position: DPCode | tuple[DPCode, ...] | None = None
set_position: DPCode | None = None


Expand All @@ -50,7 +50,7 @@ class TuyaCoverEntityDescription(CoverEntityDescription):
key=DPCode.CONTROL,
name="Curtain",
current_state=DPCode.SITUATION_SET,
current_position=DPCode.PERCENT_STATE,
current_position=(DPCode.PERCENT_CONTROL, DPCode.PERCENT_STATE),
set_position=DPCode.PERCENT_CONTROL,
device_class=DEVICE_CLASS_CURTAIN,
),
Expand Down Expand Up @@ -235,12 +235,23 @@ def current_cover_position(self) -> int | None:
if self._current_position_type is None:
return None

if not (
dpcode := (
# Determine current_position DPCodes
if isinstance(self.entity_description.current_position, DPCode):
leeyuentuen marked this conversation as resolved.
Show resolved Hide resolved
dpcode = (
self.entity_description.current_position
or self.entity_description.set_position
)
):
elif isinstance(self.entity_description.current_position, tuple):
dpcode = next(
(
dpcode
for dpcode in self.entity_description.current_position
if self.device.status.get(dpcode) is not None
),
None,
)

if not (dpcode):
return None

if (position := self.device.status.get(dpcode)) is None:
Expand Down Expand Up @@ -290,14 +301,40 @@ def open_cover(self, **kwargs: Any) -> None:
value: bool | str = True
if self.device.function[self.entity_description.key].type == "Enum":
value = "open"
self._send_command([{"code": self.entity_description.key, "value": value}])

commands: list[dict[str, str | int]] = [
{"code": self.entity_description.key, "value": value}
]

if (self.entity_description.set_position) is not None:
commands.append(
{
"code": self.entity_description.set_position,
"value": 0,
}
)

self._send_command(commands)

def close_cover(self, **kwargs: Any) -> None:
"""Close cover."""
value: bool | str = True
if self.device.function[self.entity_description.key].type == "Enum":
value = "close"
self._send_command([{"code": self.entity_description.key, "value": value}])

commands: list[dict[str, str | int]] = [
{"code": self.entity_description.key, "value": value}
]

if (self.entity_description.set_position) is not None:
commands.append(
{
"code": self.entity_description.set_position,
"value": 100,
}
)

self._send_command(commands)

def set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position."""
Expand Down