mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Migrate esphome cover platform to use _on_static_info_update (#94959)
This commit is contained in:
parent
af97857c87
commit
1cb62d776e
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aioesphomeapi import APIVersion, CoverInfo, CoverOperation, CoverState
|
from aioesphomeapi import APIVersion, CoverInfo, CoverOperation, CoverState, EntityInfo
|
||||||
|
|
||||||
from homeassistant.components.cover import (
|
from homeassistant.components.cover import (
|
||||||
ATTR_POSITION,
|
ATTR_POSITION,
|
||||||
@ -13,7 +13,7 @@ from homeassistant.components.cover import (
|
|||||||
CoverEntityFeature,
|
CoverEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.enum import try_parse_enum
|
from homeassistant.util.enum import try_parse_enum
|
||||||
|
|
||||||
@ -38,32 +38,27 @@ async def async_setup_entry(
|
|||||||
class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
|
class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
|
||||||
"""A cover implementation for ESPHome."""
|
"""A cover implementation for ESPHome."""
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def supported_features(self) -> CoverEntityFeature:
|
def _on_static_info_update(self, static_info: EntityInfo) -> None:
|
||||||
"""Flag supported features."""
|
"""Set attrs from static info."""
|
||||||
|
super()._on_static_info_update(static_info)
|
||||||
|
static_info = self._static_info
|
||||||
flags = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
flags = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
||||||
|
if self._api_version < APIVersion(1, 8) or static_info.supports_stop:
|
||||||
if self._api_version < APIVersion(1, 8) or self._static_info.supports_stop:
|
|
||||||
flags |= CoverEntityFeature.STOP
|
flags |= CoverEntityFeature.STOP
|
||||||
if self._static_info.supports_position:
|
if static_info.supports_position:
|
||||||
flags |= CoverEntityFeature.SET_POSITION
|
flags |= CoverEntityFeature.SET_POSITION
|
||||||
if self._static_info.supports_tilt:
|
if static_info.supports_tilt:
|
||||||
flags |= (
|
flags |= (
|
||||||
CoverEntityFeature.OPEN_TILT
|
CoverEntityFeature.OPEN_TILT
|
||||||
| CoverEntityFeature.CLOSE_TILT
|
| CoverEntityFeature.CLOSE_TILT
|
||||||
| CoverEntityFeature.SET_TILT_POSITION
|
| CoverEntityFeature.SET_TILT_POSITION
|
||||||
)
|
)
|
||||||
return flags
|
self._attr_supported_features = flags
|
||||||
|
self._attr_device_class = try_parse_enum(
|
||||||
@property
|
CoverDeviceClass, static_info.device_class
|
||||||
def device_class(self) -> CoverDeviceClass | None:
|
)
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
self._attr_assumed_state = static_info.assumed_state
|
||||||
return try_parse_enum(CoverDeviceClass, self._static_info.device_class)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def assumed_state(self) -> bool:
|
|
||||||
"""Return true if we do optimistic updates."""
|
|
||||||
return self._static_info.assumed_state
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
@ -102,33 +97,31 @@ class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
|
|||||||
|
|
||||||
async def async_open_cover(self, **kwargs: Any) -> None:
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
||||||
"""Open the cover."""
|
"""Open the cover."""
|
||||||
await self._client.cover_command(key=self._static_info.key, position=1.0)
|
await self._client.cover_command(key=self._key, position=1.0)
|
||||||
|
|
||||||
async def async_close_cover(self, **kwargs: Any) -> None:
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
||||||
"""Close cover."""
|
"""Close cover."""
|
||||||
await self._client.cover_command(key=self._static_info.key, position=0.0)
|
await self._client.cover_command(key=self._key, position=0.0)
|
||||||
|
|
||||||
async def async_stop_cover(self, **kwargs: Any) -> None:
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
||||||
"""Stop the cover."""
|
"""Stop the cover."""
|
||||||
await self._client.cover_command(key=self._static_info.key, stop=True)
|
await self._client.cover_command(key=self._key, stop=True)
|
||||||
|
|
||||||
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
||||||
"""Move the cover to a specific position."""
|
"""Move the cover to a specific position."""
|
||||||
await self._client.cover_command(
|
await self._client.cover_command(
|
||||||
key=self._static_info.key, position=kwargs[ATTR_POSITION] / 100
|
key=self._key, position=kwargs[ATTR_POSITION] / 100
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
||||||
"""Open the cover tilt."""
|
"""Open the cover tilt."""
|
||||||
await self._client.cover_command(key=self._static_info.key, tilt=1.0)
|
await self._client.cover_command(key=self._key, tilt=1.0)
|
||||||
|
|
||||||
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
||||||
"""Close the cover tilt."""
|
"""Close the cover tilt."""
|
||||||
await self._client.cover_command(key=self._static_info.key, tilt=0.0)
|
await self._client.cover_command(key=self._key, tilt=0.0)
|
||||||
|
|
||||||
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
||||||
"""Move the cover tilt to a specific position."""
|
"""Move the cover tilt to a specific position."""
|
||||||
tilt_position: int = kwargs[ATTR_TILT_POSITION]
|
tilt_position: int = kwargs[ATTR_TILT_POSITION]
|
||||||
await self._client.cover_command(
|
await self._client.cover_command(key=self._key, tilt=tilt_position / 100)
|
||||||
key=self._static_info.key, tilt=tilt_position / 100
|
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user