2018-12-17 20:44:47 +01:00
|
|
|
"""Support for ESPHome covers."""
|
2021-03-17 23:49:01 +01:00
|
|
|
from __future__ import annotations
|
2019-05-29 13:33:49 +02:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2019-11-26 03:00:58 +01:00
|
|
|
from aioesphomeapi import CoverInfo, CoverOperation, CoverState
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_POSITION,
|
|
|
|
ATTR_TILT_POSITION,
|
2021-11-30 09:44:39 +01:00
|
|
|
DEVICE_CLASSES,
|
2020-04-25 18:07:15 +02:00
|
|
|
CoverEntity,
|
2022-04-06 11:52:59 +02:00
|
|
|
CoverEntityFeature,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2018-12-17 20:44:47 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-21 12:18:42 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-12 22:56:10 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2019-05-29 13:33:49 +02:00
|
|
|
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
2018-12-17 20:44:47 +01:00
|
|
|
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_setup_entry(
|
2021-07-12 22:56:10 +02:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2019-07-31 12:25:30 -07:00
|
|
|
) -> None:
|
2018-12-17 20:44:47 +01:00
|
|
|
"""Set up ESPHome covers based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="cover",
|
|
|
|
info_type=CoverInfo,
|
|
|
|
entity_type=EsphomeCover,
|
|
|
|
state_type=CoverState,
|
2018-12-17 20:44:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
2021-07-15 06:44:57 +02:00
|
|
|
# pylint: disable=invalid-overridden-method
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
|
|
|
|
class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
|
|
|
|
"""A cover implementation for ESPHome."""
|
2018-12-17 20:44:47 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
2022-04-06 11:52:59 +02:00
|
|
|
flags = (
|
|
|
|
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
|
|
|
|
)
|
2019-04-08 15:44:24 +02:00
|
|
|
if self._static_info.supports_position:
|
2022-04-06 11:52:59 +02:00
|
|
|
flags |= CoverEntityFeature.SET_POSITION
|
2019-04-08 15:44:24 +02:00
|
|
|
if self._static_info.supports_tilt:
|
2022-04-06 11:52:59 +02:00
|
|
|
flags |= (
|
|
|
|
CoverEntityFeature.OPEN_TILT
|
|
|
|
| CoverEntityFeature.CLOSE_TILT
|
|
|
|
| CoverEntityFeature.SET_TILT_POSITION
|
|
|
|
)
|
2019-04-08 15:44:24 +02:00
|
|
|
return flags
|
|
|
|
|
|
|
|
@property
|
2021-11-30 09:44:39 +01:00
|
|
|
def device_class(self) -> str | None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
2021-11-30 09:44:39 +01:00
|
|
|
if self._static_info.device_class not in DEVICE_CLASSES:
|
|
|
|
return None
|
2019-04-08 15:44:24 +02:00
|
|
|
return self._static_info.device_class
|
2018-12-17 20:44:47 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self) -> bool:
|
|
|
|
"""Return true if we do optimistic updates."""
|
2019-04-08 15:44:24 +02:00
|
|
|
return self._static_info.assumed_state
|
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2021-03-17 23:49:01 +01:00
|
|
|
def is_closed(self) -> bool | None:
|
2018-12-17 20:44:47 +01:00
|
|
|
"""Return if the cover is closed or not."""
|
2019-04-08 15:44:24 +02:00
|
|
|
# Check closed state with api version due to a protocol change
|
2021-06-28 13:43:45 +02:00
|
|
|
return self._state.is_closed(self._api_version)
|
2019-04-08 15:44:24 +02:00
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
|
|
|
def is_opening(self) -> bool:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return self._state.current_operation == CoverOperation.IS_OPENING
|
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
|
|
|
def is_closing(self) -> bool:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
return self._state.current_operation == CoverOperation.IS_CLOSING
|
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2021-03-17 23:49:01 +01:00
|
|
|
def current_cover_position(self) -> int | None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Return current position of cover. 0 is closed, 100 is open."""
|
2019-04-16 22:48:46 +02:00
|
|
|
if not self._static_info.supports_position:
|
2019-04-08 15:44:24 +02:00
|
|
|
return None
|
2019-09-29 12:06:51 +02:00
|
|
|
return round(self._state.position * 100.0)
|
2019-04-08 15:44:24 +02:00
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2021-07-12 22:56:10 +02:00
|
|
|
def current_cover_tilt_position(self) -> int | None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Return current position of cover tilt. 0 is closed, 100 is open."""
|
2019-04-16 22:48:46 +02:00
|
|
|
if not self._static_info.supports_tilt:
|
2019-04-08 15:44:24 +02:00
|
|
|
return None
|
2021-07-12 22:56:10 +02:00
|
|
|
return round(self._state.tilt * 100.0)
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2018-12-17 20:44:47 +01:00
|
|
|
"""Open the cover."""
|
2019-07-31 12:25:30 -07:00
|
|
|
await self._client.cover_command(key=self._static_info.key, position=1.0)
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2018-12-17 20:44:47 +01:00
|
|
|
"""Close cover."""
|
2019-07-31 12:25:30 -07:00
|
|
|
await self._client.cover_command(key=self._static_info.key, position=0.0)
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
2018-12-17 20:44:47 +01:00
|
|
|
"""Stop the cover."""
|
2019-04-08 15:44:24 +02:00
|
|
|
await self._client.cover_command(key=self._static_info.key, stop=True)
|
|
|
|
|
2022-06-25 11:59:56 +02:00
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Move the cover to a specific position."""
|
2019-07-31 12:25:30 -07:00
|
|
|
await self._client.cover_command(
|
|
|
|
key=self._static_info.key, position=kwargs[ATTR_POSITION] / 100
|
|
|
|
)
|
2019-04-08 15:44:24 +02:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Open the cover tilt."""
|
|
|
|
await self._client.cover_command(key=self._static_info.key, tilt=1.0)
|
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Close the cover tilt."""
|
|
|
|
await self._client.cover_command(key=self._static_info.key, tilt=0.0)
|
2018-12-17 20:44:47 +01:00
|
|
|
|
2022-06-28 11:08:31 +02:00
|
|
|
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
2019-04-08 15:44:24 +02:00
|
|
|
"""Move the cover tilt to a specific position."""
|
2022-06-28 11:08:31 +02:00
|
|
|
tilt_position: int = kwargs[ATTR_TILT_POSITION]
|
2019-07-31 12:25:30 -07:00
|
|
|
await self._client.cover_command(
|
2022-06-28 11:08:31 +02:00
|
|
|
key=self._static_info.key, tilt=tilt_position / 100
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|