From 602e36aa12c2218840728cea21b9db0f286d98da Mon Sep 17 00:00:00 2001 From: Luke Lashley Date: Sat, 9 Sep 2023 18:40:28 -0400 Subject: [PATCH] Add new sensors to Roborock (#99983) * Add 3 new sensor types * add state options for dock error * add unit of measurement --- homeassistant/components/roborock/sensor.py | 36 ++++++++++++++++++- .../components/roborock/strings.json | 17 +++++++++ tests/components/roborock/test_sensor.py | 3 +- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/roborock/sensor.py b/homeassistant/components/roborock/sensor.py index 0629839f01bba..8d58ae96c453d 100644 --- a/homeassistant/components/roborock/sensor.py +++ b/homeassistant/components/roborock/sensor.py @@ -4,7 +4,12 @@ from collections.abc import Callable from dataclasses import dataclass -from roborock.containers import RoborockErrorCode, RoborockStateCode +from roborock.containers import ( + RoborockDockErrorCode, + RoborockDockTypeCode, + RoborockErrorCode, + RoborockStateCode, +) from roborock.roborock_typing import DeviceProp from homeassistant.components.sensor import ( @@ -134,6 +139,35 @@ class RoborockSensorDescription( native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.BATTERY, ), + # Only available on some newer models + RoborockSensorDescription( + key="clean_percent", + icon="mdi:progress-check", + translation_key="clean_percent", + value_fn=lambda data: data.status.clean_percent, + entity_category=EntityCategory.DIAGNOSTIC, + native_unit_of_measurement=PERCENTAGE, + ), + # Only available with more than just the basic dock + RoborockSensorDescription( + key="dock_error", + icon="mdi:garage-open", + translation_key="dock_error", + value_fn=lambda data: data.status.dock_error_status.name + if data.status.dock_type != RoborockDockTypeCode.no_dock + else None, + entity_category=EntityCategory.DIAGNOSTIC, + device_class=SensorDeviceClass.ENUM, + options=RoborockDockErrorCode.keys(), + ), + RoborockSensorDescription( + key="mop_clean_remaining", + native_unit_of_measurement=UnitOfTime.SECONDS, + device_class=SensorDeviceClass.DURATION, + value_fn=lambda data: data.status.rdt, + translation_key="mop_drying_remaining_time", + entity_category=EntityCategory.DIAGNOSTIC, + ), ] diff --git a/homeassistant/components/roborock/strings.json b/homeassistant/components/roborock/strings.json index 269bbf04cf20a..0170c8ac70618 100644 --- a/homeassistant/components/roborock/strings.json +++ b/homeassistant/components/roborock/strings.json @@ -50,9 +50,26 @@ "cleaning_time": { "name": "Cleaning time" }, + "clean_percent": { + "name": "Cleaning progress" + }, + "dock_error": { + "name": "Dock error", + "state": { + "ok": "Ok", + "duct_blockage": "Duct blockage", + "water_empty": "Water empty", + "waste_water_tank_full": "Waste water tank full", + "dirty_tank_latch_open": "Dirty tank latch open", + "no_dustbin": "No dustbin" + } + }, "main_brush_time_left": { "name": "Main brush time left" }, + "mop_drying_remaining_time": { + "name": "Mop drying remaining time" + }, "side_brush_time_left": { "name": "Side brush time left" }, diff --git a/tests/components/roborock/test_sensor.py b/tests/components/roborock/test_sensor.py index 19648343bb48d..a022f0dfa5140 100644 --- a/tests/components/roborock/test_sensor.py +++ b/tests/components/roborock/test_sensor.py @@ -14,7 +14,7 @@ async def test_sensors(hass: HomeAssistant, setup_entry: MockConfigEntry) -> None: """Test sensors and check test values are correctly set.""" - assert len(hass.states.async_all("sensor")) == 11 + assert len(hass.states.async_all("sensor")) == 12 assert hass.states.get("sensor.roborock_s7_maxv_main_brush_time_left").state == str( MAIN_BRUSH_REPLACE_TIME - 74382 ) @@ -38,3 +38,4 @@ async def test_sensors(hass: HomeAssistant, setup_entry: MockConfigEntry) -> Non assert hass.states.get("sensor.roborock_s7_maxv_cleaning_area").state == "21.0" assert hass.states.get("sensor.roborock_s7_maxv_vacuum_error").state == "none" assert hass.states.get("sensor.roborock_s7_maxv_battery").state == "100" + assert hass.states.get("sensor.roborock_s7_maxv_dock_error").state == "ok"