Use EntityFeature enum in components (f**) (#69389)

This commit is contained in:
epenet 2022-04-06 10:55:25 +02:00 committed by GitHub
parent 1fe5b1e68a
commit 9ab9fcfc56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 104 deletions

View file

@ -3,7 +3,11 @@ from __future__ import annotations
import logging import logging
from homeassistant.components.climate import ENTITY_ID_FORMAT, ClimateEntity from homeassistant.components.climate import (
ENTITY_ID_FORMAT,
ClimateEntity,
ClimateEntityFeature,
)
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
HVAC_MODE_AUTO, HVAC_MODE_AUTO,
HVAC_MODE_COOL, HVAC_MODE_COOL,
@ -13,9 +17,6 @@ from homeassistant.components.climate.const import (
HVAC_MODE_OFF, HVAC_MODE_OFF,
PRESET_AWAY, PRESET_AWAY,
PRESET_BOOST, PRESET_BOOST,
SUPPORT_FAN_MODE,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
@ -157,16 +158,16 @@ class FibaroThermostat(FibaroDevice, ClimateEntity):
or "setHeatingThermostatSetpoint" in device.actions or "setHeatingThermostatSetpoint" in device.actions
): ):
self._target_temp_device = FibaroDevice(device) self._target_temp_device = FibaroDevice(device)
self._support_flags |= SUPPORT_TARGET_TEMPERATURE self._support_flags |= ClimateEntityFeature.TARGET_TEMPERATURE
tempunit = device.properties.unit tempunit = device.properties.unit
if "setMode" in device.actions or "setOperatingMode" in device.actions: if "setMode" in device.actions or "setOperatingMode" in device.actions:
self._op_mode_device = FibaroDevice(device) self._op_mode_device = FibaroDevice(device)
self._support_flags |= SUPPORT_PRESET_MODE self._support_flags |= ClimateEntityFeature.PRESET_MODE
if "setFanMode" in device.actions: if "setFanMode" in device.actions:
self._fan_mode_device = FibaroDevice(device) self._fan_mode_device = FibaroDevice(device)
self._support_flags |= SUPPORT_FAN_MODE self._support_flags |= ClimateEntityFeature.FAN_MODE
if tempunit == "F": if tempunit == "F":
self._unit_of_temp = TEMP_FAHRENHEIT self._unit_of_temp = TEMP_FAHRENHEIT
@ -286,7 +287,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity):
def preset_mode(self): def preset_mode(self):
"""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 not self._op_mode_device: if not self._op_mode_device:
return None return None
@ -304,7 +305,7 @@ class FibaroThermostat(FibaroDevice, ClimateEntity):
def preset_modes(self): def preset_modes(self):
"""Return a list of available preset modes. """Return a list of available preset modes.
Requires SUPPORT_PRESET_MODE. Requires ClimateEntityFeature.PRESET_MODE.
""" """
if not self._op_mode_device: if not self._op_mode_device:
return None return None

View file

@ -10,11 +10,7 @@ from fjaraskupan import (
State, State,
) )
from homeassistant.components.fan import ( from homeassistant.components.fan import FanEntity, FanEntityFeature
SUPPORT_PRESET_MODE,
SUPPORT_SET_SPEED,
FanEntity,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -73,6 +69,8 @@ async def async_setup_entry(
class Fan(CoordinatorEntity[DataUpdateCoordinator[State]], FanEntity): class Fan(CoordinatorEntity[DataUpdateCoordinator[State]], FanEntity):
"""Fan entity.""" """Fan entity."""
_attr_supported_features = FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
def __init__( def __init__(
self, self,
coordinator: DataUpdateCoordinator[State], coordinator: DataUpdateCoordinator[State],
@ -155,11 +153,6 @@ class Fan(CoordinatorEntity[DataUpdateCoordinator[State]], FanEntity):
"""Return the current speed.""" """Return the current speed."""
return self._percentage return self._percentage
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_SET_SPEED | SUPPORT_PRESET_MODE
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if fan is on.""" """Return true if fan is on."""

View file

@ -5,12 +5,12 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.climate import PLATFORM_SCHEMA, ClimateEntity from homeassistant.components.climate import (
from homeassistant.components.climate.const import ( PLATFORM_SCHEMA,
HVAC_MODE_COOL, ClimateEntity,
SUPPORT_FAN_MODE, ClimateEntityFeature,
SUPPORT_TARGET_TEMPERATURE,
) )
from homeassistant.components.climate.const import HVAC_MODE_COOL
from homeassistant.components.modbus import get_hub from homeassistant.components.modbus import get_hub
from homeassistant.components.modbus.const import ( from homeassistant.components.modbus.const import (
CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_HOLDING,
@ -42,8 +42,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -61,6 +59,10 @@ async def async_setup_platform(
class Flexit(ClimateEntity): class Flexit(ClimateEntity):
"""Representation of a Flexit AC unit.""" """Representation of a Flexit AC unit."""
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
)
def __init__( def __init__(
self, hub: ModbusHub, modbus_slave: int | None, name: str | None self, hub: ModbusHub, modbus_slave: int | None, name: str | None
) -> None: ) -> None:
@ -83,11 +85,6 @@ class Flexit(ClimateEntity):
self._alarm = False self._alarm = False
self._outdoor_air_temp = None self._outdoor_air_temp = None
@property
def supported_features(self):
"""Return the list of supported features."""
return SUPPORT_FLAGS
async def async_update(self): async def async_update(self):
"""Update unit attributes.""" """Update unit attributes."""
self._target_temperature = await self._async_read_temp_from_register( self._target_temperature = await self._async_read_temp_from_register(

View file

@ -19,9 +19,8 @@ from homeassistant.components.light import (
ATTR_RGBW_COLOR, ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR, ATTR_RGBWW_COLOR,
ATTR_WHITE, ATTR_WHITE,
SUPPORT_EFFECT,
SUPPORT_TRANSITION,
LightEntity, LightEntity,
LightEntityFeature,
) )
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -192,7 +191,7 @@ class FluxLight(
): ):
"""Representation of a Flux light.""" """Representation of a Flux light."""
_attr_supported_features = SUPPORT_TRANSITION | SUPPORT_EFFECT _attr_supported_features = LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT
def __init__( def __init__(
self, self,

View file

@ -6,7 +6,7 @@ import asyncio
from libpyfoscam import FoscamCamera from libpyfoscam import FoscamCamera
import voluptuous as vol import voluptuous as vol
from homeassistant.components.camera import SUPPORT_STREAM, Camera from homeassistant.components.camera import Camera, CameraEntityFeature
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -106,6 +106,8 @@ class HassFoscamCamera(Camera):
self._unique_id = config_entry.entry_id self._unique_id = config_entry.entry_id
self._rtsp_port = config_entry.data[CONF_RTSP_PORT] self._rtsp_port = config_entry.data[CONF_RTSP_PORT]
self._motion_status = False self._motion_status = False
if self._rtsp_port:
self._attr_supported_features = CameraEntityFeature.STREAM
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Handle entity addition to hass.""" """Handle entity addition to hass."""
@ -145,14 +147,6 @@ class HassFoscamCamera(Camera):
return response return response
@property
def supported_features(self):
"""Return supported features."""
if self._rtsp_port:
return SUPPORT_STREAM
return None
async def stream_source(self): async def stream_source(self):
"""Return the stream source.""" """Return the stream source."""
if self._rtsp_port: if self._rtsp_port:

View file

@ -4,13 +4,12 @@ import logging
from pyfreedompro import put_state from pyfreedompro import put_state
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 (
ATTR_HVAC_MODE, ATTR_HVAC_MODE,
HVAC_MODE_COOL, HVAC_MODE_COOL,
HVAC_MODE_HEAT, HVAC_MODE_HEAT,
HVAC_MODE_OFF, HVAC_MODE_OFF,
SUPPORT_TARGET_TEMPERATURE,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, CONF_API_KEY, TEMP_CELSIUS from homeassistant.const import ATTR_TEMPERATURE, CONF_API_KEY, TEMP_CELSIUS
@ -72,7 +71,7 @@ class Device(CoordinatorEntity, ClimateEntity):
model=device["type"], model=device["type"],
name=self.name, name=self.name,
) )
self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE self._attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
self._attr_current_temperature = 0 self._attr_current_temperature = 0
self._attr_target_temperature = 0 self._attr_target_temperature = 0
self._attr_hvac_mode = HVAC_MODE_OFF self._attr_hvac_mode = HVAC_MODE_OFF

View file

@ -5,11 +5,9 @@ from pyfreedompro import put_state
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_POSITION, ATTR_POSITION,
SUPPORT_CLOSE,
SUPPORT_OPEN,
SUPPORT_SET_POSITION,
CoverDeviceClass, CoverDeviceClass,
CoverEntity, CoverEntity,
CoverEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY from homeassistant.const import CONF_API_KEY
@ -66,7 +64,9 @@ class Device(CoordinatorEntity, CoverEntity):
self._attr_current_cover_position = 0 self._attr_current_cover_position = 0
self._attr_is_closed = True self._attr_is_closed = True
self._attr_supported_features = ( self._attr_supported_features = (
SUPPORT_CLOSE | SUPPORT_OPEN | SUPPORT_SET_POSITION CoverEntityFeature.CLOSE
| CoverEntityFeature.OPEN
| CoverEntityFeature.SET_POSITION
) )
self._attr_device_class = DEVICE_CLASS_MAP[device["type"]] self._attr_device_class = DEVICE_CLASS_MAP[device["type"]]

View file

@ -5,7 +5,7 @@ import json
from pyfreedompro import put_state from pyfreedompro import put_state
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -51,6 +51,8 @@ class FreedomproFan(CoordinatorEntity, FanEntity):
) )
self._attr_is_on = False self._attr_is_on = False
self._attr_percentage = 0 self._attr_percentage = 0
if "rotationSpeed" in self._characteristics:
self._attr_supported_features = FanEntityFeature.SET_SPEED
@property @property
def is_on(self) -> bool | None: def is_on(self) -> bool | None:
@ -62,13 +64,6 @@ class FreedomproFan(CoordinatorEntity, FanEntity):
"""Return the current speed percentage.""" """Return the current speed percentage."""
return self._attr_percentage return self._attr_percentage
@property
def supported_features(self):
"""Flag supported features."""
if "rotationSpeed" in self._characteristics:
return SUPPORT_SET_SPEED
return 0
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator.""" """Handle updated data from the coordinator."""

View file

@ -3,15 +3,13 @@ from __future__ import annotations
from typing import Any from typing import Any
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 (
ATTR_HVAC_MODE, ATTR_HVAC_MODE,
HVAC_MODE_HEAT, HVAC_MODE_HEAT,
HVAC_MODE_OFF, HVAC_MODE_OFF,
PRESET_COMFORT, PRESET_COMFORT,
PRESET_ECO, PRESET_ECO,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -34,8 +32,6 @@ from .const import (
) )
from .model import ClimateExtraAttributes from .model import ClimateExtraAttributes
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE
OPERATION_LIST = [HVAC_MODE_HEAT, HVAC_MODE_OFF] OPERATION_LIST = [HVAC_MODE_HEAT, HVAC_MODE_OFF]
MIN_TEMPERATURE = 8 MIN_TEMPERATURE = 8
@ -68,10 +64,9 @@ async def async_setup_entry(
class FritzboxThermostat(FritzBoxEntity, ClimateEntity): class FritzboxThermostat(FritzBoxEntity, ClimateEntity):
"""The thermostat class for FRITZ!SmartHome thermostats.""" """The thermostat class for FRITZ!SmartHome thermostats."""
@property _attr_supported_features = (
def supported_features(self) -> int: ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
"""Return the list of supported features.""" )
return SUPPORT_FLAGS
@property @property
def temperature_unit(self) -> str: def temperature_unit(self) -> str:

View file

@ -7,23 +7,12 @@ from afsapi import AFSAPI
import requests import requests
import voluptuous as vol import voluptuous as vol
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity from homeassistant.components.media_player import (
from homeassistant.components.media_player.const import ( PLATFORM_SCHEMA,
MEDIA_TYPE_MUSIC, MediaPlayerEntity,
SUPPORT_NEXT_TRACK, MediaPlayerEntityFeature,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SEEK,
SUPPORT_SELECT_SOURCE,
SUPPORT_STOP,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP,
) )
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_NAME, CONF_NAME,
@ -42,22 +31,6 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SUPPORT_FRONTIER_SILICON = (
SUPPORT_PAUSE
| SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_STEP
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_NEXT_TRACK
| SUPPORT_SEEK
| SUPPORT_PLAY_MEDIA
| SUPPORT_PLAY
| SUPPORT_STOP
| SUPPORT_TURN_ON
| SUPPORT_TURN_OFF
| SUPPORT_SELECT_SOURCE
)
DEFAULT_PORT = 80 DEFAULT_PORT = 80
DEFAULT_PASSWORD = "1234" DEFAULT_PASSWORD = "1234"
@ -104,6 +77,22 @@ async def async_setup_platform(
class AFSAPIDevice(MediaPlayerEntity): class AFSAPIDevice(MediaPlayerEntity):
"""Representation of a Frontier Silicon device on the network.""" """Representation of a Frontier Silicon device on the network."""
_attr_supported_features = (
MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_STEP
| MediaPlayerEntityFeature.PREVIOUS_TRACK
| MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.SEEK
| MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.STOP
| MediaPlayerEntityFeature.TURN_ON
| MediaPlayerEntityFeature.TURN_OFF
| MediaPlayerEntityFeature.SELECT_SOURCE
)
def __init__(self, device_url, password, name): def __init__(self, device_url, password, name):
"""Initialize the Frontier Silicon API device.""" """Initialize the Frontier Silicon API device."""
self._device_url = device_url self._device_url = device_url
@ -158,11 +147,6 @@ class AFSAPIDevice(MediaPlayerEntity):
"""Content type of current playing media.""" """Content type of current playing media."""
return MEDIA_TYPE_MUSIC return MEDIA_TYPE_MUSIC
@property
def supported_features(self):
"""Flag of media commands that are supported."""
return SUPPORT_FRONTIER_SILICON
@property @property
def state(self): def state(self):
"""Return the state of the player.""" """Return the state of the player."""