Use EntityFeature enum in components (k**) (#69411)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
epenet 2022-04-07 15:31:54 +02:00 committed by GitHub
parent 2e7c65495b
commit 4a0e00d939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 114 deletions

View File

@ -1,5 +1,4 @@
"""Kaleidescape Media Player.""" """Kaleidescape Media Player."""
from __future__ import annotations from __future__ import annotations
import logging import logging
@ -7,15 +6,9 @@ from typing import TYPE_CHECKING
from kaleidescape import const as kaleidescape_const from kaleidescape import const as kaleidescape_const
from homeassistant.components.media_player import MediaPlayerEntity from homeassistant.components.media_player import (
from homeassistant.components.media_player.const import ( MediaPlayerEntity,
SUPPORT_NEXT_TRACK, MediaPlayerEntityFeature,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_STOP,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
) )
from homeassistant.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING from homeassistant.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -26,8 +19,6 @@ from .entity import KaleidescapeEntity
if TYPE_CHECKING: if TYPE_CHECKING:
from datetime import datetime from datetime import datetime
from kaleidescape import Device as KaleidescapeDevice
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -41,15 +32,6 @@ KALEIDESCAPE_PLAYING_STATES = [
KALEIDESCAPE_PAUSED_STATES = [kaleidescape_const.PLAY_STATUS_PAUSED] KALEIDESCAPE_PAUSED_STATES = [kaleidescape_const.PLAY_STATUS_PAUSED]
SUPPORTED_FEATURES = (
SUPPORT_TURN_ON
| SUPPORT_TURN_OFF
| SUPPORT_PLAY
| SUPPORT_PAUSE
| SUPPORT_STOP
| SUPPORT_NEXT_TRACK
| SUPPORT_PREVIOUS_TRACK
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -65,10 +47,15 @@ async def async_setup_entry(
class KaleidescapeMediaPlayer(KaleidescapeEntity, MediaPlayerEntity): class KaleidescapeMediaPlayer(KaleidescapeEntity, MediaPlayerEntity):
"""Representation of a Kaleidescape device.""" """Representation of a Kaleidescape device."""
def __init__(self, device: KaleidescapeDevice) -> None: _attr_supported_features = (
"""Initialize media player.""" MediaPlayerEntityFeature.TURN_ON
super().__init__(device) | MediaPlayerEntityFeature.TURN_OFF
self._attr_supported_features = SUPPORTED_FEATURES | MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.STOP
| MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PREVIOUS_TRACK
)
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Send leave standby command.""" """Send leave standby command."""

View File

@ -13,17 +13,8 @@ import voluptuous as vol
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature,
) )
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
@ -238,6 +229,20 @@ class KefMediaPlayer(MediaPlayerEntity):
self._dsp = None self._dsp = None
self._update_dsp_task_remover = None self._update_dsp_task_remover = None
self._attr_supported_features = (
MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_STEP
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.SELECT_SOURCE
| MediaPlayerEntityFeature.TURN_OFF
| MediaPlayerEntityFeature.NEXT_TRACK # only in Bluetooth and Wifi
| MediaPlayerEntityFeature.PAUSE # only in Bluetooth and Wifi
| MediaPlayerEntityFeature.PLAY # only in Bluetooth and Wifi
| MediaPlayerEntityFeature.PREVIOUS_TRACK # only in Bluetooth and Wifi
)
if supports_on:
self._attr_supported_features |= MediaPlayerEntityFeature.TURN_ON
@property @property
def name(self): def name(self):
"""Return the name of the device.""" """Return the name of the device."""
@ -283,25 +288,6 @@ class KefMediaPlayer(MediaPlayerEntity):
"""Boolean if volume is currently muted.""" """Boolean if volume is currently muted."""
return self._muted return self._muted
@property
def supported_features(self):
"""Flag media player features that are supported."""
support_kef = (
SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_STEP
| SUPPORT_VOLUME_MUTE
| SUPPORT_SELECT_SOURCE
| SUPPORT_TURN_OFF
| SUPPORT_NEXT_TRACK # only in Bluetooth and Wifi
| SUPPORT_PAUSE # only in Bluetooth and Wifi
| SUPPORT_PLAY # only in Bluetooth and Wifi
| SUPPORT_PREVIOUS_TRACK # only in Bluetooth and Wifi
)
if self._supports_on:
support_kef |= SUPPORT_TURN_ON
return support_kef
@property @property
def source(self): def source(self):
"""Name of the current input source.""" """Name of the current input source."""

View File

@ -8,14 +8,12 @@ from xknx.devices import Climate as XknxClimate, ClimateMode as XknxClimateMode
from xknx.dpt.dpt_hvac_mode import HVACControllerMode, HVACOperationMode from xknx.dpt.dpt_hvac_mode import HVACControllerMode, HVACOperationMode
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
CURRENT_HVAC_IDLE, CURRENT_HVAC_IDLE,
CURRENT_HVAC_OFF, CURRENT_HVAC_OFF,
HVAC_MODE_OFF, HVAC_MODE_OFF,
PRESET_AWAY, PRESET_AWAY,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
) )
from homeassistant.const import ( from homeassistant.const import (
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
@ -141,9 +139,9 @@ class KNXClimate(KnxEntity, ClimateEntity):
"""Initialize of a KNX climate device.""" """Initialize of a KNX climate device."""
super().__init__(_create_climate(xknx, config)) super().__init__(_create_climate(xknx, config))
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY) self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE self._attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
if self.preset_modes: if self.preset_modes:
self._attr_supported_features |= SUPPORT_PRESET_MODE self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
self._attr_target_temperature_step = self._device.temperature_step self._attr_target_temperature_step = self._device.temperature_step
self._attr_unique_id = ( self._attr_unique_id = (
f"{self._device.temperature.group_address_state}_" f"{self._device.temperature.group_address_state}_"
@ -254,7 +252,7 @@ class KNXClimate(KnxEntity, ClimateEntity):
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
"""Return the current preset mode, e.g., home, away, temp. """Return the current preset mode, e.g., home, away, temp.
Requires SUPPORT_PRESET_MODE. Requires ClimateEntityFeature.PRESET_MODE.
""" """
if self._device.mode is not None and self._device.mode.supports_operation_mode: if self._device.mode is not None and self._device.mode.supports_operation_mode:
return PRESET_MODES.get(self._device.mode.operation_mode.value, PRESET_AWAY) return PRESET_MODES.get(self._device.mode.operation_mode.value, PRESET_AWAY)
@ -264,7 +262,7 @@ class KNXClimate(KnxEntity, ClimateEntity):
def preset_modes(self) -> list[str] | None: def preset_modes(self) -> list[str] | None:
"""Return a list of available preset modes. """Return a list of available preset modes.
Requires SUPPORT_PRESET_MODE. Requires ClimateEntityFeature.PRESET_MODE.
""" """
if self._device.mode is None: if self._device.mode is None:
return None return None

View File

@ -12,16 +12,9 @@ from homeassistant import config_entries
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_POSITION, ATTR_POSITION,
ATTR_TILT_POSITION, ATTR_TILT_POSITION,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP,
SUPPORT_STOP_TILT,
CoverDeviceClass, CoverDeviceClass,
CoverEntity, CoverEntity,
CoverEntityFeature,
) )
from homeassistant.const import ( from homeassistant.const import (
CONF_DEVICE_CLASS, CONF_DEVICE_CLASS,
@ -84,20 +77,24 @@ class KNXCover(KnxEntity, CoverEntity):
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY) self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
_supports_tilt = False _supports_tilt = False
self._attr_supported_features = ( self._attr_supported_features = (
SUPPORT_CLOSE | SUPPORT_OPEN | SUPPORT_SET_POSITION CoverEntityFeature.CLOSE
| CoverEntityFeature.OPEN
| CoverEntityFeature.SET_POSITION
) )
if self._device.step.writable: if self._device.step.writable:
_supports_tilt = True _supports_tilt = True
self._attr_supported_features |= ( self._attr_supported_features |= (
SUPPORT_CLOSE_TILT | SUPPORT_OPEN_TILT | SUPPORT_STOP_TILT CoverEntityFeature.CLOSE_TILT
| CoverEntityFeature.OPEN_TILT
| CoverEntityFeature.STOP_TILT
) )
if self._device.supports_angle: if self._device.supports_angle:
_supports_tilt = True _supports_tilt = True
self._attr_supported_features |= SUPPORT_SET_TILT_POSITION self._attr_supported_features |= CoverEntityFeature.SET_TILT_POSITION
if self._device.supports_stop: if self._device.supports_stop:
self._attr_supported_features |= SUPPORT_STOP self._attr_supported_features |= CoverEntityFeature.STOP
if _supports_tilt: if _supports_tilt:
self._attr_supported_features |= SUPPORT_STOP_TILT self._attr_supported_features |= CoverEntityFeature.STOP_TILT
self._attr_device_class = config.get(CONF_DEVICE_CLASS) or ( self._attr_device_class = config.get(CONF_DEVICE_CLASS) or (
CoverDeviceClass.BLIND if _supports_tilt else None CoverDeviceClass.BLIND if _supports_tilt else None

View File

@ -8,7 +8,7 @@ from xknx import XKNX
from xknx.devices import Fan as XknxFan from xknx.devices import Fan as XknxFan
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.fan import SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, FanEntity from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -78,10 +78,10 @@ class KNXFan(KnxEntity, FanEntity):
@property @property
def supported_features(self) -> int: def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
flags = SUPPORT_SET_SPEED flags: int = FanEntityFeature.SET_SPEED
if self._device.supports_oscillation: if self._device.supports_oscillation:
flags |= SUPPORT_OSCILLATE flags |= FanEntityFeature.OSCILLATE
return flags return flags

View File

@ -14,7 +14,11 @@ from pykodi import CannotConnectError
import voluptuous as vol import voluptuous as vol
from homeassistant.components import media_source from homeassistant.components import media_source
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
MediaPlayerEntity,
MediaPlayerEntityFeature,
)
from homeassistant.components.media_player.browse_media import ( from homeassistant.components.media_player.browse_media import (
async_process_play_media_url, async_process_play_media_url,
) )
@ -31,20 +35,6 @@ from homeassistant.components.media_player.const import (
MEDIA_TYPE_TVSHOW, MEDIA_TYPE_TVSHOW,
MEDIA_TYPE_URL, MEDIA_TYPE_URL,
MEDIA_TYPE_VIDEO, MEDIA_TYPE_VIDEO,
SUPPORT_BROWSE_MEDIA,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SEEK,
SUPPORT_SHUFFLE_SET,
SUPPORT_STOP,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP,
) )
from homeassistant.components.media_player.errors import BrowseError from homeassistant.components.media_player.errors import BrowseError
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
@ -142,23 +132,6 @@ MAP_KODI_MEDIA_TYPES = {
MEDIA_TYPE_TVSHOW: "tvshowid", MEDIA_TYPE_TVSHOW: "tvshowid",
} }
SUPPORT_KODI = (
SUPPORT_BROWSE_MEDIA
| SUPPORT_NEXT_TRACK
| SUPPORT_PAUSE
| SUPPORT_PLAY
| SUPPORT_PLAY_MEDIA
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_SEEK
| SUPPORT_SHUFFLE_SET
| SUPPORT_STOP
| SUPPORT_TURN_OFF
| SUPPORT_TURN_ON
| SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_STEP
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
@ -300,6 +273,23 @@ def cmd(func):
class KodiEntity(MediaPlayerEntity): class KodiEntity(MediaPlayerEntity):
"""Representation of a XBMC/Kodi device.""" """Representation of a XBMC/Kodi device."""
_attr_supported_features = (
MediaPlayerEntityFeature.BROWSE_MEDIA
| MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.PREVIOUS_TRACK
| MediaPlayerEntityFeature.SEEK
| MediaPlayerEntityFeature.SHUFFLE_SET
| MediaPlayerEntityFeature.STOP
| MediaPlayerEntityFeature.TURN_OFF
| MediaPlayerEntityFeature.TURN_ON
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_STEP
)
def __init__(self, connection, kodi, name, uid): def __init__(self, connection, kodi, name, uid):
"""Initialize the Kodi entity.""" """Initialize the Kodi entity."""
self._connection = connection self._connection = connection
@ -647,11 +637,6 @@ class KodiEntity(MediaPlayerEntity):
return None return None
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_KODI
async def async_turn_on(self): async def async_turn_on(self):
"""Turn the media player on.""" """Turn the media player on."""
_LOGGER.debug("Firing event to turn on device") _LOGGER.debug("Firing event to turn on device")