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

Remove all self._* variables in cover module #128

Merged
merged 1 commit into from
Jul 8, 2020
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
84 changes: 28 additions & 56 deletions custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,89 +79,60 @@ async def async_setup_entry(hass, entry, async_add_entities):
class TahomaCover(TahomaDevice, CoverEntity):
"""Representation a TaHoma Cover."""

def __init__(self, tahoma_device, controller):
"""Initialize the device."""
super().__init__(tahoma_device, controller)

self._tilt_position = None
self._position = None

self._lock_timer = 0 # Can be 0 and bigger

# Can be 'LSC', 'SAAC', 'SFC', 'UPS', 'externalGateway', 'localUser',
# 'myself', 'rain', 'security', 'temperature', 'timer', 'user', 'wind'
self._lock_originator = None

def update(self):
"""Update method."""
if self.should_wait():
self.schedule_update_ha_state(True)
return

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

self.update_position()
self.update_tilt_position()
self.update_lock()

def update_position(self):
"""Update position."""
# Home Assistant: 0 is closed, 100 is fully open.
# core:ClosureState: 100 is closed, 0 is fully open.
@property
def current_cover_position(self):
"""Return current position of cover."""

states = self.tahoma_device.active_states

position = None

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

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

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

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

if self._position is not None:
if 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
position = 100 - position

# TODO Check if this offset is really necessary
if self._position <= 5:
self._position = 0
if self._position >= 95:
self._position = 100
if position <= 5:
position = 0
if position >= 95:
position = 100

def update_tilt_position(self):
"""Update tilt position."""
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):
"""Update lock."""
states = self.tahoma_device.active_states
self._lock_timer = states.get(CORE_PRIORITY_LOCK_TIMER_STATE, 0)
self._lock_originator = states.get(IO_PRIORITY_LOCK_ORIGINATOR_STATE)

@property
def current_cover_position(self):
"""Return current position of cover."""
return self._position
return 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 self._tilt_position
states = self.tahoma_device.active_states
position = None
if CORE_SLATS_ORIENTATION_STATE in states:
position = 100 - states.get(CORE_SLATS_ORIENTATION_STATE)
return position

def set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
Expand Down Expand Up @@ -206,11 +177,11 @@ def is_closed(self):
if "core:OpenClosedUnknownState" in states:
return states.get("core:OpenClosedUnknownState") == "closed"

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

if self._tilt_position is not None:
return self._tilt_position == 0
if self.current_cover_tilt_position is not None:
return self.current_cover_tilt_position == 0

return None

Expand Down Expand Up @@ -244,8 +215,9 @@ def device_state_attributes(self):
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
if self._lock_timer > 0:
if self._lock_originator == "wind":
states = self.tahoma_device.active_states
if states.get(CORE_PRIORITY_LOCK_TIMER_STATE, 0) > 0:
if states.get(IO_PRIORITY_LOCK_ORIGINATOR_STATE) == "wind":
return "mdi:weather-windy"
else:
return "mdi:lock-alert"
Expand Down