From 8eae572c930fd4a335714940d9ac4d10df109e27 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Mon, 25 Apr 2022 08:25:38 +0200 Subject: [PATCH] Mark type "On/Off Switch" as a deCONZ switch entity (#70598) Convert entity types of light platform to switch platform --- homeassistant/components/deconz/const.py | 9 ++++++- homeassistant/components/deconz/light.py | 13 ++++++++++ tests/components/deconz/test_switch.py | 32 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/deconz/const.py b/homeassistant/components/deconz/const.py index ca2e791f9e9..60ae610bd5a 100644 --- a/homeassistant/components/deconz/const.py +++ b/homeassistant/components/deconz/const.py @@ -1,6 +1,8 @@ """Constants for the deCONZ component.""" import logging +from pydeconz.models import ResourceType + from homeassistant.const import Platform LOGGER = logging.getLogger(__package__) @@ -45,7 +47,12 @@ ATTR_ON = "on" ATTR_VALVE = "valve" # Switches -POWER_PLUGS = ["On/Off light", "On/Off plug-in unit", "Smart plug"] +POWER_PLUGS = [ + ResourceType.ON_OFF_LIGHT.value, + ResourceType.ON_OFF_OUTPUT.value, + ResourceType.ON_OFF_PLUGIN_UNIT.value, + ResourceType.SMART_PLUG.value, +] CONF_ANGLE = "angle" CONF_GESTURE = "gesture" diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index e2ed4c06721..8f881ce4917 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -4,6 +4,7 @@ from __future__ import annotations from typing import Any, Generic, TypedDict, TypeVar from pydeconz.interfaces.lights import LightResources +from pydeconz.models import ResourceType from pydeconz.models.group import Group from pydeconz.models.light import ( ALERT_LONG, @@ -31,6 +32,7 @@ from homeassistant.components.light import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -70,6 +72,17 @@ async def async_setup_entry( gateway = get_gateway_from_config_entry(hass, config_entry) gateway.entities[DOMAIN] = set() + entity_registry = er.async_get(hass) + + # On/Off Output should be switch not light 2022.5 + for light in gateway.api.lights.lights.values(): + if light.type == ResourceType.ON_OFF_OUTPUT.value and ( + entity_id := entity_registry.async_get_entity_id( + DOMAIN, DECONZ_DOMAIN, light.unique_id + ) + ): + entity_registry.async_remove(entity_id) + @callback def async_add_light(lights: list[LightResources] | None = None) -> None: """Add light from deCONZ.""" diff --git a/tests/components/deconz/test_switch.py b/tests/components/deconz/test_switch.py index 99dcbe1089a..1893cead287 100644 --- a/tests/components/deconz/test_switch.py +++ b/tests/components/deconz/test_switch.py @@ -2,12 +2,15 @@ from unittest.mock import patch +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN +from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.switch import ( DOMAIN as SWITCH_DOMAIN, SERVICE_TURN_OFF, SERVICE_TURN_ON, ) from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE +from homeassistant.helpers import entity_registry as er from .test_gateway import ( DECONZ_WEB_REQUEST, @@ -107,3 +110,32 @@ async def test_power_plugs(hass, aioclient_mock, mock_deconz_websocket): await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 + + +async def test_remove_legacy_on_off_output_as_light(hass, aioclient_mock): + """Test that switch platform cleans up legacy light entities.""" + unique_id = "00:00:00:00:00:00:00:00-00" + + registry = er.async_get(hass) + switch_light_entity = registry.async_get_or_create( + LIGHT_DOMAIN, DECONZ_DOMAIN, unique_id + ) + + assert switch_light_entity + + data = { + "lights": { + "1": { + "name": "On Off output device", + "type": "On/Off output", + "state": {"on": True, "reachable": True}, + "uniqueid": unique_id, + }, + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + await setup_deconz_integration(hass, aioclient_mock) + + assert not registry.async_get("light.on_off_output_device") + assert registry.async_get("switch.on_off_output_device") + assert len(hass.states.async_all()) == 1