Add a shadow for covers that do not support postion

This commit is contained in:
ColinRobbins 2020-02-02 12:30:29 +00:00
parent 48402d49dc
commit 4f53e2c6c6
2 changed files with 22 additions and 3 deletions

View file

@ -32,6 +32,7 @@ DOMAIN = "somfy"
CONF_CLIENT_ID = "client_id" CONF_CLIENT_ID = "client_id"
CONF_CLIENT_SECRET = "client_secret" CONF_CLIENT_SECRET = "client_secret"
CONF_SHADOW = "shadow_cover"
SOMFY_AUTH_CALLBACK_PATH = "/auth/somfy/callback" SOMFY_AUTH_CALLBACK_PATH = "/auth/somfy/callback"
SOMFY_AUTH_START = "/auth/somfy" SOMFY_AUTH_START = "/auth/somfy"
@ -42,6 +43,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_SHADOW): cv.boolean,
} }
) )
}, },
@ -58,6 +60,10 @@ async def async_setup(hass, config):
if DOMAIN not in config: if DOMAIN not in config:
return True return True
hass.data[DOMAIN][CONF_SHADOW] = False
if CONF_SHADOW in config[DOMAIN].keys():
hass.data[DOMAIN][CONF_SHADOW] = config[DOMAIN][CONF_SHADOW]
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_SHADOW, 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):
@ -24,8 +24,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
devices = hass.data[DOMAIN][DEVICES] devices = hass.data[DOMAIN][DEVICES]
if CONF_SHADOW in hass.data[DOMAIN].keys():
shadow = hass.data[DOMAIN][CONF_SHADOW]
else:
shadow = False
return [ return [
SomfyCover(cover, hass.data[DOMAIN][API]) SomfyCover(cover, hass.data[DOMAIN][API], shadow)
for cover in devices for cover in devices
if categories & set(cover.categories) if categories & set(cover.categories)
] ]
@ -36,10 +41,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, shadow):
"""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.shadow = shadow
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 +55,14 @@ class SomfyCover(SomfyEntity, CoverDevice):
def close_cover(self, **kwargs): def close_cover(self, **kwargs):
"""Close the cover.""" """Close the cover."""
if self.shadow:
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.shadow:
self._closed = False
self.cover.open() self.cover.open()
def stop_cover(self, **kwargs): def stop_cover(self, **kwargs):
@ -76,6 +87,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.shadow:
is_closed = self._closed
return is_closed return is_closed
@property @property