Handle Shelly light domain for relay switches ( fw >=1.9 ) (#42508)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
pull/42883/head
Simone Chemelli 2020-11-05 18:38:53 +01:00 committed by GitHub
parent 06a7fc491e
commit 05dc457955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View 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