Move light constants to separate module (#132473)

This commit is contained in:
epenet 2024-12-06 20:25:17 +01:00 committed by GitHub
parent 49621aedb0
commit 3c06fe1e21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 86 additions and 62 deletions

View File

@ -5,8 +5,6 @@ from __future__ import annotations
from collections.abc import Iterable from collections.abc import Iterable
import csv import csv
import dataclasses import dataclasses
from datetime import timedelta
from enum import IntFlag, StrEnum
from functools import partial from functools import partial
import logging import logging
import os import os
@ -37,24 +35,22 @@ from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType, VolDictType from homeassistant.helpers.typing import ConfigType, VolDictType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
from homeassistant.util.hass_dict import HassKey
DOMAIN = "light" from .const import ( # noqa: F401
DATA_COMPONENT: HassKey[EntityComponent[LightEntity]] = HassKey(DOMAIN) COLOR_MODES_BRIGHTNESS,
COLOR_MODES_COLOR,
DATA_COMPONENT,
DATA_PROFILES,
DOMAIN,
SCAN_INTERVAL,
VALID_COLOR_MODES,
ColorMode,
LightEntityFeature,
)
ENTITY_ID_FORMAT = DOMAIN + ".{}" ENTITY_ID_FORMAT = DOMAIN + ".{}"
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
SCAN_INTERVAL = timedelta(seconds=30)
DATA_PROFILES: HassKey[Profiles] = HassKey(f"{DOMAIN}_profiles")
class LightEntityFeature(IntFlag):
"""Supported features of the light entity."""
EFFECT = 4
FLASH = 8
TRANSITION = 32
# These SUPPORT_* constants are deprecated as of Home Assistant 2022.5. # These SUPPORT_* constants are deprecated as of Home Assistant 2022.5.
@ -83,26 +79,6 @@ ATTR_COLOR_MODE = "color_mode"
# List of color modes supported by the light # List of color modes supported by the light
ATTR_SUPPORTED_COLOR_MODES = "supported_color_modes" ATTR_SUPPORTED_COLOR_MODES = "supported_color_modes"
class ColorMode(StrEnum):
"""Possible light color modes."""
UNKNOWN = "unknown"
"""Ambiguous color mode"""
ONOFF = "onoff"
"""Must be the only supported mode"""
BRIGHTNESS = "brightness"
"""Must be the only supported mode"""
COLOR_TEMP = "color_temp"
HS = "hs"
XY = "xy"
RGB = "rgb"
RGBW = "rgbw"
RGBWW = "rgbww"
WHITE = "white"
"""Must *NOT* be the only supported mode"""
# These COLOR_MODE_* constants are deprecated as of Home Assistant 2022.5. # These COLOR_MODE_* constants are deprecated as of Home Assistant 2022.5.
# Please use the LightEntityFeature enum instead. # Please use the LightEntityFeature enum instead.
_DEPRECATED_COLOR_MODE_UNKNOWN: Final = DeprecatedConstantEnum( _DEPRECATED_COLOR_MODE_UNKNOWN: Final = DeprecatedConstantEnum(
@ -122,25 +98,6 @@ _DEPRECATED_COLOR_MODE_RGBW: Final = DeprecatedConstantEnum(ColorMode.RGBW, "202
_DEPRECATED_COLOR_MODE_RGBWW: Final = DeprecatedConstantEnum(ColorMode.RGBWW, "2026.1") _DEPRECATED_COLOR_MODE_RGBWW: Final = DeprecatedConstantEnum(ColorMode.RGBWW, "2026.1")
_DEPRECATED_COLOR_MODE_WHITE: Final = DeprecatedConstantEnum(ColorMode.WHITE, "2026.1") _DEPRECATED_COLOR_MODE_WHITE: Final = DeprecatedConstantEnum(ColorMode.WHITE, "2026.1")
VALID_COLOR_MODES = {
ColorMode.ONOFF,
ColorMode.BRIGHTNESS,
ColorMode.COLOR_TEMP,
ColorMode.HS,
ColorMode.XY,
ColorMode.RGB,
ColorMode.RGBW,
ColorMode.RGBWW,
ColorMode.WHITE,
}
COLOR_MODES_BRIGHTNESS = VALID_COLOR_MODES - {ColorMode.ONOFF}
COLOR_MODES_COLOR = {
ColorMode.HS,
ColorMode.RGB,
ColorMode.RGBW,
ColorMode.RGBWW,
ColorMode.XY,
}
# mypy: disallow-any-generics # mypy: disallow-any-generics

View File

@ -0,0 +1,68 @@
"""Provides constants for lights."""
from __future__ import annotations
from datetime import timedelta
from enum import IntFlag, StrEnum
from typing import TYPE_CHECKING
from homeassistant.util.hass_dict import HassKey
if TYPE_CHECKING:
from homeassistant.helpers.entity_component import EntityComponent
from . import LightEntity, Profiles
DOMAIN = "light"
DATA_COMPONENT: HassKey[EntityComponent[LightEntity]] = HassKey(DOMAIN)
SCAN_INTERVAL = timedelta(seconds=30)
DATA_PROFILES: HassKey[Profiles] = HassKey(f"{DOMAIN}_profiles")
class LightEntityFeature(IntFlag):
"""Supported features of the light entity."""
EFFECT = 4
FLASH = 8
TRANSITION = 32
class ColorMode(StrEnum):
"""Possible light color modes."""
UNKNOWN = "unknown"
"""Ambiguous color mode"""
ONOFF = "onoff"
"""Must be the only supported mode"""
BRIGHTNESS = "brightness"
"""Must be the only supported mode"""
COLOR_TEMP = "color_temp"
HS = "hs"
XY = "xy"
RGB = "rgb"
RGBW = "rgbw"
RGBWW = "rgbww"
WHITE = "white"
"""Must *NOT* be the only supported mode"""
VALID_COLOR_MODES = {
ColorMode.ONOFF,
ColorMode.BRIGHTNESS,
ColorMode.COLOR_TEMP,
ColorMode.HS,
ColorMode.XY,
ColorMode.RGB,
ColorMode.RGBW,
ColorMode.RGBWW,
ColorMode.WHITE,
}
COLOR_MODES_BRIGHTNESS = VALID_COLOR_MODES - {ColorMode.ONOFF}
COLOR_MODES_COLOR = {
ColorMode.HS,
ColorMode.RGB,
ColorMode.RGBW,
ColorMode.RGBWW,
ColorMode.XY,
}

View File

@ -27,14 +27,13 @@ from . import (
ATTR_BRIGHTNESS_PCT, ATTR_BRIGHTNESS_PCT,
ATTR_BRIGHTNESS_STEP_PCT, ATTR_BRIGHTNESS_STEP_PCT,
ATTR_FLASH, ATTR_FLASH,
DOMAIN,
FLASH_SHORT, FLASH_SHORT,
VALID_BRIGHTNESS_PCT, VALID_BRIGHTNESS_PCT,
VALID_FLASH, VALID_FLASH,
LightEntityFeature,
brightness_supported, brightness_supported,
get_supported_color_modes, get_supported_color_modes,
) )
from .const import DOMAIN, LightEntityFeature
# mypy: disallow-any-generics # mypy: disallow-any-generics

View File

@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.condition import ConditionCheckerType from homeassistant.helpers.condition import ConditionCheckerType
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DOMAIN from .const import DOMAIN
# mypy: disallow-any-generics # mypy: disallow-any-generics

View File

@ -10,7 +10,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DOMAIN from .const import DOMAIN
TRIGGER_SCHEMA = vol.All( TRIGGER_SCHEMA = vol.All(
toggle_entity.TRIGGER_SCHEMA, toggle_entity.TRIGGER_SCHEMA,

View File

@ -11,7 +11,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv, intent from homeassistant.helpers import config_validation as cv, intent
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
from . import ATTR_BRIGHTNESS_PCT, ATTR_COLOR_TEMP_KELVIN, ATTR_RGB_COLOR, DOMAIN from . import ATTR_BRIGHTNESS_PCT, ATTR_COLOR_TEMP_KELVIN, ATTR_RGB_COLOR
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -28,9 +28,8 @@ from . import (
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_WHITE, ATTR_WHITE,
ATTR_XY_COLOR, ATTR_XY_COLOR,
DOMAIN,
ColorMode,
) )
from .const import DOMAIN, ColorMode
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)