diff --git a/homeassistant/components/flux/switch.py b/homeassistant/components/flux/switch.py index 63f58ff64c4..fac31d445cc 100644 --- a/homeassistant/components/flux/switch.py +++ b/homeassistant/components/flux/switch.py @@ -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 diff --git a/tests/components/flux/test_switch.py b/tests/components/flux/test_switch.py index baf568b79b4..ab85303584f 100644 --- a/tests/components/flux/test_switch.py +++ b/tests/components/flux/test_switch.py @@ -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")])