Use EntityFeature enum in components (x**) (#69469)

This commit is contained in:
epenet 2022-04-07 14:39:09 +02:00 committed by GitHub
parent 3c57adefdb
commit 1ec08d2fe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 86 deletions

View File

@ -13,21 +13,11 @@ from xbox.webapi.api.provider.smartglass.models import (
VolumeDirection,
)
from homeassistant.components.media_player import MediaPlayerEntity
from homeassistant.components.media_player.const import (
MEDIA_TYPE_APP,
MEDIA_TYPE_GAME,
SUPPORT_BROWSE_MEDIA,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_STEP,
from homeassistant.components.media_player import (
MediaPlayerEntity,
MediaPlayerEntityFeature,
)
from homeassistant.components.media_player.const import MEDIA_TYPE_APP, MEDIA_TYPE_GAME
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING
from homeassistant.core import HomeAssistant
@ -40,16 +30,16 @@ from .browse_media import build_item_response
from .const import DOMAIN
SUPPORT_XBOX = (
SUPPORT_TURN_ON
| SUPPORT_TURN_OFF
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_NEXT_TRACK
| SUPPORT_PLAY
| SUPPORT_PAUSE
| SUPPORT_VOLUME_STEP
| SUPPORT_VOLUME_MUTE
| SUPPORT_BROWSE_MEDIA
| SUPPORT_PLAY_MEDIA
MediaPlayerEntityFeature.TURN_ON
| MediaPlayerEntityFeature.TURN_OFF
| MediaPlayerEntityFeature.PREVIOUS_TRACK
| MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.VOLUME_STEP
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.BROWSE_MEDIA
| MediaPlayerEntityFeature.PLAY_MEDIA
)
XBOX_STATE_MAP = {
@ -119,7 +109,11 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
def supported_features(self):
"""Flag media player features that are supported."""
if self.state not in [STATE_PLAYING, STATE_PAUSED]:
return SUPPORT_XBOX & ~SUPPORT_NEXT_TRACK & ~SUPPORT_PREVIOUS_TRACK
return (
SUPPORT_XBOX
& ~MediaPlayerEntityFeature.NEXT_TRACK
& ~MediaPlayerEntityFeature.PREVIOUS_TRACK
)
return SUPPORT_XBOX
@property

View File

@ -5,8 +5,8 @@ import logging
from miio import DeviceException
from homeassistant.components.alarm_control_panel import (
SUPPORT_ALARM_ARM_AWAY,
AlarmControlPanelEntity,
AlarmControlPanelEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -49,6 +49,8 @@ async def async_setup_entry(
class XiaomiGatewayAlarm(AlarmControlPanelEntity):
"""Representation of the XiaomiGatewayAlarm."""
_attr_supported_features = AlarmControlPanelEntityFeature.ARM_AWAY
def __init__(
self, gateway_device, gateway_name, model, mac_address, gateway_device_id
):
@ -98,11 +100,6 @@ class XiaomiGatewayAlarm(AlarmControlPanelEntity):
"""Return the state of the device."""
return self._state
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_AWAY
async def _try_command(self, mask_error, func, *args, **kwargs):
"""Call a device command handling error messages."""
try:

View File

@ -17,13 +17,7 @@ from miio.integrations.fan.zhimi.zhimi_miot import (
)
import voluptuous as vol
from homeassistant.components.fan import (
SUPPORT_DIRECTION,
SUPPORT_OSCILLATE,
SUPPORT_PRESET_MODE,
SUPPORT_SET_SPEED,
FanEntity,
)
from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, CONF_MODEL
from homeassistant.core import HomeAssistant, ServiceCall, callback
@ -289,14 +283,8 @@ class XiaomiGenericDevice(XiaomiCoordinatedMiioEntity, FanEntity):
self._fan_level = None
self._state_attrs = {}
self._device_features = 0
self._supported_features = 0
self._preset_modes = []
@property
def supported_features(self):
"""Flag supported features."""
return self._supported_features
@property
@abstractmethod
def operation_mode_class(self):
@ -412,37 +400,39 @@ class XiaomiAirPurifier(XiaomiGenericAirPurifier):
self._device_features = FEATURE_FLAGS_AIRPURIFIER_PRO
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_PRO
self._preset_modes = PRESET_MODES_AIRPURIFIER_PRO
self._supported_features = SUPPORT_PRESET_MODE
self._attr_supported_features = FanEntityFeature.PRESET_MODE
self._speed_count = 1
elif self._model == MODEL_AIRPURIFIER_PRO_V7:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_PRO_V7
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_PRO_V7
self._preset_modes = PRESET_MODES_AIRPURIFIER_PRO_V7
self._supported_features = SUPPORT_PRESET_MODE
self._attr_supported_features = FanEntityFeature.PRESET_MODE
self._speed_count = 1
elif self._model in [MODEL_AIRPURIFIER_2S, MODEL_AIRPURIFIER_2H]:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_2S
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_COMMON
self._preset_modes = PRESET_MODES_AIRPURIFIER_2S
self._supported_features = SUPPORT_PRESET_MODE
self._attr_supported_features = FanEntityFeature.PRESET_MODE
self._speed_count = 1
elif self._model in MODELS_PURIFIER_MIOT:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_MIOT
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_MIOT
self._preset_modes = PRESET_MODES_AIRPURIFIER_MIOT
self._supported_features = SUPPORT_SET_SPEED | SUPPORT_PRESET_MODE
self._attr_supported_features = (
FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
)
self._speed_count = 3
elif self._model == MODEL_AIRPURIFIER_V3:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_V3
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_V3
self._preset_modes = PRESET_MODES_AIRPURIFIER_V3
self._supported_features = SUPPORT_PRESET_MODE
self._attr_supported_features = FanEntityFeature.PRESET_MODE
self._speed_count = 1
else:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_MIIO
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER
self._preset_modes = PRESET_MODES_AIRPURIFIER
self._supported_features = SUPPORT_PRESET_MODE
self._attr_supported_features = FanEntityFeature.PRESET_MODE
self._speed_count = 1
self._state = self.coordinator.data.is_on
@ -577,7 +567,7 @@ class XiaomiAirPurifierMB4(XiaomiGenericAirPurifier):
self._device_features = FEATURE_FLAGS_AIRPURIFIER_3C
self._preset_modes = PRESET_MODES_AIRPURIFIER_3C
self._supported_features = SUPPORT_PRESET_MODE
self._attr_supported_features = FanEntityFeature.PRESET_MODE
self._state = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value
@ -633,7 +623,9 @@ class XiaomiAirFresh(XiaomiGenericAirPurifier):
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRFRESH
self._speed_count = 4
self._preset_modes = PRESET_MODES_AIRFRESH
self._supported_features = SUPPORT_SET_SPEED | SUPPORT_PRESET_MODE
self._attr_supported_features = (
FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
)
self._state = self.coordinator.data.is_on
self._state_attrs.update(
@ -727,7 +719,9 @@ class XiaomiAirFreshA1(XiaomiGenericAirPurifier):
self._favorite_speed = None
self._device_features = FEATURE_FLAGS_AIRFRESH_A1
self._preset_modes = PRESET_MODES_AIRFRESH_A1
self._supported_features = SUPPORT_SET_SPEED | SUPPORT_PRESET_MODE
self._attr_supported_features = (
FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
)
self._state = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value
@ -820,11 +814,13 @@ class XiaomiGenericFan(XiaomiGenericDevice):
self._device_features = FEATURE_FLAGS_FAN_P10_P11
else:
self._device_features = FEATURE_FLAGS_FAN
self._supported_features = (
SUPPORT_SET_SPEED | SUPPORT_OSCILLATE | SUPPORT_PRESET_MODE
self._attr_supported_features = (
FanEntityFeature.SET_SPEED
| FanEntityFeature.OSCILLATE
| FanEntityFeature.PRESET_MODE
)
if self._model != MODEL_FAN_1C:
self._supported_features |= SUPPORT_DIRECTION
self._attr_supported_features |= FanEntityFeature.DIRECTION
self._preset_mode = None
self._oscillating = None
self._percentage = None

View File

@ -6,8 +6,11 @@ from miio.airhumidifier import OperationMode as AirhumidifierOperationMode
from miio.airhumidifier_miot import OperationMode as AirhumidifierMiotOperationMode
from miio.airhumidifier_mjjsq import OperationMode as AirhumidifierMjjsqOperationMode
from homeassistant.components.humidifier import HumidifierDeviceClass, HumidifierEntity
from homeassistant.components.humidifier.const import SUPPORT_MODES
from homeassistant.components.humidifier import (
HumidifierDeviceClass,
HumidifierEntity,
HumidifierEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_MODE, CONF_MODEL
from homeassistant.core import HomeAssistant, callback
@ -108,7 +111,7 @@ class XiaomiGenericHumidifier(XiaomiCoordinatedMiioEntity, HumidifierEntity):
"""Representation of a generic Xiaomi humidifier device."""
_attr_device_class = HumidifierDeviceClass.HUMIDIFIER
_attr_supported_features = SUPPORT_MODES
_attr_supported_features = HumidifierEntityFeature.MODES
def __init__(self, name, device, entry, unique_id, coordinator):
"""Initialize the generic Xiaomi device."""
@ -243,7 +246,7 @@ class XiaomiAirHumidifier(XiaomiGenericHumidifier, HumidifierEntity):
):
self._target_humidity = target_humidity
if (
self.supported_features & SUPPORT_MODES == 0
self.supported_features & HumidifierEntityFeature.MODES == 0
or AirhumidifierOperationMode(self._attributes[ATTR_MODE])
== AirhumidifierOperationMode.Auto
or AirhumidifierOperationMode.Auto.name not in self.available_modes
@ -261,7 +264,7 @@ class XiaomiAirHumidifier(XiaomiGenericHumidifier, HumidifierEntity):
async def async_set_mode(self, mode: str) -> None:
"""Set the mode of the humidifier."""
if self.supported_features & SUPPORT_MODES == 0 or not mode:
if self.supported_features & HumidifierEntityFeature.MODES == 0 or not mode:
return
if mode not in self.available_modes:
@ -321,7 +324,7 @@ class XiaomiAirHumidifierMiot(XiaomiAirHumidifier):
):
self._target_humidity = target_humidity
if (
self.supported_features & SUPPORT_MODES == 0
self.supported_features & HumidifierEntityFeature.MODES == 0
or AirhumidifierMiotOperationMode(self._attributes[ATTR_MODE])
== AirhumidifierMiotOperationMode.Auto
):
@ -338,7 +341,7 @@ class XiaomiAirHumidifierMiot(XiaomiAirHumidifier):
async def async_set_mode(self, mode: str) -> None:
"""Set the mode of the fan."""
if self.supported_features & SUPPORT_MODES == 0 or not mode:
if self.supported_features & HumidifierEntityFeature.MODES == 0 or not mode:
return
if mode not in self.REVERSE_MODE_MAPPING:
@ -396,7 +399,7 @@ class XiaomiAirHumidifierMjjsq(XiaomiAirHumidifier):
):
self._target_humidity = target_humidity
if (
self.supported_features & SUPPORT_MODES == 0
self.supported_features & HumidifierEntityFeature.MODES == 0
or AirhumidifierMjjsqOperationMode(self._attributes[ATTR_MODE])
== AirhumidifierMjjsqOperationMode.Humidity
):

View File

@ -6,11 +6,10 @@ import logging
import pymitv
import voluptuous as vol
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
from homeassistant.components.media_player.const import (
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_STEP,
from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
MediaPlayerEntity,
MediaPlayerEntityFeature,
)
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
@ -22,8 +21,6 @@ DEFAULT_NAME = "Xiaomi TV"
_LOGGER = logging.getLogger(__name__)
SUPPORT_XIAOMI_TV = SUPPORT_VOLUME_STEP | SUPPORT_TURN_ON | SUPPORT_TURN_OFF
# No host is needed for configuration, however it can be set.
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
@ -60,6 +57,12 @@ def setup_platform(
class XiaomiTV(MediaPlayerEntity):
"""Represent the Xiaomi TV for Home Assistant."""
_attr_supported_features = (
MediaPlayerEntityFeature.VOLUME_STEP
| MediaPlayerEntityFeature.TURN_ON
| MediaPlayerEntityFeature.TURN_OFF
)
def __init__(self, ip, name):
"""Receive IP address and name to construct class."""
@ -84,11 +87,6 @@ class XiaomiTV(MediaPlayerEntity):
"""Indicate that state is assumed."""
return True
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_XIAOMI_TV
def turn_off(self):
"""
Instruct the TV to turn sleep.

View File

@ -3,11 +3,8 @@ from __future__ import annotations
from xs1_api_client.api_constants import ActuatorType
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
HVAC_MODE_HEAT,
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature
from homeassistant.components.climate.const import HVAC_MODE_HEAT
from homeassistant.const import ATTR_TEMPERATURE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -51,6 +48,8 @@ def setup_platform(
class XS1ThermostatEntity(XS1DeviceEntity, ClimateEntity):
"""Representation of a XS1 thermostat."""
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
def __init__(self, device, sensor):
"""Initialize the actuator."""
super().__init__(device)
@ -61,11 +60,6 @@ class XS1ThermostatEntity(XS1DeviceEntity, ClimateEntity):
"""Return the name of the device if any."""
return self.device.name()
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_TARGET_TEMPERATURE
@property
def hvac_mode(self):
"""Return hvac operation ie. heat, cool mode.