Handle Shelly light domain for relay switches ( fw >=1.9 ) (#42508)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
06a7fc491e
commit
05dc457955
4 changed files with 54 additions and 2 deletions
|
@ -771,6 +771,7 @@ omit =
|
||||||
homeassistant/components/shelly/light.py
|
homeassistant/components/shelly/light.py
|
||||||
homeassistant/components/shelly/sensor.py
|
homeassistant/components/shelly/sensor.py
|
||||||
homeassistant/components/shelly/switch.py
|
homeassistant/components/shelly/switch.py
|
||||||
|
homeassistant/components/shelly/utils.py
|
||||||
homeassistant/components/sht31/sensor.py
|
homeassistant/components/sht31/sensor.py
|
||||||
homeassistant/components/sigfox/sensor.py
|
homeassistant/components/sigfox/sensor.py
|
||||||
homeassistant/components/simplepush/notify.py
|
homeassistant/components/simplepush/notify.py
|
||||||
|
|
|
@ -19,12 +19,29 @@ from homeassistant.util.color import (
|
||||||
from . import ShellyDeviceWrapper
|
from . import ShellyDeviceWrapper
|
||||||
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
from .entity import ShellyBlockEntity
|
from .entity import ShellyBlockEntity
|
||||||
|
from .utils import async_remove_entity_by_domain
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up lights for device."""
|
"""Set up lights for device."""
|
||||||
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id]
|
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:
|
if not blocks:
|
||||||
return
|
return
|
||||||
|
|
|
@ -7,6 +7,7 @@ from homeassistant.core import callback
|
||||||
from . import ShellyDeviceWrapper
|
from . import ShellyDeviceWrapper
|
||||||
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
from .entity import ShellyBlockEntity
|
from .entity import ShellyBlockEntity
|
||||||
|
from .utils import async_remove_entity_by_domain
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
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
|
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:
|
if not relay_blocks:
|
||||||
return
|
return
|
||||||
|
|
20
homeassistant/components/shelly/utils.py
Normal file
20
homeassistant/components/shelly/utils.py
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue