Add charging binary_sensor so front end can render battery icon properly (#139684)

* Add charging binary sensor

* Add charging binary sensor test
This commit is contained in:
StaleLoafOfBread 2025-03-03 14:21:05 -05:00 committed by GitHub
parent e28e4d210f
commit 890c672f8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -5,6 +5,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from roborock.containers import RoborockStateCode
from roborock.roborock_typing import DeviceProp from roborock.roborock_typing import DeviceProp
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -12,7 +13,7 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.const import EntityCategory from homeassistant.const import ATTR_BATTERY_CHARGING, EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@ -63,6 +64,13 @@ BINARY_SENSOR_DESCRIPTIONS = [
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.status.in_cleaning, value_fn=lambda data: data.status.in_cleaning,
), ),
RoborockBinarySensorDescription(
key=ATTR_BATTERY_CHARGING,
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.status.state
in (RoborockStateCode.charging, RoborockStateCode.charging_complete),
),
] ]

View File

@ -18,7 +18,7 @@ async def test_binary_sensors(
hass: HomeAssistant, setup_entry: MockConfigEntry hass: HomeAssistant, setup_entry: MockConfigEntry
) -> None: ) -> None:
"""Test binary sensors and check test values are correctly set.""" """Test binary sensors and check test values are correctly set."""
assert len(hass.states.async_all("binary_sensor")) == 8 assert len(hass.states.async_all("binary_sensor")) == 10
assert hass.states.get("binary_sensor.roborock_s7_maxv_mop_attached").state == "on" assert hass.states.get("binary_sensor.roborock_s7_maxv_mop_attached").state == "on"
assert ( assert (
hass.states.get("binary_sensor.roborock_s7_maxv_water_box_attached").state hass.states.get("binary_sensor.roborock_s7_maxv_water_box_attached").state
@ -28,3 +28,4 @@ async def test_binary_sensors(
hass.states.get("binary_sensor.roborock_s7_maxv_water_shortage").state == "off" hass.states.get("binary_sensor.roborock_s7_maxv_water_shortage").state == "off"
) )
assert hass.states.get("binary_sensor.roborock_s7_maxv_cleaning").state == "off" assert hass.states.get("binary_sensor.roborock_s7_maxv_cleaning").state == "off"
assert hass.states.get("binary_sensor.roborock_s7_maxv_charging").state == "on"