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 all commits
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
62 changes: 51 additions & 11 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 @@ -165,6 +165,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity):
_set_position_type: IntegerTypeData | None = None
_tilt_dpcode: DPCode | None = None
_tilt_type: IntegerTypeData | None = None
_position_dpcode: DPCode | None = None
entity_description: TuyaCoverEntityDescription

def __init__(
Expand Down Expand Up @@ -229,21 +230,34 @@ def __init__(
device.status_range[tilt_dpcode].values
)

# Determine current_position DPCodes
if (
self.entity_description.current_position is None
and self.entity_description.set_position is not None
):
self._position_dpcode = self.entity_description.set_position
elif isinstance(self.entity_description.current_position, DPCode):
self._position_dpcode = self.entity_description.current_position
elif isinstance(self.entity_description.current_position, tuple):
self._position_dpcode = next(
(
dpcode
for dpcode in self.entity_description.current_position
if self.device.status.get(dpcode) is not None
),
None,
)

@property
def current_cover_position(self) -> int | None:
"""Return cover current position."""
if self._current_position_type is None:
return None

if not (
dpcode := (
self.entity_description.current_position
or self.entity_description.set_position
)
):
if not self._position_dpcode:
return None

if (position := self.device.status.get(dpcode)) is None:
if (position := self.device.status.get(self._position_dpcode)) is None:
return None

return round(
Expand Down Expand Up @@ -290,14 +304,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