Use EntityFeature in group (#69393)

pull/69415/head
epenet 2022-04-06 11:56:05 +02:00 committed by GitHub
parent 0b18459ab8
commit a3e0a1d04d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 88 deletions

View File

@ -12,15 +12,8 @@ from homeassistant.components.cover import (
ATTR_TILT_POSITION,
DOMAIN,
PLATFORM_SCHEMA,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP,
SUPPORT_STOP_TILT,
CoverEntity,
CoverEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -154,28 +147,28 @@ class CoverGroup(GroupEntity, CoverEntity):
features = new_state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if features & (SUPPORT_OPEN | SUPPORT_CLOSE):
if features & (CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE):
self._covers[KEY_OPEN_CLOSE].add(entity_id)
else:
self._covers[KEY_OPEN_CLOSE].discard(entity_id)
if features & (SUPPORT_STOP):
if features & (CoverEntityFeature.STOP):
self._covers[KEY_STOP].add(entity_id)
else:
self._covers[KEY_STOP].discard(entity_id)
if features & (SUPPORT_SET_POSITION):
if features & (CoverEntityFeature.SET_POSITION):
self._covers[KEY_POSITION].add(entity_id)
else:
self._covers[KEY_POSITION].discard(entity_id)
if features & (SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT):
if features & (CoverEntityFeature.OPEN_TILT | CoverEntityFeature.CLOSE_TILT):
self._tilts[KEY_OPEN_CLOSE].add(entity_id)
else:
self._tilts[KEY_OPEN_CLOSE].discard(entity_id)
if features & (SUPPORT_STOP_TILT):
if features & (CoverEntityFeature.STOP_TILT):
self._tilts[KEY_STOP].add(entity_id)
else:
self._tilts[KEY_STOP].discard(entity_id)
if features & (SUPPORT_SET_TILT_POSITION):
if features & (CoverEntityFeature.SET_TILT_POSITION):
self._tilts[KEY_POSITION].add(entity_id)
else:
self._tilts[KEY_POSITION].discard(entity_id)
@ -320,18 +313,19 @@ class CoverGroup(GroupEntity, CoverEntity):
)
supported_features = 0
supported_features |= (
SUPPORT_OPEN | SUPPORT_CLOSE if self._covers[KEY_OPEN_CLOSE] else 0
)
supported_features |= SUPPORT_STOP if self._covers[KEY_STOP] else 0
supported_features |= SUPPORT_SET_POSITION if self._covers[KEY_POSITION] else 0
supported_features |= (
SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT if self._tilts[KEY_OPEN_CLOSE] else 0
)
supported_features |= SUPPORT_STOP_TILT if self._tilts[KEY_STOP] else 0
supported_features |= (
SUPPORT_SET_TILT_POSITION if self._tilts[KEY_POSITION] else 0
)
if self._covers[KEY_OPEN_CLOSE]:
supported_features |= CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
supported_features |= CoverEntityFeature.STOP if self._covers[KEY_STOP] else 0
if self._covers[KEY_POSITION]:
supported_features |= CoverEntityFeature.SET_POSITION
if self._tilts[KEY_OPEN_CLOSE]:
supported_features |= (
CoverEntityFeature.OPEN_TILT | CoverEntityFeature.CLOSE_TILT
)
if self._tilts[KEY_STOP]:
supported_features |= CoverEntityFeature.STOP_TILT
if self._tilts[KEY_POSITION]:
supported_features |= CoverEntityFeature.SET_TILT_POSITION
self._attr_supported_features = supported_features
if not self._attr_assumed_state:

View File

@ -20,10 +20,8 @@ from homeassistant.components.fan import (
SERVICE_SET_PERCENTAGE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
SUPPORT_DIRECTION,
SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED,
FanEntity,
FanEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -49,7 +47,11 @@ from .util import (
states_equal,
)
SUPPORTED_FLAGS = {SUPPORT_SET_SPEED, SUPPORT_DIRECTION, SUPPORT_OSCILLATE}
SUPPORTED_FLAGS = {
FanEntityFeature.SET_SPEED,
FanEntityFeature.DIRECTION,
FanEntityFeature.OSCILLATE,
}
DEFAULT_NAME = "Fan Group"
@ -191,19 +193,25 @@ class FanGroup(GroupEntity, FanEntity):
if percentage == 0:
await self.async_turn_off()
await self._async_call_supported_entities(
SERVICE_SET_PERCENTAGE, SUPPORT_SET_SPEED, {ATTR_PERCENTAGE: percentage}
SERVICE_SET_PERCENTAGE,
FanEntityFeature.SET_SPEED,
{ATTR_PERCENTAGE: percentage},
)
async def async_oscillate(self, oscillating: bool) -> None:
"""Oscillate the fan."""
await self._async_call_supported_entities(
SERVICE_OSCILLATE, SUPPORT_OSCILLATE, {ATTR_OSCILLATING: oscillating}
SERVICE_OSCILLATE,
FanEntityFeature.OSCILLATE,
{ATTR_OSCILLATING: oscillating},
)
async def async_set_direction(self, direction: str) -> None:
"""Set the direction of the fan."""
await self._async_call_supported_entities(
SERVICE_SET_DIRECTION, SUPPORT_DIRECTION, {ATTR_DIRECTION: direction}
SERVICE_SET_DIRECTION,
FanEntityFeature.DIRECTION,
{ATTR_DIRECTION: direction},
)
async def async_turn_on(
@ -268,7 +276,9 @@ class FanGroup(GroupEntity, FanEntity):
self._is_on = any(state.state == STATE_ON for state in on_states)
self._attr_assumed_state |= not states_equal(on_states)
percentage_states = self._async_states_by_support_flag(SUPPORT_SET_SPEED)
percentage_states = self._async_states_by_support_flag(
FanEntityFeature.SET_SPEED
)
self._percentage = reduce_attribute(percentage_states, ATTR_PERCENTAGE)
self._attr_assumed_state |= not attribute_equal(
percentage_states, ATTR_PERCENTAGE
@ -286,9 +296,11 @@ class FanGroup(GroupEntity, FanEntity):
self._speed_count = 100
self._set_attr_most_frequent(
"_oscillating", SUPPORT_OSCILLATE, ATTR_OSCILLATING
"_oscillating", FanEntityFeature.OSCILLATE, ATTR_OSCILLATING
)
self._set_attr_most_frequent(
"_direction", FanEntityFeature.DIRECTION, ATTR_DIRECTION
)
self._set_attr_most_frequent("_direction", SUPPORT_DIRECTION, ATTR_DIRECTION)
self._supported_features = reduce(
ior, [feature for feature in SUPPORTED_FLAGS if self._fans[feature]], 0

View File

@ -30,11 +30,9 @@ from homeassistant.components.light import (
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_ONOFF,
PLATFORM_SCHEMA,
SUPPORT_EFFECT,
SUPPORT_FLASH,
SUPPORT_TRANSITION,
SUPPORT_WHITE_VALUE,
LightEntity,
LightEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -74,7 +72,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
)
SUPPORT_GROUP_LIGHT = (
SUPPORT_EFFECT | SUPPORT_FLASH | SUPPORT_TRANSITION | SUPPORT_WHITE_VALUE
LightEntityFeature.EFFECT
| LightEntityFeature.FLASH
| LightEntityFeature.TRANSITION
| SUPPORT_WHITE_VALUE
)
_LOGGER = logging.getLogger(__name__)

View File

@ -16,21 +16,8 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
SERVICE_CLEAR_PLAYLIST,
SERVICE_PLAY_MEDIA,
SUPPORT_CLEAR_PLAYLIST,
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,
MediaPlayerEntity,
MediaPlayerEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -157,36 +144,47 @@ class MediaPlayerGroup(MediaPlayerEntity):
return
new_features = new_state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if new_features & SUPPORT_CLEAR_PLAYLIST:
if new_features & MediaPlayerEntityFeature.CLEAR_PLAYLIST:
self._features[KEY_CLEAR_PLAYLIST].add(entity_id)
else:
self._features[KEY_CLEAR_PLAYLIST].discard(entity_id)
if new_features & (SUPPORT_NEXT_TRACK | SUPPORT_PREVIOUS_TRACK):
if new_features & (
MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PREVIOUS_TRACK
):
self._features[KEY_TRACKS].add(entity_id)
else:
self._features[KEY_TRACKS].discard(entity_id)
if new_features & (SUPPORT_PAUSE | SUPPORT_PLAY | SUPPORT_STOP):
if new_features & (
MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.STOP
):
self._features[KEY_PAUSE_PLAY_STOP].add(entity_id)
else:
self._features[KEY_PAUSE_PLAY_STOP].discard(entity_id)
if new_features & SUPPORT_PLAY_MEDIA:
if new_features & MediaPlayerEntityFeature.PLAY_MEDIA:
self._features[KEY_PLAY_MEDIA].add(entity_id)
else:
self._features[KEY_PLAY_MEDIA].discard(entity_id)
if new_features & SUPPORT_SEEK:
if new_features & MediaPlayerEntityFeature.SEEK:
self._features[KEY_SEEK].add(entity_id)
else:
self._features[KEY_SEEK].discard(entity_id)
if new_features & SUPPORT_SHUFFLE_SET:
if new_features & MediaPlayerEntityFeature.SHUFFLE_SET:
self._features[KEY_SHUFFLE].add(entity_id)
else:
self._features[KEY_SHUFFLE].discard(entity_id)
if new_features & (SUPPORT_TURN_ON | SUPPORT_TURN_OFF):
if new_features & (
MediaPlayerEntityFeature.TURN_ON | MediaPlayerEntityFeature.TURN_OFF
):
self._features[KEY_ON_OFF].add(entity_id)
else:
self._features[KEY_ON_OFF].discard(entity_id)
if new_features & (
SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_STEP
MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_STEP
):
self._features[KEY_VOLUME].add(entity_id)
else:
@ -407,32 +405,35 @@ class MediaPlayerGroup(MediaPlayerEntity):
self._state = None
supported_features = 0
supported_features |= (
SUPPORT_CLEAR_PLAYLIST if self._features[KEY_CLEAR_PLAYLIST] else 0
)
supported_features |= (
SUPPORT_NEXT_TRACK | SUPPORT_PREVIOUS_TRACK
if self._features[KEY_TRACKS]
else 0
)
supported_features |= (
SUPPORT_PAUSE | SUPPORT_PLAY | SUPPORT_STOP
if self._features[KEY_PAUSE_PLAY_STOP]
else 0
)
supported_features |= (
SUPPORT_PLAY_MEDIA if self._features[KEY_PLAY_MEDIA] else 0
)
supported_features |= SUPPORT_SEEK if self._features[KEY_SEEK] else 0
supported_features |= SUPPORT_SHUFFLE_SET if self._features[KEY_SHUFFLE] else 0
supported_features |= (
SUPPORT_TURN_ON | SUPPORT_TURN_OFF if self._features[KEY_ON_OFF] else 0
)
supported_features |= (
SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_STEP
if self._features[KEY_VOLUME]
else 0
)
if self._features[KEY_CLEAR_PLAYLIST]:
supported_features |= MediaPlayerEntityFeature.CLEAR_PLAYLIST
if self._features[KEY_TRACKS]:
supported_features |= (
MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PREVIOUS_TRACK
)
if self._features[KEY_PAUSE_PLAY_STOP]:
supported_features |= (
MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.STOP
)
if self._features[KEY_PLAY_MEDIA]:
supported_features |= MediaPlayerEntityFeature.PLAY_MEDIA
if self._features[KEY_SEEK]:
supported_features |= MediaPlayerEntityFeature.SEEK
if self._features[KEY_SHUFFLE]:
supported_features |= MediaPlayerEntityFeature.SHUFFLE_SET
if self._features[KEY_ON_OFF]:
supported_features |= (
MediaPlayerEntityFeature.TURN_ON | MediaPlayerEntityFeature.TURN_OFF
)
if self._features[KEY_VOLUME]:
supported_features |= (
MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_STEP
)
self._supported_features = supported_features
self.async_write_ha_state()