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>
This commit is contained in:
parent
1adb45f74e
commit
87e7e7fe8a
3 changed files with 37 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
||||||
"""Component for the Go Slide API."""
|
"""Component for the Slide API."""
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
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.discovery import async_load_platform
|
||||||
from homeassistant.helpers.event import async_call_later, async_track_time_interval
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -34,6 +42,7 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
|
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
|
||||||
): cv.time_period,
|
): 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:
|
for slide in result:
|
||||||
if "device_id" not in slide:
|
if "device_id" not in slide:
|
||||||
_LOGGER.error(
|
_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
|
continue
|
||||||
|
|
||||||
|
@ -73,6 +82,7 @@ async def async_setup(hass, config):
|
||||||
oldpos = slidenew.get("pos")
|
oldpos = slidenew.get("pos")
|
||||||
slidenew["pos"] = None
|
slidenew["pos"] = None
|
||||||
slidenew["online"] = False
|
slidenew["online"] = False
|
||||||
|
slidenew["invert"] = config[DOMAIN][CONF_INVERT_POSITION]
|
||||||
|
|
||||||
if "device_info" not in slide:
|
if "device_info" not in slide:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
@ -91,15 +101,21 @@ async def async_setup(hass, config):
|
||||||
|
|
||||||
if oldpos is None or oldpos == slidenew["pos"]:
|
if oldpos is None or oldpos == slidenew["pos"]:
|
||||||
slidenew["state"] = (
|
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"]:
|
elif oldpos < slidenew["pos"]:
|
||||||
slidenew["state"] = (
|
slidenew["state"] = (
|
||||||
STATE_CLOSED if slidenew["pos"] >= 0.95 else STATE_CLOSING
|
STATE_CLOSED
|
||||||
|
if slidenew["pos"] >= (1 - DEFAULT_OFFSET)
|
||||||
|
else STATE_CLOSING
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
slidenew["state"] = (
|
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"]:
|
elif "code" in slide["device_info"]:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
|
@ -135,7 +151,7 @@ async def async_setup(hass, config):
|
||||||
result = await hass.data[DOMAIN][API].login()
|
result = await hass.data[DOMAIN][API].login()
|
||||||
except (goslideapi.ClientConnectionError, goslideapi.ClientTimeoutError) as err:
|
except (goslideapi.ClientConnectionError, goslideapi.ClientTimeoutError) as err:
|
||||||
_LOGGER.error(
|
_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,
|
err,
|
||||||
DEFAULT_RETRY,
|
DEFAULT_RETRY,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
"""Define constants for the Go Slide component."""
|
"""Define constants for the Slide component."""
|
||||||
|
|
||||||
API = "api"
|
API = "api"
|
||||||
COMPONENT = "cover"
|
COMPONENT = "cover"
|
||||||
|
CONF_INVERT_POSITION = "invert_position"
|
||||||
DOMAIN = "slide"
|
DOMAIN = "slide"
|
||||||
SLIDES = "slides"
|
SLIDES = "slides"
|
||||||
|
DEFAULT_OFFSET = 0.15
|
||||||
DEFAULT_RETRY = 120
|
DEFAULT_RETRY = 120
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
"""Support for Go Slide slides."""
|
"""Support for Slide slides."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -12,13 +12,13 @@ from homeassistant.components.cover import (
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ID
|
from homeassistant.const import ATTR_ID
|
||||||
|
|
||||||
from .const import API, DOMAIN, SLIDES
|
from .const import API, DEFAULT_OFFSET, DOMAIN, SLIDES
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
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:
|
if discovery_info is None:
|
||||||
return
|
return
|
||||||
|
@ -33,7 +33,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||||
|
|
||||||
|
|
||||||
class SlideCover(CoverDevice):
|
class SlideCover(CoverDevice):
|
||||||
"""Representation of a Go Slide cover."""
|
"""Representation of a Slide cover."""
|
||||||
|
|
||||||
def __init__(self, api, slide):
|
def __init__(self, api, slide):
|
||||||
"""Initialize the cover."""
|
"""Initialize the cover."""
|
||||||
|
@ -42,6 +42,7 @@ class SlideCover(CoverDevice):
|
||||||
self._id = slide["id"]
|
self._id = slide["id"]
|
||||||
self._unique_id = slide["mac"]
|
self._unique_id = slide["mac"]
|
||||||
self._name = slide["name"]
|
self._name = slide["name"]
|
||||||
|
self._invert = slide["invert"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
|
@ -95,6 +96,10 @@ class SlideCover(CoverDevice):
|
||||||
"""Return the current position of cover shutter."""
|
"""Return the current position of cover shutter."""
|
||||||
pos = self._slide["pos"]
|
pos = self._slide["pos"]
|
||||||
if pos is not None:
|
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)
|
pos = int(pos * 100)
|
||||||
return pos
|
return pos
|
||||||
|
|
||||||
|
@ -115,6 +120,8 @@ class SlideCover(CoverDevice):
|
||||||
async def async_set_cover_position(self, **kwargs):
|
async def async_set_cover_position(self, **kwargs):
|
||||||
"""Move the cover to a specific position."""
|
"""Move the cover to a specific position."""
|
||||||
position = kwargs[ATTR_POSITION] / 100
|
position = kwargs[ATTR_POSITION] / 100
|
||||||
|
if not self._invert:
|
||||||
|
position = 1 - position
|
||||||
|
|
||||||
if self._slide["pos"] is not None:
|
if self._slide["pos"] is not None:
|
||||||
if position > self._slide["pos"]:
|
if position > self._slide["pos"]:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue