Add last seen and status code diagnostic sensors to litterrobot (#71760)

This commit is contained in:
Nathan Spencer 2022-05-13 17:03:25 -06:00 committed by GitHub
parent 3e386064cf
commit 72a65b6a21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 4 deletions

View file

@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
@ -63,7 +64,9 @@ class LitterRobotSensorEntity(LitterRobotEntity, SensorEntity):
def native_value(self) -> StateType | datetime:
"""Return the state."""
if self.entity_description.should_report(self.robot):
return getattr(self.robot, self.entity_description.key)
if isinstance(val := getattr(self.robot, self.entity_description.key), str):
return val.lower()
return val
return None
@property
@ -93,6 +96,18 @@ ROBOT_SENSORS = [
device_class=SensorDeviceClass.TIMESTAMP,
should_report=lambda robot: robot.sleep_mode_enabled,
),
LitterRobotSensorEntityDescription(
name="Last Seen",
key="last_seen",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
),
LitterRobotSensorEntityDescription(
name="Status Code",
key="status_code",
device_class="litterrobot__status_code",
entity_category=EntityCategory.DIAGNOSTIC,
),
]