Add power entity for yolink plug (#124678)

* Add Plug power entity

* change state class to total

* Add translations and moving icon to icon.json

* sort translation key

* Fix suggestions
This commit is contained in:
Matrix 2024-08-27 18:03:50 +08:00 committed by GitHub
parent 6b0428774d
commit 37e2839fa3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 1 deletions

View file

@ -23,3 +23,7 @@ DEV_MODEL_TH_SENSOR_YS8014_UC = "YS8014-UC"
DEV_MODEL_TH_SENSOR_YS8014_EC = "YS8014-EC"
DEV_MODEL_TH_SENSOR_YS8017_UC = "YS8017-UC"
DEV_MODEL_TH_SENSOR_YS8017_EC = "YS8017-EC"
DEV_MODEL_PLUG_YS6602_UC = "YS6602-UC"
DEV_MODEL_PLUG_YS6602_EC = "YS6602-EC"
DEV_MODEL_PLUG_YS6803_UC = "YS6803-UC"
DEV_MODEL_PLUG_YS6803_EC = "YS6803-EC"

View file

@ -17,6 +17,9 @@
},
"power_failure_alarm_beep": {
"default": "mdi:bullhorn"
},
"water_meter_reading": {
"default": "mdi:gauge"
}
},
"switch": {

View file

@ -40,7 +40,9 @@ from homeassistant.const import (
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
EntityCategory,
UnitOfEnergy,
UnitOfLength,
UnitOfPower,
UnitOfTemperature,
UnitOfVolume,
)
@ -49,6 +51,10 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import percentage
from .const import (
DEV_MODEL_PLUG_YS6602_EC,
DEV_MODEL_PLUG_YS6602_UC,
DEV_MODEL_PLUG_YS6803_EC,
DEV_MODEL_PLUG_YS6803_UC,
DEV_MODEL_TH_SENSOR_YS8004_EC,
DEV_MODEL_TH_SENSOR_YS8004_UC,
DEV_MODEL_TH_SENSOR_YS8014_EC,
@ -125,6 +131,13 @@ NONE_HUMIDITY_SENSOR_MODELS = [
DEV_MODEL_TH_SENSOR_YS8017_EC,
]
POWER_SUPPORT_MODELS = [
DEV_MODEL_PLUG_YS6602_UC,
DEV_MODEL_PLUG_YS6602_EC,
DEV_MODEL_PLUG_YS6803_UC,
DEV_MODEL_PLUG_YS6803_EC,
]
def cvt_battery(val: int | None) -> int | None:
"""Convert battery to percentage."""
@ -228,13 +241,32 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
key="meter_reading",
translation_key="water_meter_reading",
device_class=SensorDeviceClass.WATER,
icon="mdi:gauge",
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
state_class=SensorStateClass.TOTAL_INCREASING,
should_update_entity=lambda value: value is not None,
exists_fn=lambda device: device.device_type
in ATTR_DEVICE_WATER_METER_CONTROLLER,
),
YoLinkSensorEntityDescription(
key="power",
translation_key="current_power",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
should_update_entity=lambda value: value is not None,
exists_fn=lambda device: device.device_model_name in POWER_SUPPORT_MODELS,
value=lambda value: value / 10 if value is not None else None,
),
YoLinkSensorEntityDescription(
key="watt",
translation_key="power_consumption",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL,
should_update_entity=lambda value: value is not None,
exists_fn=lambda device: device.device_model_name in POWER_SUPPORT_MODELS,
value=lambda value: value / 100 if value is not None else None,
),
)

View file

@ -51,6 +51,12 @@
"plug_4": { "name": "Plug 4" }
},
"sensor": {
"current_power": {
"name": "Current power"
},
"power_consumption": {
"name": "Power consumption"
},
"power_failure_alarm": {
"name": "Power failure alarm",
"state": {