From 05dc457955681de4152afff0800d26a3e44ea13f Mon Sep 17 00:00:00 2001
From: Simone Chemelli <simone.chemelli@gmail.com>
Date: Thu, 5 Nov 2020 18:38:53 +0100
Subject: [PATCH] Handle Shelly light domain for relay switches ( fw >=1.9 )
 (#42508)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
---
 .coveragerc                               |  1 +
 homeassistant/components/shelly/light.py  | 19 ++++++++++++++++++-
 homeassistant/components/shelly/switch.py | 16 +++++++++++++++-
 homeassistant/components/shelly/utils.py  | 20 ++++++++++++++++++++
 4 files changed, 54 insertions(+), 2 deletions(-)
 create mode 100644 homeassistant/components/shelly/utils.py

diff --git a/.coveragerc b/.coveragerc
index c500956af30..9a7a1a52887 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -771,6 +771,7 @@ omit =
     homeassistant/components/shelly/light.py
     homeassistant/components/shelly/sensor.py
     homeassistant/components/shelly/switch.py
+    homeassistant/components/shelly/utils.py
     homeassistant/components/sht31/sensor.py
     homeassistant/components/sigfox/sensor.py
     homeassistant/components/simplepush/notify.py
diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py
index 85763a51f2a..7c4af9cf1ae 100644
--- a/homeassistant/components/shelly/light.py
+++ b/homeassistant/components/shelly/light.py
@@ -19,12 +19,29 @@ from homeassistant.util.color import (
 from . import ShellyDeviceWrapper
 from .const import DATA_CONFIG_ENTRY, DOMAIN
 from .entity import ShellyBlockEntity
+from .utils import async_remove_entity_by_domain
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):
     """Set up lights for device."""
     wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id]
-    blocks = [block for block in wrapper.device.blocks if block.type == "light"]
+
+    blocks = []
+    for block in wrapper.device.blocks:
+        if block.type == "light":
+            blocks.append(block)
+        elif (
+            block.type == "relay"
+            and wrapper.device.settings["relays"][int(block.channel)].get(
+                "appliance_type"
+            )
+            == "light"
+        ):
+            blocks.append(block)
+            unique_id = f'{wrapper.device.shelly["mac"]}-{block.type}_{block.channel}'
+            await async_remove_entity_by_domain(
+                hass, "switch", unique_id, config_entry.entry_id
+            )
 
     if not blocks:
         return
diff --git a/homeassistant/components/shelly/switch.py b/homeassistant/components/shelly/switch.py
index 7d140528858..48cb6d728e9 100644
--- a/homeassistant/components/shelly/switch.py
+++ b/homeassistant/components/shelly/switch.py
@@ -7,6 +7,7 @@ from homeassistant.core import callback
 from . import ShellyDeviceWrapper
 from .const import DATA_CONFIG_ENTRY, DOMAIN
 from .entity import ShellyBlockEntity
+from .utils import async_remove_entity_by_domain
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):
@@ -20,7 +21,20 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
     ):
         return
 
-    relay_blocks = [block for block in wrapper.device.blocks if block.type == "relay"]
+    relay_blocks = []
+    for block in wrapper.device.blocks:
+        if block.type == "relay" and (
+            wrapper.device.settings["relays"][int(block.channel)].get("appliance_type")
+            != "light"
+        ):
+            relay_blocks.append(block)
+            unique_id = f'{wrapper.device.shelly["mac"]}-{block.type}_{block.channel}'
+            await async_remove_entity_by_domain(
+                hass,
+                "light",
+                unique_id,
+                config_entry.entry_id,
+            )
 
     if not relay_blocks:
         return
diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py
new file mode 100644
index 00000000000..72ba4dbd3cc
--- /dev/null
+++ b/homeassistant/components/shelly/utils.py
@@ -0,0 +1,20 @@
+"""Shelly helpers functions."""
+
+import logging
+
+from homeassistant.helpers import entity_registry
+
+_LOGGER = logging.getLogger(__name__)
+
+
+async def async_remove_entity_by_domain(hass, domain, unique_id, config_entry_id):
+    """Remove entity by domain."""
+
+    entity_reg = await hass.helpers.entity_registry.async_get_registry()
+    for entry in entity_registry.async_entries_for_config_entry(
+        entity_reg, config_entry_id
+    ):
+        if entry.domain == domain and entry.unique_id == unique_id:
+            entity_reg.async_remove(entry.entity_id)
+            _LOGGER.debug("Removed %s domain for %s", domain, entry.original_name)
+            break
-- 
GitLab