Fix slide open/close percentage (#33739)

* Fix Open/Close percentage

* Update __init__.py

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/33739/merge
Alexander 2020-04-09 10:20:48 +02:00 committed by GitHub
parent 1adb45f74e
commit 87e7e7fe8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 12 deletions

View File

@ -1,4 +1,4 @@
"""Component for the Go Slide API."""
"""Component for the Slide API."""
from datetime import timedelta
import logging
@ -19,7 +19,15 @@ from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.event import async_call_later, async_track_time_interval
from .const import API, COMPONENT, DEFAULT_RETRY, DOMAIN, SLIDES
from .const import (
API,
COMPONENT,
CONF_INVERT_POSITION,
DEFAULT_OFFSET,
DEFAULT_RETRY,
DOMAIN,
SLIDES,
)
_LOGGER = logging.getLogger(__name__)
@ -34,6 +42,7 @@ CONFIG_SCHEMA = vol.Schema(
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): cv.time_period,
vol.Optional(CONF_INVERT_POSITION, default=False): cv.boolean,
}
)
},
@ -60,7 +69,7 @@ async def async_setup(hass, config):
for slide in result:
if "device_id" not in slide:
_LOGGER.error(
"Found invalid Slide entry, device_id is missing. Entry=%s", slide,
"Found invalid Slide entry, device_id is missing. Entry=%s", slide
)
continue
@ -73,6 +82,7 @@ async def async_setup(hass, config):
oldpos = slidenew.get("pos")
slidenew["pos"] = None
slidenew["online"] = False
slidenew["invert"] = config[DOMAIN][CONF_INVERT_POSITION]
if "device_info" not in slide:
_LOGGER.error(
@ -91,15 +101,21 @@ async def async_setup(hass, config):
if oldpos is None or oldpos == slidenew["pos"]:
slidenew["state"] = (
STATE_CLOSED if slidenew["pos"] > 0.95 else STATE_OPEN
STATE_CLOSED
if slidenew["pos"] > (1 - DEFAULT_OFFSET)
else STATE_OPEN
)
elif oldpos < slidenew["pos"]:
slidenew["state"] = (
STATE_CLOSED if slidenew["pos"] >= 0.95 else STATE_CLOSING
STATE_CLOSED
if slidenew["pos"] >= (1 - DEFAULT_OFFSET)
else STATE_CLOSING
)
else:
slidenew["state"] = (
STATE_OPEN if slidenew["pos"] <= 0.05 else STATE_OPENING
STATE_OPEN
if slidenew["pos"] <= DEFAULT_OFFSET
else STATE_OPENING
)
elif "code" in slide["device_info"]:
_LOGGER.warning(
@ -135,7 +151,7 @@ async def async_setup(hass, config):
result = await hass.data[DOMAIN][API].login()
except (goslideapi.ClientConnectionError, goslideapi.ClientTimeoutError) as err:
_LOGGER.error(
"Error connecting to Slide Cloud: %s, going to retry in %s seconds",
"Error connecting to Slide Cloud: %s, going to retry in %s second(s)",
err,
DEFAULT_RETRY,
)

View File

@ -1,7 +1,9 @@
"""Define constants for the Go Slide component."""
"""Define constants for the Slide component."""
API = "api"
COMPONENT = "cover"
CONF_INVERT_POSITION = "invert_position"
DOMAIN = "slide"
SLIDES = "slides"
DEFAULT_OFFSET = 0.15
DEFAULT_RETRY = 120

View File

@ -1,4 +1,4 @@
"""Support for Go Slide slides."""
"""Support for Slide slides."""
import logging
@ -12,13 +12,13 @@ from homeassistant.components.cover import (
)
from homeassistant.const import ATTR_ID
from .const import API, DOMAIN, SLIDES
from .const import API, DEFAULT_OFFSET, DOMAIN, SLIDES
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up cover(s) for Go Slide platform."""
"""Set up cover(s) for Slide platform."""
if discovery_info is None:
return
@ -33,7 +33,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SlideCover(CoverDevice):
"""Representation of a Go Slide cover."""
"""Representation of a Slide cover."""
def __init__(self, api, slide):
"""Initialize the cover."""
@ -42,6 +42,7 @@ class SlideCover(CoverDevice):
self._id = slide["id"]
self._unique_id = slide["mac"]
self._name = slide["name"]
self._invert = slide["invert"]
@property
def unique_id(self):
@ -95,6 +96,10 @@ class SlideCover(CoverDevice):
"""Return the current position of cover shutter."""
pos = self._slide["pos"]
if pos is not None:
if (1 - pos) <= DEFAULT_OFFSET or pos <= DEFAULT_OFFSET:
pos = round(pos)
if not self._invert:
pos = 1 - pos
pos = int(pos * 100)
return pos
@ -115,6 +120,8 @@ class SlideCover(CoverDevice):
async def async_set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
position = kwargs[ATTR_POSITION] / 100
if not self._invert:
position = 1 - position
if self._slide["pos"] is not None:
if position > self._slide["pos"]: