mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ATTR_UNIQUE_ID = "unique_id"
|
||||||
|
|
||||||
CONF_START_TIME = "start_time"
|
CONF_START_TIME = "start_time"
|
||||||
CONF_STOP_TIME = "stop_time"
|
CONF_STOP_TIME = "stop_time"
|
||||||
CONF_START_CT = "start_colortemp"
|
CONF_START_CT = "start_colortemp"
|
||||||
@ -88,6 +90,7 @@ PLATFORM_SCHEMA = vol.Schema(
|
|||||||
),
|
),
|
||||||
vol.Optional(CONF_INTERVAL, default=30): cv.positive_int,
|
vol.Optional(CONF_INTERVAL, default=30): cv.positive_int,
|
||||||
vol.Optional(ATTR_TRANSITION, default=30): VALID_TRANSITION,
|
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)
|
mode = config.get(CONF_MODE)
|
||||||
interval = config.get(CONF_INTERVAL)
|
interval = config.get(CONF_INTERVAL)
|
||||||
transition = config.get(ATTR_TRANSITION)
|
transition = config.get(ATTR_TRANSITION)
|
||||||
|
unique_id = config.get(ATTR_UNIQUE_ID)
|
||||||
flux = FluxSwitch(
|
flux = FluxSwitch(
|
||||||
name,
|
name,
|
||||||
hass,
|
hass,
|
||||||
@ -165,6 +169,7 @@ async def async_setup_platform(
|
|||||||
mode,
|
mode,
|
||||||
interval,
|
interval,
|
||||||
transition,
|
transition,
|
||||||
|
unique_id,
|
||||||
)
|
)
|
||||||
async_add_entities([flux])
|
async_add_entities([flux])
|
||||||
|
|
||||||
@ -194,6 +199,7 @@ class FluxSwitch(SwitchEntity, RestoreEntity):
|
|||||||
mode,
|
mode,
|
||||||
interval,
|
interval,
|
||||||
transition,
|
transition,
|
||||||
|
unique_id,
|
||||||
):
|
):
|
||||||
"""Initialize the Flux switch."""
|
"""Initialize the Flux switch."""
|
||||||
self._name = name
|
self._name = name
|
||||||
@ -209,6 +215,7 @@ class FluxSwitch(SwitchEntity, RestoreEntity):
|
|||||||
self._mode = mode
|
self._mode = mode
|
||||||
self._interval = interval
|
self._interval = interval
|
||||||
self._transition = transition
|
self._transition = transition
|
||||||
|
self._attr_unique_id = unique_id
|
||||||
self.unsub_tracker = None
|
self.unsub_tracker = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -14,6 +14,7 @@ from homeassistant.const import (
|
|||||||
SUN_EVENT_SUNRISE,
|
SUN_EVENT_SUNRISE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
@ -52,6 +53,31 @@ async def test_valid_config(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == "off"
|
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:
|
async def test_restore_state_last_on(hass: HomeAssistant) -> None:
|
||||||
"""Test restoring state when the last state is on."""
|
"""Test restoring state when the last state is on."""
|
||||||
mock_restore_cache(hass, [State("switch.flux", "on")])
|
mock_restore_cache(hass, [State("switch.flux", "on")])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user