2019-02-25 19:13:34 +01:00
|
|
|
"""Support for covers through the SmartThings cloud API."""
|
2024-03-08 14:33:51 +01:00
|
|
|
|
2021-03-18 14:31:38 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 17:40:41 +02:00
|
|
|
from collections.abc import Sequence
|
2022-06-24 06:40:26 +02:00
|
|
|
from typing import Any
|
2019-02-25 19:13:34 +01:00
|
|
|
|
2019-07-01 04:29:21 +02:00
|
|
|
from pysmartthings import Attribute, Capability
|
|
|
|
|
2019-02-25 19:13:34 +01:00
|
|
|
from homeassistant.components.cover import (
|
|
|
|
ATTR_POSITION,
|
|
|
|
DOMAIN as COVER_DOMAIN,
|
|
|
|
STATE_CLOSED,
|
|
|
|
STATE_CLOSING,
|
|
|
|
STATE_OPEN,
|
|
|
|
STATE_OPENING,
|
2021-12-20 09:46:33 +01:00
|
|
|
CoverDeviceClass,
|
2020-04-25 18:07:15 +02:00
|
|
|
CoverEntity,
|
2022-04-07 09:38:14 +02:00
|
|
|
CoverEntityFeature,
|
2019-02-25 19:13:34 +01:00
|
|
|
)
|
2022-01-03 15:13:18 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-02-25 19:13:34 +01:00
|
|
|
from homeassistant.const import ATTR_BATTERY_LEVEL
|
2022-01-03 15:13:18 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-02-25 19:13:34 +01:00
|
|
|
|
|
|
|
from . import SmartThingsEntity
|
|
|
|
from .const import DATA_BROKERS, DOMAIN
|
|
|
|
|
|
|
|
VALUE_TO_STATE = {
|
|
|
|
"closed": STATE_CLOSED,
|
|
|
|
"closing": STATE_CLOSING,
|
|
|
|
"open": STATE_OPEN,
|
|
|
|
"opening": STATE_OPENING,
|
|
|
|
"partially open": STATE_OPEN,
|
|
|
|
"unknown": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-03 15:13:18 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Add covers for a config entry."""
|
|
|
|
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
SmartThingsCover(device)
|
|
|
|
for device in broker.devices.values()
|
|
|
|
if broker.any_assigned(device.device_id, COVER_DOMAIN)
|
|
|
|
],
|
|
|
|
True,
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2019-02-25 19:13:34 +01:00
|
|
|
|
|
|
|
|
2021-03-18 14:31:38 +01:00
|
|
|
def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Return all capabilities supported if minimum required are present."""
|
|
|
|
min_required = [
|
|
|
|
Capability.door_control,
|
|
|
|
Capability.garage_door_control,
|
|
|
|
Capability.window_shade,
|
|
|
|
]
|
|
|
|
# Must have one of the min_required
|
|
|
|
if any(capability in capabilities for capability in min_required):
|
|
|
|
# Return all capabilities supported/consumed
|
2024-03-16 18:37:20 +01:00
|
|
|
return [
|
|
|
|
*min_required,
|
2023-07-18 14:13:31 +02:00
|
|
|
Capability.battery,
|
|
|
|
Capability.switch_level,
|
|
|
|
Capability.window_shade_level,
|
|
|
|
]
|
2019-02-25 19:13:34 +01:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-04-25 18:07:15 +02:00
|
|
|
class SmartThingsCover(SmartThingsEntity, CoverEntity):
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Define a SmartThings cover."""
|
|
|
|
|
|
|
|
def __init__(self, device):
|
|
|
|
"""Initialize the cover class."""
|
|
|
|
super().__init__(device)
|
2023-07-18 14:13:31 +02:00
|
|
|
self._current_cover_position = None
|
2019-02-25 19:13:34 +01:00
|
|
|
self._state = None
|
2022-04-07 09:38:14 +02:00
|
|
|
self._attr_supported_features = (
|
|
|
|
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
|
|
|
)
|
2023-07-18 14:13:31 +02:00
|
|
|
if (
|
|
|
|
Capability.switch_level in device.capabilities
|
|
|
|
or Capability.window_shade_level in device.capabilities
|
|
|
|
):
|
2022-04-07 09:38:14 +02:00
|
|
|
self._attr_supported_features |= CoverEntityFeature.SET_POSITION
|
2019-02-25 19:13:34 +01:00
|
|
|
|
2023-09-12 19:26:33 +02:00
|
|
|
if Capability.door_control in device.capabilities:
|
|
|
|
self._attr_device_class = CoverDeviceClass.DOOR
|
|
|
|
elif Capability.window_shade in device.capabilities:
|
|
|
|
self._attr_device_class = CoverDeviceClass.SHADE
|
|
|
|
elif Capability.garage_door_control in device.capabilities:
|
|
|
|
self._attr_device_class = CoverDeviceClass.GARAGE
|
|
|
|
|
2022-06-24 06:40:26 +02:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Close cover."""
|
|
|
|
# Same command for all 3 supported capabilities
|
|
|
|
await self._device.close(set_status=True)
|
|
|
|
# State is set optimistically in the commands above, therefore update
|
|
|
|
# the entity state ahead of receiving the confirming push updates
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2022-06-24 06:40:26 +02:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Open the cover."""
|
|
|
|
# Same for all capability types
|
|
|
|
await self._device.open(set_status=True)
|
|
|
|
# State is set optimistically in the commands above, therefore update
|
|
|
|
# the entity state ahead of receiving the confirming push updates
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2022-06-25 12:31:53 +02:00
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Move the cover to a specific position."""
|
2022-11-17 08:32:42 +01:00
|
|
|
if not self.supported_features & CoverEntityFeature.SET_POSITION:
|
2019-02-25 19:13:34 +01:00
|
|
|
return
|
|
|
|
# Do not set_status=True as device will report progress.
|
2023-07-18 14:13:31 +02:00
|
|
|
if Capability.window_shade_level in self._device.capabilities:
|
|
|
|
await self._device.set_window_shade_level(
|
|
|
|
kwargs[ATTR_POSITION], set_status=False
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await self._device.set_level(kwargs[ATTR_POSITION], set_status=False)
|
2019-02-25 19:13:34 +01:00
|
|
|
|
2022-06-28 10:26:36 +02:00
|
|
|
async def async_update(self) -> None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Update the attrs of the cover."""
|
|
|
|
if Capability.door_control in self._device.capabilities:
|
2022-06-28 10:26:36 +02:00
|
|
|
self._state = VALUE_TO_STATE.get(self._device.status.door)
|
2019-02-25 19:13:34 +01:00
|
|
|
elif Capability.window_shade in self._device.capabilities:
|
2022-06-28 10:26:36 +02:00
|
|
|
self._state = VALUE_TO_STATE.get(self._device.status.window_shade)
|
2019-02-25 19:13:34 +01:00
|
|
|
elif Capability.garage_door_control in self._device.capabilities:
|
2022-06-28 10:26:36 +02:00
|
|
|
self._state = VALUE_TO_STATE.get(self._device.status.door)
|
2019-02-25 19:13:34 +01:00
|
|
|
|
2023-07-18 14:13:31 +02:00
|
|
|
if Capability.window_shade_level in self._device.capabilities:
|
2023-09-12 19:26:33 +02:00
|
|
|
self._attr_current_cover_position = self._device.status.shade_level
|
2023-07-18 14:13:31 +02:00
|
|
|
elif Capability.switch_level in self._device.capabilities:
|
2023-09-12 19:26:33 +02:00
|
|
|
self._attr_current_cover_position = self._device.status.level
|
2023-07-18 14:13:31 +02:00
|
|
|
|
2023-09-12 19:26:33 +02:00
|
|
|
self._attr_extra_state_attributes = {}
|
2019-02-25 19:13:34 +01:00
|
|
|
battery = self._device.status.attributes[Attribute.battery].value
|
|
|
|
if battery is not None:
|
2023-09-12 19:26:33 +02:00
|
|
|
self._attr_extra_state_attributes[ATTR_BATTERY_LEVEL] = battery
|
2019-02-25 19:13:34 +01:00
|
|
|
|
|
|
|
@property
|
2022-06-25 11:59:56 +02:00
|
|
|
def is_opening(self) -> bool:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return self._state == STATE_OPENING
|
|
|
|
|
|
|
|
@property
|
2022-06-25 11:59:56 +02:00
|
|
|
def is_closing(self) -> bool:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
return self._state == STATE_CLOSING
|
|
|
|
|
|
|
|
@property
|
2022-06-25 11:59:56 +02:00
|
|
|
def is_closed(self) -> bool | None:
|
2019-02-25 19:13:34 +01:00
|
|
|
"""Return if the cover is closed or not."""
|
|
|
|
if self._state == STATE_CLOSED:
|
|
|
|
return True
|
|
|
|
return None if self._state is None else False
|