Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 2 additions & 50 deletions homeassistant/components/shelly/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import aioshelly

from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import callback
from homeassistant.helpers import device_registry, entity, update_coordinator

Expand All @@ -13,53 +12,6 @@
from .utils import get_entity_name, get_rest_value_from_path


def temperature_unit(block_info: dict) -> str:
"""Detect temperature unit."""
if block_info[aioshelly.BLOCK_VALUE_UNIT] == "F":
return TEMP_FAHRENHEIT
return TEMP_CELSIUS


def shelly_naming(self, block, entity_type: str):
"""Naming for switch and sensors."""

entity_name = self.wrapper.name
if not block:
return f"{entity_name} {self.description.name}"

channels = 0
mode = block.type + "s"
if "num_outputs" in self.wrapper.device.shelly:
channels = self.wrapper.device.shelly["num_outputs"]
if (
self.wrapper.model in ["SHSW-21", "SHSW-25"]
and self.wrapper.device.settings["mode"] == "roller"
):
channels = 1
if block.type == "emeter" and "num_emeters" in self.wrapper.device.shelly:
channels = self.wrapper.device.shelly["num_emeters"]
if channels > 1 and block.type != "device":
# Shelly EM (SHEM) with firmware v1.8.1 doesn't have "name" key; will be fixed in next firmware release
if "name" in self.wrapper.device.settings[mode][int(block.channel)]:
entity_name = self.wrapper.device.settings[mode][int(block.channel)]["name"]
else:
entity_name = None
if not entity_name:
if self.wrapper.model == "SHEM-3":
base = ord("A")
else:
base = ord("1")
entity_name = f"{self.wrapper.name} channel {chr(int(block.channel)+base)}"

if entity_type == "switch":
return entity_name

if entity_type == "sensor":
return f"{entity_name} {self.description.name}"

raise ValueError


async def async_setup_entry_attribute_entities(
hass, config_entry, async_add_entities, sensors, sensor_class
):
Expand Down Expand Up @@ -217,7 +169,7 @@ def __init__(

self._unit = unit
self._unique_id = f"{super().unique_id}-{self.attribute}"
self._name = shelly_naming(self, block, "sensor")
self._name = get_entity_name(wrapper, block, self.description.name)

@property
def unique_id(self):
Expand Down Expand Up @@ -285,7 +237,7 @@ def __init__(
self.description = description

self._unit = self.description.unit
self._name = shelly_naming(self, None, "sensor")
self._name = get_entity_name(wrapper, None, self.description.name)
self.path = self.description.path
self._attributes = self.description.attributes

Expand Down
59 changes: 31 additions & 28 deletions homeassistant/components/shelly/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,37 @@ def get_entity_name(
"""Naming for switch and sensors."""
entity_name = wrapper.name

channels = None
if block.type == "input":
channels = wrapper.device.shelly.get("num_inputs")
elif block.type == "emeter":
channels = wrapper.device.shelly.get("num_emeters")
elif block.type in ["relay", "light"]:
channels = wrapper.device.shelly.get("num_outputs")
elif block.type in ["roller", "device"]:
channels = 1

channels = channels or 1

if channels > 1 and block.type != "device":
entity_name = None
mode = block.type + "s"
if mode in wrapper.device.settings:
entity_name = wrapper.device.settings[mode][int(block.channel)].get("name")

if not entity_name:
if wrapper.model == "SHEM-3":
base = ord("A")
else:
base = ord("1")
entity_name = f"{wrapper.name} channel {chr(int(block.channel)+base)}"

# Shelly Dimmer has two input channels and missing "num_inputs"
if wrapper.model in ["SHDM-1", "SHDM-2"] and block.type == "input":
entity_name = f"{entity_name} channel {int(block.channel)+1}"
if block:
channels = None
if block.type == "input":
channels = wrapper.device.shelly.get("num_inputs")
elif block.type == "emeter":
channels = wrapper.device.shelly.get("num_emeters")
elif block.type in ["relay", "light"]:
channels = wrapper.device.shelly.get("num_outputs")
elif block.type in ["roller", "device"]:
channels = 1

channels = channels or 1

if channels > 1 and block.type != "device":
entity_name = None
mode = block.type + "s"
if mode in wrapper.device.settings:
entity_name = wrapper.device.settings[mode][int(block.channel)].get(
"name"
)

if not entity_name:
if wrapper.model == "SHEM-3":
base = ord("A")
else:
base = ord("1")
entity_name = f"{wrapper.name} channel {chr(int(block.channel)+base)}"

# Shelly Dimmer has two input channels and missing "num_inputs"
if wrapper.model in ["SHDM-1", "SHDM-2"] and block.type == "input":
entity_name = f"{entity_name} channel {int(block.channel)+1}"

if description:
entity_name = f"{entity_name} {description}"
Expand Down