From 92ca94e91536400f8b34a88ab41bb504c0f1bd01 Mon Sep 17 00:00:00 2001 From: Thomas Schamm Date: Thu, 18 Nov 2021 14:00:01 +0100 Subject: [PATCH] Add cover platform to bosch_shc integration (#51443) Co-authored-by: Artem Draft Co-authored-by: Martin Hjelmare --- .coveragerc | 1 + .../components/bosch_shc/__init__.py | 2 +- homeassistant/components/bosch_shc/cover.py | 86 +++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/bosch_shc/cover.py diff --git a/.coveragerc b/.coveragerc index 6750675da26..bbe00155b2f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -123,6 +123,7 @@ omit = homeassistant/components/bosch_shc/__init__.py homeassistant/components/bosch_shc/binary_sensor.py homeassistant/components/bosch_shc/const.py + homeassistant/components/bosch_shc/cover.py homeassistant/components/bosch_shc/entity.py homeassistant/components/bosch_shc/sensor.py homeassistant/components/braviatv/__init__.py diff --git a/homeassistant/components/bosch_shc/__init__.py b/homeassistant/components/bosch_shc/__init__.py index f68a2b68467..2909350fc1c 100644 --- a/homeassistant/components/bosch_shc/__init__.py +++ b/homeassistant/components/bosch_shc/__init__.py @@ -19,7 +19,7 @@ from .const import ( DOMAIN, ) -PLATFORMS = ["binary_sensor", "sensor"] +PLATFORMS = ["binary_sensor", "cover", "sensor"] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/bosch_shc/cover.py b/homeassistant/components/bosch_shc/cover.py new file mode 100644 index 00000000000..91543e586bf --- /dev/null +++ b/homeassistant/components/bosch_shc/cover.py @@ -0,0 +1,86 @@ +"""Platform for cover integration.""" +from boschshcpy import SHCSession, SHCShutterControl + +from homeassistant.components.cover import ( + ATTR_POSITION, + DEVICE_CLASS_SHUTTER, + SUPPORT_CLOSE, + SUPPORT_OPEN, + SUPPORT_SET_POSITION, + SUPPORT_STOP, + CoverEntity, +) + +from .const import DATA_SESSION, DOMAIN +from .entity import SHCEntity + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the SHC cover platform.""" + + entities = [] + session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION] + + for cover in session.device_helper.shutter_controls: + entities.append( + ShutterControlCover( + device=cover, + parent_id=session.information.unique_id, + entry_id=config_entry.entry_id, + ) + ) + + if entities: + async_add_entities(entities) + + +class ShutterControlCover(SHCEntity, CoverEntity): + """Representation of a SHC shutter control device.""" + + _attr_device_class = DEVICE_CLASS_SHUTTER + _attr_supported_features = ( + SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP | SUPPORT_SET_POSITION + ) + + @property + def current_cover_position(self): + """Return the current cover position.""" + return round(self._device.level * 100.0) + + def stop_cover(self, **kwargs): + """Stop the cover.""" + self._device.stop() + + @property + def is_closed(self): + """Return if the cover is closed or not.""" + return self.current_cover_position == 0 + + @property + def is_opening(self): + """Return if the cover is opening or not.""" + return ( + self._device.operation_state + == SHCShutterControl.ShutterControlService.State.OPENING + ) + + @property + def is_closing(self): + """Return if the cover is closing or not.""" + return ( + self._device.operation_state + == SHCShutterControl.ShutterControlService.State.CLOSING + ) + + def open_cover(self, **kwargs): + """Open the cover.""" + self._device.level = 1.0 + + def close_cover(self, **kwargs): + """Close cover.""" + self._device.level = 0.0 + + def set_cover_position(self, **kwargs): + """Move the cover to a specific position.""" + position = kwargs[ATTR_POSITION] + self._device.level = position / 100.0