Add optimistic mode for somfy covers that do not support position (#31407)

* Add a shadow for covers that do not support postion

* Rename shadow as optimistic

* Set optimisic default mode

* fix black error

* Remove redundant check

* optimisitc variable name consistency with config
pull/32384/head
Colin Robbins 2020-03-01 15:42:26 +00:00 committed by GitHub
parent a9a523b05c
commit 0431b983d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -27,6 +27,7 @@ DOMAIN = "somfy"
CONF_CLIENT_ID = "client_id" CONF_CLIENT_ID = "client_id"
CONF_CLIENT_SECRET = "client_secret" CONF_CLIENT_SECRET = "client_secret"
CONF_OPTIMISTIC = "optimisitic"
SOMFY_AUTH_CALLBACK_PATH = "/auth/somfy/callback" SOMFY_AUTH_CALLBACK_PATH = "/auth/somfy/callback"
SOMFY_AUTH_START = "/auth/somfy" SOMFY_AUTH_START = "/auth/somfy"
@ -37,6 +38,7 @@ CONFIG_SCHEMA = vol.Schema(
{ {
vol.Required(CONF_CLIENT_ID): cv.string, vol.Required(CONF_CLIENT_ID): cv.string,
vol.Required(CONF_CLIENT_SECRET): cv.string, vol.Required(CONF_CLIENT_SECRET): cv.string,
vol.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
} }
) )
}, },
@ -53,6 +55,8 @@ async def async_setup(hass, config):
if DOMAIN not in config: if DOMAIN not in config:
return True return True
hass.data[DOMAIN][CONF_OPTIMISTIC] = config[DOMAIN][CONF_OPTIMISTIC]
config_flow.SomfyFlowHandler.async_register_implementation( config_flow.SomfyFlowHandler.async_register_implementation(
hass, hass,
config_entry_oauth2_flow.LocalOAuth2Implementation( config_entry_oauth2_flow.LocalOAuth2Implementation(

View File

@ -8,7 +8,7 @@ from homeassistant.components.cover import (
CoverDevice, CoverDevice,
) )
from . import API, DEVICES, DOMAIN, SomfyEntity from . import API, CONF_OPTIMISTIC, DEVICES, DOMAIN, SomfyEntity
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
@ -25,7 +25,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
devices = hass.data[DOMAIN][DEVICES] devices = hass.data[DOMAIN][DEVICES]
return [ return [
SomfyCover(cover, hass.data[DOMAIN][API]) SomfyCover(
cover, hass.data[DOMAIN][API], hass.data[DOMAIN][CONF_OPTIMISTIC]
)
for cover in devices for cover in devices
if categories & set(cover.categories) if categories & set(cover.categories)
] ]
@ -36,10 +38,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class SomfyCover(SomfyEntity, CoverDevice): class SomfyCover(SomfyEntity, CoverDevice):
"""Representation of a Somfy cover device.""" """Representation of a Somfy cover device."""
def __init__(self, device, api): def __init__(self, device, api, optimistic):
"""Initialize the Somfy device.""" """Initialize the Somfy device."""
super().__init__(device, api) super().__init__(device, api)
self.cover = Blind(self.device, self.api) self.cover = Blind(self.device, self.api)
self.optimistic = optimistic
self._closed = None
async def async_update(self): async def async_update(self):
"""Update the device with the latest data.""" """Update the device with the latest data."""
@ -48,10 +52,14 @@ class SomfyCover(SomfyEntity, CoverDevice):
def close_cover(self, **kwargs): def close_cover(self, **kwargs):
"""Close the cover.""" """Close the cover."""
if self.optimistic:
self._closed = True
self.cover.close() self.cover.close()
def open_cover(self, **kwargs): def open_cover(self, **kwargs):
"""Open the cover.""" """Open the cover."""
if self.optimistic:
self._closed = False
self.cover.open() self.cover.open()
def stop_cover(self, **kwargs): def stop_cover(self, **kwargs):
@ -76,6 +84,8 @@ class SomfyCover(SomfyEntity, CoverDevice):
is_closed = None is_closed = None
if self.has_capability("position"): if self.has_capability("position"):
is_closed = self.cover.is_closed() is_closed = self.cover.is_closed()
elif self.optimistic:
is_closed = self._closed
return is_closed return is_closed
@property @property