Add entity category to Shelly (#57705)

This commit is contained in:
Maciej Bieniek 2021-10-15 00:17:00 +02:00 committed by GitHub
parent 4745e58a92
commit 3127074f76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -41,16 +42,24 @@ from .utils import (
SENSORS: Final = { SENSORS: Final = {
("device", "overtemp"): BlockAttributeDescription( ("device", "overtemp"): BlockAttributeDescription(
name="Overheating", device_class=DEVICE_CLASS_PROBLEM name="Overheating",
device_class=DEVICE_CLASS_PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("device", "overpower"): BlockAttributeDescription( ("device", "overpower"): BlockAttributeDescription(
name="Overpowering", device_class=DEVICE_CLASS_PROBLEM name="Overpowering",
device_class=DEVICE_CLASS_PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("light", "overpower"): BlockAttributeDescription( ("light", "overpower"): BlockAttributeDescription(
name="Overpowering", device_class=DEVICE_CLASS_PROBLEM name="Overpowering",
device_class=DEVICE_CLASS_PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("relay", "overpower"): BlockAttributeDescription( ("relay", "overpower"): BlockAttributeDescription(
name="Overpowering", device_class=DEVICE_CLASS_PROBLEM name="Overpowering",
device_class=DEVICE_CLASS_PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("sensor", "dwIsOpened"): BlockAttributeDescription( ("sensor", "dwIsOpened"): BlockAttributeDescription(
name="Door", name="Door",
@ -106,6 +115,7 @@ REST_SENSORS: Final = {
value=lambda status, _: status["cloud"]["connected"], value=lambda status, _: status["cloud"]["connected"],
device_class=DEVICE_CLASS_CONNECTIVITY, device_class=DEVICE_CLASS_CONNECTIVITY,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
"fwupdate": RestAttributeDescription( "fwupdate": RestAttributeDescription(
name="Firmware Update", name="Firmware Update",
@ -116,6 +126,7 @@ REST_SENSORS: Final = {
"latest_stable_version": status["update"]["new_version"], "latest_stable_version": status["update"]["new_version"],
"installed_version": status["update"]["old_version"], "installed_version": status["update"]["old_version"],
}, },
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
} }
@ -134,6 +145,7 @@ RPC_SENSORS: Final = {
name="Cloud", name="Cloud",
device_class=DEVICE_CLASS_CONNECTIVITY, device_class=DEVICE_CLASS_CONNECTIVITY,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
"fwupdate": RpcAttributeDescription( "fwupdate": RpcAttributeDescription(
key="sys", key="sys",
@ -145,6 +157,7 @@ RPC_SENSORS: Final = {
"latest_stable_version": status.get("stable", {"version": ""})["version"], "latest_stable_version": status.get("stable", {"version": ""})["version"],
"beta_version": status.get("beta", {"version": ""})["version"], "beta_version": status.get("beta", {"version": ""})["version"],
}, },
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
} }

View File

@ -237,6 +237,7 @@ class BlockAttributeDescription:
# Callable (settings, block), return true if entity should be removed # Callable (settings, block), return true if entity should be removed
removal_condition: Callable[[dict, Block], bool] | None = None removal_condition: Callable[[dict, Block], bool] | None = None
extra_state_attributes: Callable[[Block], dict | None] | None = None extra_state_attributes: Callable[[Block], dict | None] | None = None
entity_category: str | None = None
@dataclass @dataclass
@ -255,6 +256,7 @@ class RpcAttributeDescription:
available: Callable[[dict], bool] | None = None available: Callable[[dict], bool] | None = None
removal_condition: Callable[[dict, str], bool] | None = None removal_condition: Callable[[dict, str], bool] | None = None
extra_state_attributes: Callable[[dict], dict | None] | None = None extra_state_attributes: Callable[[dict], dict | None] | None = None
entity_category: str | None = None
@dataclass @dataclass
@ -269,6 +271,7 @@ class RestAttributeDescription:
state_class: str | None = None state_class: str | None = None
default_enabled: bool = True default_enabled: bool = True
extra_state_attributes: Callable[[dict], dict | None] | None = None extra_state_attributes: Callable[[dict], dict | None] | None = None
entity_category: str | None = None
class ShellyBlockEntity(entity.Entity): class ShellyBlockEntity(entity.Entity):
@ -469,6 +472,11 @@ class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity):
return self.description.extra_state_attributes(self.block) return self.description.extra_state_attributes(self.block)
@property
def entity_category(self) -> str | None:
"""Return category of entity."""
return self.description.entity_category
class ShellyRestAttributeEntity(update_coordinator.CoordinatorEntity): class ShellyRestAttributeEntity(update_coordinator.CoordinatorEntity):
"""Class to load info from REST.""" """Class to load info from REST."""
@ -541,6 +549,11 @@ class ShellyRestAttributeEntity(update_coordinator.CoordinatorEntity):
return self.description.extra_state_attributes(self.wrapper.device.status) return self.description.extra_state_attributes(self.wrapper.device.status)
@property
def entity_category(self) -> str | None:
"""Return category of entity."""
return self.description.entity_category
class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity): class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity):
"""Helper class to represent a rpc attribute.""" """Helper class to represent a rpc attribute."""
@ -599,6 +612,11 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity):
self.wrapper.device.status[self.key][self.sub_key] self.wrapper.device.status[self.key][self.sub_key]
) )
@property
def entity_category(self) -> str | None:
"""Return category of entity."""
return self.description.entity_category
class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEntity): class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEntity):
"""Represent a shelly sleeping block attribute entity.""" """Represent a shelly sleeping block attribute entity."""

View File

@ -12,6 +12,7 @@ from homeassistant.const import (
ELECTRIC_CURRENT_AMPERE, ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
ENTITY_CATEGORY_DIAGNOSTIC,
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE, PERCENTAGE,
POWER_WATT, POWER_WATT,
@ -45,6 +46,7 @@ SENSORS: Final = {
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=sensor.STATE_CLASS_MEASUREMENT,
removal_condition=lambda settings, _: settings.get("external_power") == 1, removal_condition=lambda settings, _: settings.get("external_power") == 1,
available=lambda block: cast(int, block.battery) != -1, available=lambda block: cast(int, block.battery) != -1,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("device", "deviceTemp"): BlockAttributeDescription( ("device", "deviceTemp"): BlockAttributeDescription(
name="Device Temperature", name="Device Temperature",
@ -53,6 +55,7 @@ SENSORS: Final = {
device_class=sensor.DEVICE_CLASS_TEMPERATURE, device_class=sensor.DEVICE_CLASS_TEMPERATURE,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("emeter", "current"): BlockAttributeDescription( ("emeter", "current"): BlockAttributeDescription(
name="Current", name="Current",
@ -205,6 +208,7 @@ SENSORS: Final = {
extra_state_attributes=lambda block: { extra_state_attributes=lambda block: {
"Operational hours": round(cast(int, block.totalWorkTime) / 3600, 1) "Operational hours": round(cast(int, block.totalWorkTime) / 3600, 1)
}, },
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
("adc", "adc"): BlockAttributeDescription( ("adc", "adc"): BlockAttributeDescription(
name="ADC", name="ADC",
@ -229,12 +233,14 @@ REST_SENSORS: Final = {
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH, device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
"uptime": RestAttributeDescription( "uptime": RestAttributeDescription(
name="Uptime", name="Uptime",
value=lambda status, last: get_device_uptime(status["uptime"], last), value=lambda status, last: get_device_uptime(status["uptime"], last),
device_class=sensor.DEVICE_CLASS_TIMESTAMP, device_class=sensor.DEVICE_CLASS_TIMESTAMP,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
} }
@ -286,6 +292,7 @@ RPC_SENSORS: Final = {
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH, device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=sensor.STATE_CLASS_MEASUREMENT, state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
"uptime": RpcAttributeDescription( "uptime": RpcAttributeDescription(
key="sys", key="sys",
@ -294,6 +301,7 @@ RPC_SENSORS: Final = {
value=get_device_uptime, value=get_device_uptime,
device_class=sensor.DEVICE_CLASS_TIMESTAMP, device_class=sensor.DEVICE_CLASS_TIMESTAMP,
default_enabled=False, default_enabled=False,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
} }