Adjust litterrobot tests and code to match guidelines (#47060)
* Use SwitchEntity instead of ToggleEntity and adjust test patches as recommended * Move async_create_entry out of try block in config_flow * Patch pypi package instead of HA code * Bump pylitterbot to 2021.2.6, fix tests, and implement other code review suggestions * Bump pylitterbot to 2021.2.8, remove sleep mode start/end time from vacuum, adjust and add sensors for sleep mode start/end time * Move icon helper back to Litter-Robot component and isoformat times on time sensors
This commit is contained in:
parent
14f85d8731
commit
e9052233a6
14 changed files with 277 additions and 142 deletions
|
@ -1,32 +1,44 @@
|
|||
"""Support for Litter-Robot sensors."""
|
||||
from homeassistant.const import PERCENTAGE
|
||||
from typing import Optional
|
||||
|
||||
from pylitterbot.robot import Robot
|
||||
|
||||
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, PERCENTAGE
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .hub import LitterRobotEntity
|
||||
|
||||
WASTE_DRAWER = "Waste Drawer"
|
||||
from .hub import LitterRobotEntity, LitterRobotHub
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up Litter-Robot sensors using config entry."""
|
||||
hub = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
entities = []
|
||||
for robot in hub.account.robots:
|
||||
entities.append(LitterRobotSensor(robot, WASTE_DRAWER, hub))
|
||||
|
||||
if entities:
|
||||
async_add_entities(entities, True)
|
||||
def icon_for_gauge_level(gauge_level: Optional[int] = None, offset: int = 0) -> str:
|
||||
"""Return a gauge icon valid identifier."""
|
||||
if gauge_level is None or gauge_level <= 0 + offset:
|
||||
return "mdi:gauge-empty"
|
||||
if gauge_level > 70 + offset:
|
||||
return "mdi:gauge-full"
|
||||
if gauge_level > 30 + offset:
|
||||
return "mdi:gauge"
|
||||
return "mdi:gauge-low"
|
||||
|
||||
|
||||
class LitterRobotSensor(LitterRobotEntity, Entity):
|
||||
"""Litter-Robot sensors."""
|
||||
class LitterRobotPropertySensor(LitterRobotEntity, Entity):
|
||||
"""Litter-Robot property sensors."""
|
||||
|
||||
def __init__(
|
||||
self, robot: Robot, entity_type: str, hub: LitterRobotHub, sensor_attribute: str
|
||||
):
|
||||
"""Pass coordinator to CoordinatorEntity."""
|
||||
super().__init__(robot, entity_type, hub)
|
||||
self.sensor_attribute = sensor_attribute
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state."""
|
||||
return self.robot.waste_drawer_gauge
|
||||
return getattr(self.robot, self.sensor_attribute)
|
||||
|
||||
|
||||
class LitterRobotWasteSensor(LitterRobotPropertySensor, Entity):
|
||||
"""Litter-Robot sensors."""
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
|
@ -36,19 +48,40 @@ class LitterRobotSensor(LitterRobotEntity, Entity):
|
|||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend, if any."""
|
||||
if self.robot.waste_drawer_gauge <= 10:
|
||||
return "mdi:gauge-empty"
|
||||
if self.robot.waste_drawer_gauge < 50:
|
||||
return "mdi:gauge-low"
|
||||
if self.robot.waste_drawer_gauge <= 90:
|
||||
return "mdi:gauge"
|
||||
return "mdi:gauge-full"
|
||||
return icon_for_gauge_level(self.state, 10)
|
||||
|
||||
|
||||
class LitterRobotSleepTimeSensor(LitterRobotPropertySensor, Entity):
|
||||
"""Litter-Robot sleep time sensors."""
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return device specific state attributes."""
|
||||
return {
|
||||
"cycle_count": self.robot.cycle_count,
|
||||
"cycle_capacity": self.robot.cycle_capacity,
|
||||
"cycles_after_drawer_full": self.robot.cycles_after_drawer_full,
|
||||
}
|
||||
def state(self):
|
||||
"""Return the state."""
|
||||
if self.robot.sleep_mode_active:
|
||||
return super().state.isoformat()
|
||||
return None
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class, if any."""
|
||||
return DEVICE_CLASS_TIMESTAMP
|
||||
|
||||
|
||||
ROBOT_SENSORS = [
|
||||
(LitterRobotWasteSensor, "Waste Drawer", "waste_drawer_gauge"),
|
||||
(LitterRobotSleepTimeSensor, "Sleep Mode Start Time", "sleep_mode_start_time"),
|
||||
(LitterRobotSleepTimeSensor, "Sleep Mode End Time", "sleep_mode_end_time"),
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up Litter-Robot sensors using config entry."""
|
||||
hub = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
entities = []
|
||||
for robot in hub.account.robots:
|
||||
for (sensor_class, entity_type, sensor_attribute) in ROBOT_SENSORS:
|
||||
entities.append(sensor_class(robot, entity_type, hub, sensor_attribute))
|
||||
|
||||
if entities:
|
||||
async_add_entities(entities, True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue