Skip to content

Commit

Permalink
[Refactor] Add helper functions select_command and select_state.
Browse files Browse the repository at this point in the history
Add two functions to select first existing command or state from a list.
Implement in cover.py
  • Loading branch information
vlebourl committed Jun 29, 2020
1 parent 7776510 commit 45a3024
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 75 deletions.
101 changes: 27 additions & 74 deletions custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,16 @@ def set_cover_position(self, **kwargs):
# HorizontalAwning devices need a reversed position that can not be obtained via the API
if "Horizontal" in self.tahoma_device.widget:
position = kwargs.get(ATTR_POSITION, 0)

if COMMAND_SET_POSITION in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_SET_POSITION, position)

if COMMAND_SET_CLOSURE in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_SET_CLOSURE, position)

if COMMAND_SET_PEDESTRIAN_POSITION in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_SET_PEDESTRIAN_POSITION, position)
self.apply_action(
self.select_command(
[
COMMAND_SET_POSITION,
COMMAND_SET_CLOSURE,
COMMAND_SET_PEDESTRIAN_POSITION,
]
),
position,
)

def set_cover_tilt_position(self, **kwargs):
"""Move the cover tilt to a specific position."""
Expand All @@ -206,35 +207,17 @@ def set_cover_tilt_position(self, **kwargs):
@property
def is_closed(self):
"""Return if the cover is closed."""

if "core:OpenClosedState" in self.tahoma_device.active_states:
return (
self.tahoma_device.active_states.get("core:OpenClosedState") == "closed"
)

if "core:SlatsOpenClosedState" in self.tahoma_device.active_states:
return (
self.tahoma_device.active_states.get("core:SlatsOpenClosedState")
== "closed"
)

if "core:OpenClosedPartialState" in self.tahoma_device.active_states:
return (
self.tahoma_device.active_states.get("core:OpenClosedPartialState")
== "closed"
)

if "core:OpenClosedPedestrianState" in self.tahoma_device.active_states:
return (
self.tahoma_device.active_states.get("core:OpenClosedPedestrianState")
== "closed"
)

if "core:OpenClosedUnknownState" in self.tahoma_device.active_states:
return (
self.tahoma_device.active_states.get("core:OpenClosedUnknownState")
== "closed"
)
state = self.select_state(
[
"core:OpenClosedState",
"core:SlatsOpenClosedState",
"core:OpenClosedPartialState",
"core:OpenClosedPedestrianState",
"core:OpenClosedUnknownState",
]
)
if state is not None:
return state == "closed"

if getattr(self, "_position", None) is not None:
return self._position == 0
Expand Down Expand Up @@ -290,57 +273,27 @@ def icon(self):

def open_cover(self, **kwargs):
"""Open the cover."""

if "open" in self.tahoma_device.command_definitions:
return self.apply_action("open")

if "up" in self.tahoma_device.command_definitions:
return self.apply_action("up")
self.apply_action(self.select_command(["open", "up"]))

def open_cover_tilt(self, **kwargs):
"""Open the cover tilt."""

if "openSlats" in self.tahoma_device.command_definitions:
return self.apply_action("openSlats")
self.apply_action(self.select_command(["openSlats"]))

def close_cover(self, **kwargs):
"""Close the cover."""

if "close" in self.tahoma_device.command_definitions:
return self.apply_action("close")

if "down" in self.tahoma_device.command_definitions:
return self.apply_action("down")
self.apply_action(self.select_command(["close", "down"]))

def close_cover_tilt(self, **kwargs):
"""Close the cover tilt."""

if "closeSlats" in self.tahoma_device.command_definitions:
return self.apply_action("closeSlats")
self.apply_action(self.select_command(["closeSlats"]))

def stop_cover(self, **kwargs):
"""Stop the cover."""

if "stop" in self.tahoma_device.command_definitions:
return self.apply_action("stop")

if "stopIdentify" in self.tahoma_device.command_definitions:
return self.apply_action("stopIdentify")

if "my" in self.tahoma_device.command_definitions:
return self.apply_action("my")
self.apply_action(self.select_command(["stop", "stopIdentify", "my"]))

def stop_cover_tilt(self, **kwargs):
"""Stop the cover."""

if "stopIdentify" in self.tahoma_device.command_definitions:
return self.apply_action("stopIdentify")

if "stop" in self.tahoma_device.command_definitions:
return self.apply_action("stop")

if "my" in self.tahoma_device.command_definitions:
return self.apply_action("my")
self.apply_action(self.select_command(["stopIdentify", "stop", "my"]))

@property
def supported_features(self):
Expand Down
20 changes: 19 additions & 1 deletion custom_components/tahoma/tahoma_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,31 @@ def device_info(self):
"sw_version": self.tahoma_device.type,
}

def select_command(self, commands):
"""Select first existing command in a list of commands."""
return next(
(c for c in self.tahoma_device.command_definitions if c in commands), None
)

def select_state(self, active_states):
"""Select first existing active state in a list of states."""
return next(
(
v
for k, v in self.tahoma_device.active_states.items()
if k in active_states
),
None,
)

async def async_apply_action(self, cmd_name, *args):
"""Apply Action to Device in async context."""
await self.hass.async_add_executor_job(self.apply_action, cmd_name, *args)

def apply_action(self, cmd_name, *args):
"""Apply Action to Device."""

if cmd_name is None:
raise NotImplementedError
action = Action(self.tahoma_device.url)
action.add_command(cmd_name, *args)
exec_id = self.controller.apply_actions("HomeAssistant", [action])
Expand Down

0 comments on commit 45a3024

Please sign in to comment.