mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Add EntityFeature enum to Siren (#69585)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
340dd3ab82
commit
a61ac3ddc6
@ -20,7 +20,7 @@ from homeassistant.helpers.entity import ToggleEntity, ToggleEntityDescription
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import (
|
||||
from .const import ( # noqa: F401
|
||||
ATTR_AVAILABLE_TONES,
|
||||
ATTR_DURATION,
|
||||
ATTR_TONE,
|
||||
@ -31,6 +31,7 @@ from .const import (
|
||||
SUPPORT_TURN_OFF,
|
||||
SUPPORT_TURN_ON,
|
||||
SUPPORT_VOLUME_SET,
|
||||
SirenEntityFeature,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -62,7 +63,7 @@ def process_turn_on_params(
|
||||
"""
|
||||
supported_features = siren.supported_features or 0
|
||||
|
||||
if not supported_features & SUPPORT_TONES:
|
||||
if not supported_features & SirenEntityFeature.TONES:
|
||||
params.pop(ATTR_TONE, None)
|
||||
elif (tone := params.get(ATTR_TONE)) is not None:
|
||||
# Raise an exception if the specified tone isn't available
|
||||
@ -88,9 +89,9 @@ def process_turn_on_params(
|
||||
key for key, value in siren.available_tones.items() if value == tone
|
||||
)
|
||||
|
||||
if not supported_features & SUPPORT_DURATION:
|
||||
if not supported_features & SirenEntityFeature.DURATION:
|
||||
params.pop(ATTR_DURATION, None)
|
||||
if not supported_features & SUPPORT_VOLUME_SET:
|
||||
if not supported_features & SirenEntityFeature.VOLUME_SET:
|
||||
params.pop(ATTR_VOLUME_LEVEL, None)
|
||||
|
||||
return params
|
||||
@ -117,13 +118,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TURN_ON, TURN_ON_SCHEMA, async_handle_turn_on_service, [SUPPORT_TURN_ON]
|
||||
SERVICE_TURN_ON,
|
||||
TURN_ON_SCHEMA,
|
||||
async_handle_turn_on_service,
|
||||
[SirenEntityFeature.TURN_ON],
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TURN_OFF, {}, "async_turn_off", [SUPPORT_TURN_OFF]
|
||||
SERVICE_TURN_OFF, {}, "async_turn_off", [SirenEntityFeature.TURN_OFF]
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TOGGLE, {}, "async_toggle", [SUPPORT_TURN_ON & SUPPORT_TURN_OFF]
|
||||
SERVICE_TOGGLE,
|
||||
{},
|
||||
"async_toggle",
|
||||
[SirenEntityFeature.TURN_ON & SirenEntityFeature.TURN_OFF],
|
||||
)
|
||||
|
||||
return True
|
||||
@ -158,7 +165,10 @@ class SirenEntity(ToggleEntity):
|
||||
"""Return capability attributes."""
|
||||
supported_features = self.supported_features or 0
|
||||
|
||||
if supported_features & SUPPORT_TONES and self.available_tones is not None:
|
||||
if (
|
||||
supported_features & SirenEntityFeature.TONES
|
||||
and self.available_tones is not None
|
||||
):
|
||||
return {ATTR_AVAILABLE_TONES: self.available_tones}
|
||||
|
||||
return None
|
||||
@ -168,6 +178,6 @@ class SirenEntity(ToggleEntity):
|
||||
"""
|
||||
Return a list of available tones.
|
||||
|
||||
Requires SUPPORT_TONES.
|
||||
Requires SirenEntityFeature.TONES.
|
||||
"""
|
||||
return self._attr_available_tones
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""Constants for the siren component."""
|
||||
|
||||
from enum import IntEnum
|
||||
from typing import Final
|
||||
|
||||
DOMAIN: Final = "siren"
|
||||
@ -10,6 +11,19 @@ ATTR_AVAILABLE_TONES: Final = "available_tones"
|
||||
ATTR_DURATION: Final = "duration"
|
||||
ATTR_VOLUME_LEVEL: Final = "volume_level"
|
||||
|
||||
|
||||
class SirenEntityFeature(IntEnum):
|
||||
"""Supported features of the siren entity."""
|
||||
|
||||
TURN_ON = 1
|
||||
TURN_OFF = 2
|
||||
TONES = 4
|
||||
VOLUME_SET = 8
|
||||
DURATION = 16
|
||||
|
||||
|
||||
# These constants are deprecated as of Home Assistant 2022.5
|
||||
# Please use the SirenEntityFeature enum instead.
|
||||
SUPPORT_TURN_ON: Final = 1
|
||||
SUPPORT_TURN_OFF: Final = 2
|
||||
SUPPORT_TONES: Final = 4
|
||||
|
@ -4,7 +4,7 @@ from unittest.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.siren import SirenEntity, process_turn_on_params
|
||||
from homeassistant.components.siren.const import SUPPORT_TONES
|
||||
from homeassistant.components.siren.const import SirenEntityFeature
|
||||
|
||||
|
||||
class MockSirenEntity(SirenEntity):
|
||||
@ -42,7 +42,7 @@ async def test_sync_turn_off(hass):
|
||||
|
||||
async def test_no_available_tones(hass):
|
||||
"""Test ValueError when siren advertises tones but has no available_tones."""
|
||||
siren = MockSirenEntity(SUPPORT_TONES)
|
||||
siren = MockSirenEntity(SirenEntityFeature.TONES)
|
||||
siren.hass = hass
|
||||
with pytest.raises(ValueError):
|
||||
process_turn_on_params(siren, {"tone": "test"})
|
||||
@ -50,14 +50,14 @@ async def test_no_available_tones(hass):
|
||||
|
||||
async def test_available_tones_list(hass):
|
||||
"""Test that valid tones from tone list will get passed in."""
|
||||
siren = MockSirenEntity(SUPPORT_TONES, ["a", "b"])
|
||||
siren = MockSirenEntity(SirenEntityFeature.TONES, ["a", "b"])
|
||||
siren.hass = hass
|
||||
assert process_turn_on_params(siren, {"tone": "a"}) == {"tone": "a"}
|
||||
|
||||
|
||||
async def test_available_tones_dict(hass):
|
||||
"""Test that valid tones from available_tones dict will get passed in."""
|
||||
siren = MockSirenEntity(SUPPORT_TONES, {1: "a", 2: "b"})
|
||||
siren = MockSirenEntity(SirenEntityFeature.TONES, {1: "a", 2: "b"})
|
||||
siren.hass = hass
|
||||
assert process_turn_on_params(siren, {"tone": "a"}) == {"tone": 1}
|
||||
assert process_turn_on_params(siren, {"tone": 1}) == {"tone": 1}
|
||||
@ -65,7 +65,7 @@ async def test_available_tones_dict(hass):
|
||||
|
||||
async def test_missing_tones_list(hass):
|
||||
"""Test ValueError when setting a tone that is missing from available_tones list."""
|
||||
siren = MockSirenEntity(SUPPORT_TONES, ["a", "b"])
|
||||
siren = MockSirenEntity(SirenEntityFeature.TONES, ["a", "b"])
|
||||
siren.hass = hass
|
||||
with pytest.raises(ValueError):
|
||||
process_turn_on_params(siren, {"tone": "test"})
|
||||
@ -73,7 +73,7 @@ async def test_missing_tones_list(hass):
|
||||
|
||||
async def test_missing_tones_dict(hass):
|
||||
"""Test ValueError when setting a tone that is missing from available_tones dict."""
|
||||
siren = MockSirenEntity(SUPPORT_TONES, {1: "a", 2: "b"})
|
||||
siren = MockSirenEntity(SirenEntityFeature.TONES, {1: "a", 2: "b"})
|
||||
siren.hass = hass
|
||||
with pytest.raises(ValueError):
|
||||
process_turn_on_params(siren, {"tone": 3})
|
||||
|
Loading…
x
Reference in New Issue
Block a user