Migrate cover device classes to StrEnum (#60655)

This commit is contained in:
Franck Nijhof 2021-12-01 00:37:34 +01:00 committed by GitHub
parent 51ebfade52
commit 98ce12c6ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 26 deletions

View file

@ -34,6 +34,7 @@ from homeassistant.helpers.config_validation import ( # noqa: F401
from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.util.enum import StrEnum
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs # mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
@ -44,31 +45,38 @@ SCAN_INTERVAL = timedelta(seconds=15)
ENTITY_ID_FORMAT = DOMAIN + ".{}" ENTITY_ID_FORMAT = DOMAIN + ".{}"
# Refer to the cover dev docs for device class descriptions
DEVICE_CLASS_AWNING = "awning"
DEVICE_CLASS_BLIND = "blind"
DEVICE_CLASS_CURTAIN = "curtain"
DEVICE_CLASS_DAMPER = "damper"
DEVICE_CLASS_DOOR = "door"
DEVICE_CLASS_GARAGE = "garage"
DEVICE_CLASS_GATE = "gate"
DEVICE_CLASS_SHADE = "shade"
DEVICE_CLASS_SHUTTER = "shutter"
DEVICE_CLASS_WINDOW = "window"
DEVICE_CLASSES = [ class CoverDeviceClass(StrEnum):
DEVICE_CLASS_AWNING, """Device class for cover."""
DEVICE_CLASS_BLIND,
DEVICE_CLASS_CURTAIN, # Refer to the cover dev docs for device class descriptions
DEVICE_CLASS_DAMPER, AWNING = "awning"
DEVICE_CLASS_DOOR, BLIND = "blind"
DEVICE_CLASS_GARAGE, CURTAIN = "curtain"
DEVICE_CLASS_GATE, DAMPER = "damper"
DEVICE_CLASS_SHADE, DOOR = "door"
DEVICE_CLASS_SHUTTER, GARAGE = "garage"
DEVICE_CLASS_WINDOW, GATE = "gate"
] SHADE = "shade"
DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.In(DEVICE_CLASSES)) SHUTTER = "shutter"
WINDOW = "window"
DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(CoverDeviceClass))
# DEVICE_CLASS* below are deprecated as of 2021.12
# use the CoverDeviceClass enum instead.
DEVICE_CLASSES = [cls.value for cls in CoverDeviceClass]
DEVICE_CLASS_AWNING = CoverDeviceClass.AWNING.value
DEVICE_CLASS_BLIND = CoverDeviceClass.BLIND.value
DEVICE_CLASS_CURTAIN = CoverDeviceClass.CURTAIN.value
DEVICE_CLASS_DAMPER = CoverDeviceClass.DAMPER.value
DEVICE_CLASS_DOOR = CoverDeviceClass.DOOR.value
DEVICE_CLASS_GARAGE = CoverDeviceClass.GARAGE.value
DEVICE_CLASS_GATE = CoverDeviceClass.GATE.value
DEVICE_CLASS_SHADE = CoverDeviceClass.SHADE.value
DEVICE_CLASS_SHUTTER = CoverDeviceClass.SHUTTER.value
DEVICE_CLASS_WINDOW = CoverDeviceClass.WINDOW.value
SUPPORT_OPEN = 1 SUPPORT_OPEN = 1
SUPPORT_CLOSE = 2 SUPPORT_CLOSE = 2
@ -175,6 +183,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class CoverEntityDescription(EntityDescription): class CoverEntityDescription(EntityDescription):
"""A class that describes cover entities.""" """A class that describes cover entities."""
device_class: CoverDeviceClass | str | None = None
class CoverEntity(Entity): class CoverEntity(Entity):
"""Base class for cover entities.""" """Base class for cover entities."""
@ -182,6 +192,7 @@ class CoverEntity(Entity):
entity_description: CoverEntityDescription entity_description: CoverEntityDescription
_attr_current_cover_position: int | None = None _attr_current_cover_position: int | None = None
_attr_current_cover_tilt_position: int | None = None _attr_current_cover_tilt_position: int | None = None
_attr_device_class: CoverDeviceClass | str | None
_attr_is_closed: bool | None _attr_is_closed: bool | None
_attr_is_closing: bool | None = None _attr_is_closing: bool | None = None
_attr_is_opening: bool | None = None _attr_is_opening: bool | None = None
@ -205,6 +216,15 @@ class CoverEntity(Entity):
""" """
return self._attr_current_cover_tilt_position return self._attr_current_cover_tilt_position
@property
def device_class(self) -> CoverDeviceClass | str | None:
"""Return the class of this entity."""
if hasattr(self, "_attr_device_class"):
return self._attr_device_class
if hasattr(self, "entity_description"):
return self.entity_description.device_class
return None
@property @property
@final @final
def state(self) -> str | None: def state(self) -> str | None:

View file

@ -1,4 +1,6 @@
"""Demo platform for the cover component.""" """Demo platform for the cover component."""
from __future__ import annotations
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_POSITION, ATTR_POSITION,
ATTR_TILT_POSITION, ATTR_TILT_POSITION,
@ -8,6 +10,7 @@ from homeassistant.components.cover import (
SUPPORT_OPEN_TILT, SUPPORT_OPEN_TILT,
SUPPORT_SET_TILT_POSITION, SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP_TILT, SUPPORT_STOP_TILT,
CoverDeviceClass,
CoverEntity, CoverEntity,
) )
from homeassistant.core import callback from homeassistant.core import callback
@ -28,7 +31,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
hass, hass,
"cover_4", "cover_4",
"Garage Door", "Garage Door",
device_class="garage", device_class=CoverDeviceClass.GARAGE,
supported_features=(SUPPORT_OPEN | SUPPORT_CLOSE), supported_features=(SUPPORT_OPEN | SUPPORT_CLOSE),
), ),
DemoCover( DemoCover(
@ -138,7 +141,7 @@ class DemoCover(CoverEntity):
return self._is_opening return self._is_opening
@property @property
def device_class(self): def device_class(self) -> CoverDeviceClass | None:
"""Return the class of this device, from component DEVICE_CLASSES.""" """Return the class of this device, from component DEVICE_CLASSES."""
return self._device_class return self._device_class