Skip to content

Commit

Permalink
Fix custom power sensor with on/off values #1235
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 17, 2024
1 parent b238cee commit e0f8d0a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
8 changes: 5 additions & 3 deletions custom_components/sonoff/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ def __init__(self, ewelink: XRegistry, device: dict):
if self.param and self.uid is None:
self.uid = self.param

default_class = (
self.uid[:-2] if self.uid.endswith(("_1", "_2", "_3", "_4")) else self.uid
)
# remove tailing _1 _2 _3 _4
default_class = self.uid.rstrip("_01234")

if device["params"].get(self.param) in ("on", "off"):
default_class = None

if device_class := DEVICE_CLASSES.get(default_class):
self._attr_device_class = device_class
Expand Down
90 changes: 90 additions & 0 deletions tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,3 +1698,93 @@ def test_powr3():
assert energy.state_class == SensorStateClass.TOTAL
energy.set_state({"dayKwh": 7})
assert energy.native_value == 0.07


def test_issue1235():
params = {
"mideaConfig": {"applianceCode": "2111XXXXXXX", "type": "0xDB"},
"appointment": "off",
"softener_lack": "off",
"flocks_wash_count": 0,
"flocks_remind_period": 0,
"lock": "off",
"door_opened": "off",
"softener_density_global": 255,
"error_code": 0,
"add_rinse": "0",
"easy_ironing": "off",
"water_level": "auto",
"eye_wash": "0",
"cloud_cycle_jiepai_time2": 255,
"cloud_cycle_low": 0,
"mode": "normal",
"detergent_density": 1,
"progress": 0,
"cloud_cycle_high": 0,
"dehydration_time_value": 0,
"appointment_time": 0,
"soak": "off",
"softener": "0",
"old_speedy": "off",
"cloud_cycle_jiepai_time3": 255,
"power": "off",
"temperature": "40",
"memory": "off",
"wash_time": 0,
"wash_time_value": 0,
"fast_clean_wash": "off",
"ai_flag": "off",
"detergent": "4",
"softener_global": 255,
"wind_dispel": "0",
"expert_step": 0,
"microbubble": "0",
"down_light": "off",
"data_type": "0202",
"detergent_density_global": 255,
"remain_time": 58,
"dehydration_speed": "800",
"device_software_version": 0,
"dehydration_time": 0,
"project_no": 0,
"flocks_switcher": "off",
"active_oxygen": "0",
"intelligent_wash": "off",
"softener_density": 1,
"program": "mixed_wash",
"steam_wash": "off",
"detergent_global": 255,
"season": 1,
"disinfectant": "0",
"speedy": "off",
"cycle_memory": "on",
"running_status": "idle",
"spray_wash": "off",
"stains": "0",
"cloud_cycle_jiepai_time1": 255,
"cloud_cycle_jiepai1": 255,
"soak_count": "2",
"dryer": "0",
"cloud_cycle_jiepai_time4": 255,
"version": 54,
"dirty_degree": "0",
"cloud_cycle_jiepai2": 255,
"strong_wash": "off",
"cloud_cycle_jiepai4": 255,
"customize_machine_cycle": 255,
"beforehand_wash": "off",
"cloud_cycle_jiepai3": 255,
"ultraviolet_lamp": "1",
"detergent_lack": "off",
"nightly": "off",
"super_clean_wash": "off",
"switch": "on",
}
devices.get_spec = devices.get_spec_wrapper(devices.get_spec, ["power"])
entities = get_entitites({"extra": {"uiid": 176}, "params": params})

power: XSensor = next(e for e in entities if e.uid == "power")
assert power.device_class is None
assert power.native_unit_of_measurement is None
assert power.native_value is "off"
assert power.state_class is None

0 comments on commit e0f8d0a

Please sign in to comment.