2018-12-17 19:44:47 +00:00
|
|
|
"""Support for ESPHome covers."""
|
2021-03-17 22:49:01 +00:00
|
|
|
from __future__ import annotations
|
2019-05-29 11:33:49 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2023-06-21 12:54:56 +00:00
|
|
|
from aioesphomeapi import APIVersion, CoverInfo, CoverOperation, CoverState, EntityInfo
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_POSITION,
|
|
|
|
ATTR_TILT_POSITION,
|
2022-12-06 12:29:58 +00:00
|
|
|
CoverDeviceClass,
|
2020-04-25 16:07:15 +00:00
|
|
|
CoverEntity,
|
2022-04-06 09:52:59 +00:00
|
|
|
CoverEntityFeature,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-12-17 19:44:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-06-21 12:54:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-07-12 20:56:10 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-02-02 11:34:01 +00:00
|
|
|
from homeassistant.util.enum import try_parse_enum
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2023-08-10 12:27:03 +00:00
|
|
|
from .entity import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
2018-12-17 19:44:47 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(
|
2021-07-12 20:56:10 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2019-07-31 19:25:30 +00:00
|
|
|
) -> None:
|
2018-12-17 19:44:47 +00:00
|
|
|
"""Set up ESPHome covers based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
info_type=CoverInfo,
|
|
|
|
entity_type=EsphomeCover,
|
|
|
|
state_type=CoverState,
|
2018-12-17 19:44:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
|
|
|
|
"""A cover implementation for ESPHome."""
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2023-06-21 12:54:56 +00:00
|
|
|
@callback
|
|
|
|
def _on_static_info_update(self, static_info: EntityInfo) -> None:
|
|
|
|
"""Set attrs from static info."""
|
|
|
|
super()._on_static_info_update(static_info)
|
|
|
|
static_info = self._static_info
|
2023-03-28 14:43:47 +00:00
|
|
|
flags = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
2023-06-21 12:54:56 +00:00
|
|
|
if self._api_version < APIVersion(1, 8) or static_info.supports_stop:
|
2023-03-28 14:43:47 +00:00
|
|
|
flags |= CoverEntityFeature.STOP
|
2023-06-21 12:54:56 +00:00
|
|
|
if static_info.supports_position:
|
2022-04-06 09:52:59 +00:00
|
|
|
flags |= CoverEntityFeature.SET_POSITION
|
2023-06-21 12:54:56 +00:00
|
|
|
if static_info.supports_tilt:
|
2022-04-06 09:52:59 +00:00
|
|
|
flags |= (
|
|
|
|
CoverEntityFeature.OPEN_TILT
|
|
|
|
| CoverEntityFeature.CLOSE_TILT
|
|
|
|
| CoverEntityFeature.SET_TILT_POSITION
|
|
|
|
)
|
2023-06-21 12:54:56 +00:00
|
|
|
self._attr_supported_features = flags
|
|
|
|
self._attr_device_class = try_parse_enum(
|
|
|
|
CoverDeviceClass, static_info.device_class
|
|
|
|
)
|
|
|
|
self._attr_assumed_state = static_info.assumed_state
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2022-09-26 20:10:06 +00:00
|
|
|
@property
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2021-03-17 22:49:01 +00:00
|
|
|
def is_closed(self) -> bool | None:
|
2018-12-17 19:44:47 +00:00
|
|
|
"""Return if the cover is closed or not."""
|
2019-04-08 13:44:24 +00:00
|
|
|
# Check closed state with api version due to a protocol change
|
2021-06-28 11:43:45 +00:00
|
|
|
return self._state.is_closed(self._api_version)
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2022-09-26 20:10:06 +00:00
|
|
|
@property
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
|
|
|
def is_opening(self) -> bool:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return self._state.current_operation == CoverOperation.IS_OPENING
|
|
|
|
|
2022-09-26 20:10:06 +00:00
|
|
|
@property
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
|
|
|
def is_closing(self) -> bool:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
return self._state.current_operation == CoverOperation.IS_CLOSING
|
|
|
|
|
2022-09-26 20:10:06 +00:00
|
|
|
@property
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2021-03-17 22:49:01 +00:00
|
|
|
def current_cover_position(self) -> int | None:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Return current position of cover. 0 is closed, 100 is open."""
|
2019-04-16 20:48:46 +00:00
|
|
|
if not self._static_info.supports_position:
|
2019-04-08 13:44:24 +00:00
|
|
|
return None
|
2019-09-29 10:06:51 +00:00
|
|
|
return round(self._state.position * 100.0)
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2022-09-26 20:10:06 +00:00
|
|
|
@property
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2021-07-12 20:56:10 +00:00
|
|
|
def current_cover_tilt_position(self) -> int | None:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Return current position of cover tilt. 0 is closed, 100 is open."""
|
2019-04-16 20:48:46 +00:00
|
|
|
if not self._static_info.supports_tilt:
|
2019-04-08 13:44:24 +00:00
|
|
|
return None
|
2021-07-12 20:56:10 +00:00
|
|
|
return round(self._state.tilt * 100.0)
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
2018-12-17 19:44:47 +00:00
|
|
|
"""Open the cover."""
|
2023-06-21 12:54:56 +00:00
|
|
|
await self._client.cover_command(key=self._key, position=1.0)
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
2018-12-17 19:44:47 +00:00
|
|
|
"""Close cover."""
|
2023-06-21 12:54:56 +00:00
|
|
|
await self._client.cover_command(key=self._key, position=0.0)
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
2018-12-17 19:44:47 +00:00
|
|
|
"""Stop the cover."""
|
2023-06-21 12:54:56 +00:00
|
|
|
await self._client.cover_command(key=self._key, stop=True)
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2022-06-25 09:59:56 +00:00
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Move the cover to a specific position."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await self._client.cover_command(
|
2023-06-21 12:54:56 +00:00
|
|
|
key=self._key, position=kwargs[ATTR_POSITION] / 100
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Open the cover tilt."""
|
2023-06-21 12:54:56 +00:00
|
|
|
await self._client.cover_command(key=self._key, tilt=1.0)
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Close the cover tilt."""
|
2023-06-21 12:54:56 +00:00
|
|
|
await self._client.cover_command(key=self._key, tilt=0.0)
|
2018-12-17 19:44:47 +00:00
|
|
|
|
2022-06-28 09:08:31 +00:00
|
|
|
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
2019-04-08 13:44:24 +00:00
|
|
|
"""Move the cover tilt to a specific position."""
|
2022-06-28 09:08:31 +00:00
|
|
|
tilt_position: int = kwargs[ATTR_TILT_POSITION]
|
2023-06-21 12:54:56 +00:00
|
|
|
await self._client.cover_command(key=self._key, tilt=tilt_position / 100)
|