mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add unique ID support to Flux (#120142)
This commit is contained in:
parent
9002d85f9b
commit
0feead385a
@ -50,6 +50,8 @@ from homeassistant.util.dt import as_local, utcnow as dt_utcnow
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_UNIQUE_ID = "unique_id"
|
||||
|
||||
CONF_START_TIME = "start_time"
|
||||
CONF_STOP_TIME = "stop_time"
|
||||
CONF_START_CT = "start_colortemp"
|
||||
@ -88,6 +90,7 @@ PLATFORM_SCHEMA = vol.Schema(
|
||||
),
|
||||
vol.Optional(CONF_INTERVAL, default=30): cv.positive_int,
|
||||
vol.Optional(ATTR_TRANSITION, default=30): VALID_TRANSITION,
|
||||
vol.Optional(ATTR_UNIQUE_ID): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
@ -151,6 +154,7 @@ async def async_setup_platform(
|
||||
mode = config.get(CONF_MODE)
|
||||
interval = config.get(CONF_INTERVAL)
|
||||
transition = config.get(ATTR_TRANSITION)
|
||||
unique_id = config.get(ATTR_UNIQUE_ID)
|
||||
flux = FluxSwitch(
|
||||
name,
|
||||
hass,
|
||||
@ -165,6 +169,7 @@ async def async_setup_platform(
|
||||
mode,
|
||||
interval,
|
||||
transition,
|
||||
unique_id,
|
||||
)
|
||||
async_add_entities([flux])
|
||||
|
||||
@ -194,6 +199,7 @@ class FluxSwitch(SwitchEntity, RestoreEntity):
|
||||
mode,
|
||||
interval,
|
||||
transition,
|
||||
unique_id,
|
||||
):
|
||||
"""Initialize the Flux switch."""
|
||||
self._name = name
|
||||
@ -209,6 +215,7 @@ class FluxSwitch(SwitchEntity, RestoreEntity):
|
||||
self._mode = mode
|
||||
self._interval = interval
|
||||
self._transition = transition
|
||||
self._attr_unique_id = unique_id
|
||||
self.unsub_tracker = None
|
||||
|
||||
@property
|
||||
|
@ -14,6 +14,7 @@ from homeassistant.const import (
|
||||
SUN_EVENT_SUNRISE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
@ -52,6 +53,31 @@ async def test_valid_config(hass: HomeAssistant) -> None:
|
||||
assert state.state == "off"
|
||||
|
||||
|
||||
async def test_unique_id(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test configuration with unique ID."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"switch",
|
||||
{
|
||||
"switch": {
|
||||
"platform": "flux",
|
||||
"name": "flux",
|
||||
"lights": ["light.desk", "light.lamp"],
|
||||
"unique_id": "zaphotbeeblebrox",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.flux")
|
||||
assert state
|
||||
assert state.state == "off"
|
||||
|
||||
assert len(entity_registry.entities) == 1
|
||||
assert entity_registry.async_get_entity_id("switch", "flux", "zaphotbeeblebrox")
|
||||
|
||||
|
||||
async def test_restore_state_last_on(hass: HomeAssistant) -> None:
|
||||
"""Test restoring state when the last state is on."""
|
||||
mock_restore_cache(hass, [State("switch.flux", "on")])
|
||||
|
Loading…
x
Reference in New Issue
Block a user