Improve Shelly light application/consumption type handling (#56461)

This commit is contained in:
Shay Levy 2021-09-20 21:12:18 +03:00 committed by GitHub
parent 5249c89c3f
commit 542f637ac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 17 deletions

View file

@ -51,7 +51,13 @@ from .const import (
STANDARD_RGB_EFFECTS,
)
from .entity import ShellyBlockEntity, ShellyRpcEntity
from .utils import async_remove_shelly_entity, get_device_entry_gen, get_rpc_key_ids
from .utils import (
async_remove_shelly_entity,
get_device_entry_gen,
get_rpc_key_ids,
is_block_channel_type_light,
is_rpc_channel_type_light,
)
_LOGGER: Final = logging.getLogger(__name__)
@ -82,10 +88,9 @@ async def async_setup_block_entry(
if block.type == "light":
blocks.append(block)
elif block.type == "relay":
app_type = wrapper.device.settings["relays"][int(block.channel)].get(
"appliance_type"
)
if not app_type or app_type.lower() != "light":
if not is_block_channel_type_light(
wrapper.device.settings, int(block.channel)
):
continue
blocks.append(block)
@ -110,8 +115,7 @@ async def async_setup_rpc_entry(
switch_ids = []
for id_ in switch_key_ids:
con_types = wrapper.device.config["sys"]["ui_data"].get("consumption_types")
if con_types is None or con_types[id_] != "lights":
if not is_rpc_channel_type_light(wrapper.device.config, id_):
continue
switch_ids.append(id_)

View file

@ -13,7 +13,13 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import BlockDeviceWrapper, RpcDeviceWrapper
from .const import BLOCK, DATA_CONFIG_ENTRY, DOMAIN, RPC
from .entity import ShellyBlockEntity, ShellyRpcEntity
from .utils import async_remove_shelly_entity, get_device_entry_gen, get_rpc_key_ids
from .utils import (
async_remove_shelly_entity,
get_device_entry_gen,
get_rpc_key_ids,
is_block_channel_type_light,
is_rpc_channel_type_light,
)
async def async_setup_entry(
@ -46,13 +52,9 @@ async def async_setup_block_entry(
relay_blocks = []
assert wrapper.device.blocks
for block in wrapper.device.blocks:
if block.type != "relay":
continue
app_type = wrapper.device.settings["relays"][int(block.channel)].get(
"appliance_type"
)
if app_type and app_type.lower() == "light":
if block.type != "relay" or is_block_channel_type_light(
wrapper.device.settings, int(block.channel)
):
continue
relay_blocks.append(block)
@ -76,8 +78,7 @@ async def async_setup_rpc_entry(
switch_ids = []
for id_ in switch_key_ids:
con_types = wrapper.device.config["sys"]["ui_data"].get("consumption_types")
if con_types is not None and con_types[id_] == "lights":
if is_rpc_channel_type_light(wrapper.device.config, id_):
continue
switch_ids.append(id_)

View file

@ -302,3 +302,15 @@ def get_rpc_key_ids(keys_dict: dict[str, Any], key: str) -> list[int]:
def is_rpc_momentary_input(config: dict[str, Any], key: str) -> bool:
"""Return true if rpc input button settings is set to a momentary type."""
return cast(bool, config[key]["type"] == "button")
def is_block_channel_type_light(settings: dict[str, Any], channel: int) -> bool:
"""Return true if block channel appliance type is set to light."""
app_type = settings["relays"][channel].get("appliance_type")
return app_type is not None and app_type.lower().startswith("light")
def is_rpc_channel_type_light(config: dict[str, Any], channel: int) -> bool:
"""Return true if rpc channel consumption type is set to light."""
con_types = config["sys"]["ui_data"].get("consumption_types")
return con_types is not None and con_types[channel].lower().startswith("light")