Improve entity names for powered by Shelly devices (#154592)

This commit is contained in:
Maciej Bieniek
2025-10-19 00:57:48 +02:00
committed by GitHub
parent ca31a279fa
commit ef69e6d54b
11 changed files with 39 additions and 18 deletions
@@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from .const import CONF_SLEEP_PERIOD, MODEL_FRANKEVER_WATER_VALVE
from .const import CONF_SLEEP_PERIOD, MODEL_FRANKEVER_WATER_VALVE, ROLE_GENERIC
from .coordinator import ShellyConfigEntry, ShellyRpcCoordinator
from .entity import (
BlockEntityDescription,
@@ -272,7 +272,7 @@ RPC_SENSORS: Final = {
removal_condition=lambda config, _status, key: not is_view_for_platform(
config, key, BINARY_SENSOR_PLATFORM
),
role="generic",
role=ROLE_GENERIC,
),
"boolean_has_power": RpcBinarySensorDescription(
key="boolean",
+8 -2
View File
@@ -23,7 +23,13 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, LOGGER, MODEL_FRANKEVER_WATER_VALVE, SHELLY_GAS_MODELS
from .const import (
DOMAIN,
LOGGER,
MODEL_FRANKEVER_WATER_VALVE,
ROLE_GENERIC,
SHELLY_GAS_MODELS,
)
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
from .entity import (
RpcEntityDescription,
@@ -351,7 +357,7 @@ class RpcVirtualButton(ShellyRpcAttributeEntity, ButtonEntity):
RPC_BUTTONS = {
"button_generic": RpcButtonDescription(
key="button",
role="generic",
role=ROLE_GENERIC,
),
"button_open": RpcButtonDescription(
key="button",
+2
View File
@@ -328,3 +328,5 @@ MODEL_LINKEDGO_ST802_THERMOSTAT = "ST-802"
MODEL_LINKEDGO_ST1820_THERMOSTAT = "ST1820"
MODEL_TOP_EV_CHARGER_EVE01 = "EVE01"
MODEL_FRANKEVER_IRRIGATION_CONTROLLER = "Irrigation"
ROLE_GENERIC = "generic"
+3 -1
View File
@@ -560,7 +560,9 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, Entity):
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}-{attribute}"
self._attr_name = get_rpc_entity_name(coordinator.device, key, description.name)
self._attr_name = get_rpc_entity_name(
coordinator.device, key, description.name, description.role
)
self._last_value = None
id_key = key.split(":")[-1]
self._id = int(id_key) if id_key.isnumeric() else None
+2 -1
View File
@@ -32,6 +32,7 @@ from .const import (
MODEL_LINKEDGO_ST802_THERMOSTAT,
MODEL_LINKEDGO_ST1820_THERMOSTAT,
MODEL_TOP_EV_CHARGER_EVE01,
ROLE_GENERIC,
VIRTUAL_NUMBER_MODE_MAP,
)
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
@@ -223,7 +224,7 @@ RPC_NUMBERS: Final = {
step_fn=lambda config: config["meta"]["ui"].get("step"),
unit=get_virtual_component_unit,
method="number_set",
role="generic",
role=ROLE_GENERIC,
),
"number_current_limit": RpcNumberDescription(
key="number",
+2 -1
View File
@@ -15,6 +15,7 @@ from homeassistant.components.select import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import ROLE_GENERIC
from .coordinator import ShellyConfigEntry, ShellyRpcCoordinator
from .entity import (
RpcEntityDescription,
@@ -44,7 +45,7 @@ RPC_SELECT_ENTITIES: Final = {
removal_condition=lambda config, _status, key: not is_view_for_platform(
config, key, SELECT_PLATFORM
),
role="generic",
role=ROLE_GENERIC,
),
}
+7 -4
View File
@@ -41,7 +41,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.entity_registry import RegistryEntry
from homeassistant.helpers.typing import StateType
from .const import CONF_SLEEP_PERIOD
from .const import CONF_SLEEP_PERIOD, ROLE_GENERIC
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
from .entity import (
BlockEntityDescription,
@@ -1425,7 +1425,7 @@ RPC_SENSORS: Final = {
removal_condition=lambda config, _, key: not is_view_for_platform(
config, key, SENSOR_PLATFORM
),
role="generic",
role=ROLE_GENERIC,
),
"number_generic": RpcSensorDescription(
key="number",
@@ -1434,7 +1434,7 @@ RPC_SENSORS: Final = {
config, key, SENSOR_PLATFORM
),
unit=get_virtual_component_unit,
role="generic",
role=ROLE_GENERIC,
),
"enum_generic": RpcSensorDescription(
key="enum",
@@ -1444,7 +1444,7 @@ RPC_SENSORS: Final = {
),
options_fn=lambda config: config["options"],
device_class=SensorDeviceClass.ENUM,
role="generic",
role=ROLE_GENERIC,
),
"valve_position": RpcSensorDescription(
key="blutrv",
@@ -1489,6 +1489,7 @@ RPC_SENSORS: Final = {
"number_current_humidity": RpcSensorDescription(
key="number",
sub_key="value",
name="Humidity",
native_unit_of_measurement=PERCENTAGE,
suggested_display_precision=1,
device_class=SensorDeviceClass.HUMIDITY,
@@ -1498,6 +1499,7 @@ RPC_SENSORS: Final = {
"number_current_temperature": RpcSensorDescription(
key="number",
sub_key="value",
name="Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
suggested_display_precision=1,
device_class=SensorDeviceClass.TEMPERATURE,
@@ -1507,6 +1509,7 @@ RPC_SENSORS: Final = {
"number_flow_rate": RpcSensorDescription(
key="number",
sub_key="value",
name="Water flow rate",
native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_MINUTE,
device_class=SensorDeviceClass.VOLUME_FLOW_RATE,
state_class=SensorStateClass.MEASUREMENT,
+2 -1
View File
@@ -27,6 +27,7 @@ from .const import (
MODEL_LINKEDGO_ST1820_THERMOSTAT,
MODEL_NEO_WATER_VALVE,
MODEL_TOP_EV_CHARGER_EVE01,
ROLE_GENERIC,
)
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
from .entity import (
@@ -105,7 +106,7 @@ RPC_SWITCHES = {
method_on="boolean_set",
method_off="boolean_set",
method_params_fn=lambda id, value: (id, value),
role="generic",
role=ROLE_GENERIC,
),
"boolean_anti_freeze": RpcSwitchDescription(
key="boolean",
+2 -1
View File
@@ -15,6 +15,7 @@ from homeassistant.components.text import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import ROLE_GENERIC
from .coordinator import ShellyConfigEntry
from .entity import (
RpcEntityDescription,
@@ -44,7 +45,7 @@ RPC_TEXT_ENTITIES: Final = {
removal_condition=lambda config, _status, key: not is_view_for_platform(
config, key, TEXT_PLATFORM
),
role="generic",
role=ROLE_GENERIC,
),
}
+7 -4
View File
@@ -66,6 +66,7 @@ from .const import (
GEN2_RELEASE_URL,
LOGGER,
MAX_SCRIPT_SIZE,
ROLE_GENERIC,
RPC_INPUTS_EVENTS_TYPES,
SHAIR_MAX_WORK_HOURS,
SHBTN_INPUTS_EVENTS_TYPES,
@@ -436,13 +437,15 @@ def get_rpc_sub_device_name(
def get_rpc_entity_name(
device: RpcDevice, key: str, description: str | None = None
device: RpcDevice, key: str, name: str | None = None, role: str | None = None
) -> str | None:
"""Naming for RPC based switch and sensors."""
channel_name = get_rpc_channel_name(device, key)
if description:
return f"{channel_name} {description.lower()}" if channel_name else description
if name:
if role and role != ROLE_GENERIC:
return name
return f"{channel_name} {name.lower()}" if channel_name else name
return channel_name
@@ -483,7 +486,7 @@ def get_rpc_key_by_role(keys_dict: dict[str, Any], role: str) -> str | None:
def get_rpc_role_by_key(keys_dict: dict[str, Any], key: str) -> str:
"""Return role by key for RPC device from a dict."""
return cast(str, keys_dict[key].get("role", "generic"))
return cast(str, keys_dict[key].get("role", ROLE_GENERIC))
def id_from_key(key: str) -> int:
+2 -1
View File
@@ -1575,7 +1575,7 @@ async def test_rpc_device_virtual_number_sensor_with_device_class(
entity_registry: EntityRegistry,
) -> None:
"""Test a virtual number sensor with device class for RPC device."""
entity_id = f"{SENSOR_DOMAIN}.test_name_current_humidity"
entity_id = f"{SENSOR_DOMAIN}.test_name_humidity"
config = deepcopy(mock_rpc_device.config)
config["number:203"] = {
"name": "Current humidity",
@@ -1587,6 +1587,7 @@ async def test_rpc_device_virtual_number_sensor_with_device_class(
monkeypatch.setattr(mock_rpc_device, "config", config)
status = deepcopy(mock_rpc_device.status)
status.pop("humidity:0")
status["number:203"] = {"value": 34}
monkeypatch.setattr(mock_rpc_device, "status", status)