Use shorthand attributes in Neato (#99605)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Joost Lekkerkerker 2023-09-05 16:33:46 +02:00 committed by GitHub
parent 5afba6327c
commit 2c45d43e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 75 deletions

View File

@ -57,6 +57,7 @@ class NeatoCleaningMap(NeatoEntity, Camera):
self._mapdata = mapdata
self._available = neato is not None
self._robot_serial: str = self.robot.serial
self._attr_unique_id = self.robot.serial
self._generated_at: str | None = None
self._image_url: str | None = None
self._image: bytes | None = None
@ -109,11 +110,6 @@ class NeatoCleaningMap(NeatoEntity, Camera):
self._generated_at = map_data.get("generated_at")
self._available = True
@property
def unique_id(self) -> str:
"""Return unique ID."""
return self._robot_serial
@property
def available(self) -> bool:
"""Return if the robot is available."""

View File

@ -17,11 +17,7 @@ class NeatoEntity(Entity):
def __init__(self, robot: Robot) -> None:
"""Initialize Neato entity."""
self.robot = robot
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
self._attr_device_info: DeviceInfo = DeviceInfo(
identifiers={(NEATO_DOMAIN, self.robot.serial)},
name=self.robot.name,
)

View File

@ -44,11 +44,16 @@ async def async_setup_entry(
class NeatoSensor(NeatoEntity, SensorEntity):
"""Neato sensor."""
_attr_device_class = SensorDeviceClass.BATTERY
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_native_unit_of_measurement = PERCENTAGE
_attr_available: bool = False
def __init__(self, neato: NeatoHub, robot: Robot) -> None:
"""Initialize Neato sensor."""
super().__init__(robot)
self._available: bool = False
self._robot_serial: str = self.robot.serial
self._attr_unique_id = self.robot.serial
self._state: dict[str, Any] | None = None
def update(self) -> None:
@ -56,45 +61,20 @@ class NeatoSensor(NeatoEntity, SensorEntity):
try:
self._state = self.robot.state
except NeatoRobotException as ex:
if self._available:
if self._attr_available:
_LOGGER.error(
"Neato sensor connection error for '%s': %s", self.entity_id, ex
)
self._state = None
self._available = False
self._attr_available = False
return
self._available = True
self._attr_available = True
_LOGGER.debug("self._state=%s", self._state)
@property
def unique_id(self) -> str:
"""Return unique ID."""
return self._robot_serial
@property
def device_class(self) -> SensorDeviceClass:
"""Return the device class."""
return SensorDeviceClass.BATTERY
@property
def entity_category(self) -> EntityCategory:
"""Device entity category."""
return EntityCategory.DIAGNOSTIC
@property
def available(self) -> bool:
"""Return availability."""
return self._available
@property
def native_value(self) -> str | None:
"""Return the state."""
if self._state is not None:
return str(self._state["details"]["charge"])
return None
@property
def native_unit_of_measurement(self) -> str:
"""Return unit of measurement."""
return PERCENTAGE

View File

@ -49,16 +49,17 @@ class NeatoConnectedSwitch(NeatoEntity, SwitchEntity):
"""Neato Connected Switches."""
_attr_translation_key = "schedule"
_attr_available = False
_attr_entity_category = EntityCategory.CONFIG
def __init__(self, neato: NeatoHub, robot: Robot, switch_type: str) -> None:
"""Initialize the Neato Connected switches."""
super().__init__(robot)
self.type = switch_type
self._available = False
self._state: dict[str, Any] | None = None
self._schedule_state: str | None = None
self._clean_state = None
self._robot_serial: str = self.robot.serial
self._attr_unique_id = self.robot.serial
def update(self) -> None:
"""Update the states of Neato switches."""
@ -66,15 +67,15 @@ class NeatoConnectedSwitch(NeatoEntity, SwitchEntity):
try:
self._state = self.robot.state
except NeatoRobotException as ex:
if self._available: # Print only once when available
if self._attr_available: # Print only once when available
_LOGGER.error(
"Neato switch connection error for '%s': %s", self.entity_id, ex
)
self._state = None
self._available = False
self._attr_available = False
return
self._available = True
self._attr_available = True
_LOGGER.debug("self._state=%s", self._state)
if self.type == SWITCH_TYPE_SCHEDULE:
_LOGGER.debug("State: %s", self._state)
@ -86,16 +87,6 @@ class NeatoConnectedSwitch(NeatoEntity, SwitchEntity):
"Schedule state for '%s': %s", self.entity_id, self._schedule_state
)
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._robot_serial
@property
def is_on(self) -> bool:
"""Return true if switch is on."""
@ -103,11 +94,6 @@ class NeatoConnectedSwitch(NeatoEntity, SwitchEntity):
self.type == SWITCH_TYPE_SCHEDULE and self._schedule_state == STATE_ON
)
@property
def entity_category(self) -> EntityCategory:
"""Device entity category."""
return EntityCategory.CONFIG
def turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
if self.type == SWITCH_TYPE_SCHEDULE:

View File

@ -124,7 +124,6 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
self._robot_serial: str = self.robot.serial
self._attr_unique_id: str = self.robot.serial
self._status_state: str | None = None
self._clean_state: str | None = None
self._state: dict[str, Any] | None = None
self._clean_time_start: str | None = None
self._clean_time_stop: str | None = None
@ -169,23 +168,23 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
robot_alert = None
if self._state["state"] == 1:
if self._state["details"]["isCharging"]:
self._clean_state = STATE_DOCKED
self._attr_state = STATE_DOCKED
self._status_state = "Charging"
elif (
self._state["details"]["isDocked"]
and not self._state["details"]["isCharging"]
):
self._clean_state = STATE_DOCKED
self._attr_state = STATE_DOCKED
self._status_state = "Docked"
else:
self._clean_state = STATE_IDLE
self._attr_state = STATE_IDLE
self._status_state = "Stopped"
if robot_alert is not None:
self._status_state = robot_alert
elif self._state["state"] == 2:
if robot_alert is None:
self._clean_state = STATE_CLEANING
self._attr_state = STATE_CLEANING
self._status_state = (
f"{MODE.get(self._state['cleaning']['mode'])} "
f"{ACTION.get(self._state['action'])}"
@ -200,10 +199,10 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
else:
self._status_state = robot_alert
elif self._state["state"] == 3:
self._clean_state = STATE_PAUSED
self._attr_state = STATE_PAUSED
self._status_state = "Paused"
elif self._state["state"] == 4:
self._clean_state = STATE_ERROR
self._attr_state = STATE_ERROR
self._status_state = ERRORS.get(self._state["error"])
self._attr_battery_level = self._state["details"]["charge"]
@ -261,11 +260,6 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
self._robot_boundaries,
)
@property
def state(self) -> str | None:
"""Return the status of the vacuum cleaner."""
return self._clean_state
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the vacuum cleaner."""
@ -299,7 +293,7 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
@property
def device_info(self) -> DeviceInfo:
"""Device info for neato robot."""
device_info = super().device_info
device_info = self._attr_device_info
if self._robot_stats:
device_info["manufacturer"] = self._robot_stats["battery"]["vendor"]
device_info["model"] = self._robot_stats["model"]
@ -331,9 +325,9 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
def return_to_base(self, **kwargs: Any) -> None:
"""Set the vacuum cleaner to return to the dock."""
try:
if self._clean_state == STATE_CLEANING:
if self._attr_state == STATE_CLEANING:
self.robot.pause_cleaning()
self._clean_state = STATE_RETURNING
self._attr_state = STATE_RETURNING
self.robot.send_to_base()
except NeatoRobotException as ex:
_LOGGER.error(
@ -383,7 +377,7 @@ class NeatoConnectedVacuum(NeatoEntity, StateVacuumEntity):
return
_LOGGER.info("Start cleaning zone '%s' with robot %s", zone, self.entity_id)
self._clean_state = STATE_CLEANING
self._attr_state = STATE_CLEANING
try:
self.robot.start_cleaning(mode, navigation, category, boundary_id)
except NeatoRobotException as ex: