Skip to content

Commit

Permalink
Merge pull request #106 from tetienne/split-update-cover
Browse files Browse the repository at this point in the history
Split update cover method
  • Loading branch information
iMicknl authored Jul 6, 2020
2 parents 975c016 + 1cca8b8 commit e7e6c55
Showing 1 changed file with 57 additions and 72 deletions.
129 changes: 57 additions & 72 deletions custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ async def async_setup_entry(hass, entry, async_add_entities):

data = hass.data[DOMAIN][entry.entry_id]

entities = []
controller = data.get("controller")

for device in data.get("devices"):
if TAHOMA_TYPES[device.uiclass] == "cover":
entities.append(TahomaCover(device, controller))
entities = [
TahomaCover(device, controller)
for device in data.get("devices")
if TAHOMA_TYPES[device.uiclass] == "cover"
]

async_add_entities(entities)

Expand All @@ -73,6 +74,9 @@ def __init__(self, tahoma_device, controller):
"""Initialize the device."""
super().__init__(tahoma_device, controller)

self._tilt_position = None
self._position = None

self._icon = None
self._lock_timer = 0 # Can be 0 and bigger
self._lock_start_ts = None
Expand All @@ -95,40 +99,33 @@ def update(self):

self.controller.get_states([self.tahoma_device])

# Set current position.
self.update_position()
self.update_tilt_position()
self.update_lock()

def update_position(self):

# Home Assistant: 0 is closed, 100 is fully open.
# core:ClosureState: 100 is closed, 0 is fully open.

states = self.tahoma_device.active_states

# Set position for vertical covers
if CORE_CLOSURE_STATE in self.tahoma_device.active_states:
self._position = 100 - self.tahoma_device.active_states.get(
CORE_CLOSURE_STATE
)
if CORE_CLOSURE_STATE in states:
self._position = 100 - states.get(CORE_CLOSURE_STATE)

# Set position for horizontal covers
if CORE_DEPLOYMENT_STATE in self.tahoma_device.active_states:
self._position = 100 - self.tahoma_device.active_states.get(
CORE_DEPLOYMENT_STATE
)
if CORE_DEPLOYMENT_STATE in states:
self._position = 100 - states.get(CORE_DEPLOYMENT_STATE)

# Set position for gates
if CORE_PEDESTRIAN_POSITION_STATE in self.tahoma_device.active_states:
self._position = 100 - self.tahoma_device.active_states.get(
CORE_PEDESTRIAN_POSITION_STATE
)
if CORE_PEDESTRIAN_POSITION_STATE in states:
self._position = 100 - states.get(CORE_PEDESTRIAN_POSITION_STATE)

if CORE_TARGET_CLOSURE_STATE in self.tahoma_device.active_states:
self._position = 100 - self.tahoma_device.active_states.get(
CORE_TARGET_CLOSURE_STATE
)
if CORE_TARGET_CLOSURE_STATE in states:
self._position = 100 - states.get(CORE_TARGET_CLOSURE_STATE)

# Set tilt position for slats
if CORE_SLATS_ORIENTATION_STATE in self.tahoma_device.active_states:
self._tilt_position = 100 - self.tahoma_device.active_states.get(
CORE_SLATS_ORIENTATION_STATE
)

if getattr(self, "_position", False):
if self._position is not None:
# HorizontalAwning devices need a reversed position that can not be obtained via the API
if "Horizontal" in self.tahoma_device.widget:
self._position = 100 - self._position
Expand All @@ -139,12 +136,17 @@ def update(self):
if self._position >= 95:
self._position = 100

# Set Lock Timers
if CORE_PRIORITY_LOCK_TIMER_STATE in self.tahoma_device.active_states:
def update_tilt_position(self):
states = self.tahoma_device.active_states
# Set tilt position for slats
if CORE_SLATS_ORIENTATION_STATE in states:
self._tilt_position = 100 - states.get(CORE_SLATS_ORIENTATION_STATE)

def update_lock(self):
states = self.tahoma_device.active_states
if CORE_PRIORITY_LOCK_TIMER_STATE in states:
old_lock_timer = self._lock_timer
self._lock_timer = self.tahoma_device.active_states[
CORE_PRIORITY_LOCK_TIMER_STATE
]
self._lock_timer = states[CORE_PRIORITY_LOCK_TIMER_STATE]

# Derive timestamps from _lock_timer, only if not already set or something has changed
if self._lock_timer > 0:
Expand All @@ -162,27 +164,23 @@ def update(self):
self._lock_end_ts = None

# Set Lock Level
self._lock_level = self.tahoma_device.active_states.get(
IO_PRIORITY_LOCK_LEVEL_STATE
)
self._lock_level = states.get(IO_PRIORITY_LOCK_LEVEL_STATE)

# Set Lock Originator
self._lock_originator = self.tahoma_device.active_states.get(
IO_PRIORITY_LOCK_ORIGINATOR_STATE
)
self._lock_originator = states.get(IO_PRIORITY_LOCK_ORIGINATOR_STATE)

@property
def current_cover_position(self):
"""Return current position of cover."""
return getattr(self, "_position", None)
return self._position

@property
def current_cover_tilt_position(self):
"""Return current position of cover tilt.
None is unknown, 0 is closed, 100 is fully open.
"""
return getattr(self, "_tilt_position", None)
return self._tilt_position

def set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
Expand Down Expand Up @@ -211,39 +209,26 @@ def set_cover_tilt_position(self, **kwargs):
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"
)

if getattr(self, "_position", None) is not None:
states = self.tahoma_device.active_states
if "core:OpenClosedState" in states:
return states.get("core:OpenClosedState") == "closed"

if "core:SlatsOpenClosedState" in states:
return states.get("core:SlatsOpenClosedState") == "closed"

if "core:OpenClosedPartialState" in states:
return states.get("core:OpenClosedPartialState") == "closed"

if "core:OpenClosedPedestrianState" in states:
return states.get("core:OpenClosedPedestrianState") == "closed"

if "core:OpenClosedUnknownState" in states:
return states.get("core:OpenClosedUnknownState") == "closed"

if self._position is not None:
return self._position == 0

if getattr(self, "_tilt_position", None) is not None:
if self._tilt_position is not None:
return self._tilt_position == 0

return None
Expand Down

0 comments on commit e7e6c55

Please sign in to comment.