From 2c45d43e7b87d9e911df3e356dcbd1898cded692 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 5 Sep 2023 16:33:46 +0200 Subject: [PATCH] Use shorthand attributes in Neato (#99605) Co-authored-by: Robert Resch --- homeassistant/components/neato/camera.py | 6 +--- homeassistant/components/neato/entity.py | 6 +--- homeassistant/components/neato/sensor.py | 38 ++++++------------------ homeassistant/components/neato/switch.py | 26 ++++------------ homeassistant/components/neato/vacuum.py | 26 +++++++--------- 5 files changed, 27 insertions(+), 75 deletions(-) diff --git a/homeassistant/components/neato/camera.py b/homeassistant/components/neato/camera.py index c1513bb1de6..9ce66a53622 100644 --- a/homeassistant/components/neato/camera.py +++ b/homeassistant/components/neato/camera.py @@ -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.""" diff --git a/homeassistant/components/neato/entity.py b/homeassistant/components/neato/entity.py index 43072f19693..46ad358c638 100644 --- a/homeassistant/components/neato/entity.py +++ b/homeassistant/components/neato/entity.py @@ -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, ) diff --git a/homeassistant/components/neato/sensor.py b/homeassistant/components/neato/sensor.py index 452f1bc3a9c..3b68ddcf3df 100644 --- a/homeassistant/components/neato/sensor.py +++ b/homeassistant/components/neato/sensor.py @@ -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 diff --git a/homeassistant/components/neato/switch.py b/homeassistant/components/neato/switch.py index a80d05eef23..ae90a8230b2 100644 --- a/homeassistant/components/neato/switch.py +++ b/homeassistant/components/neato/switch.py @@ -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: diff --git a/homeassistant/components/neato/vacuum.py b/homeassistant/components/neato/vacuum.py index ecc39e515c2..891b090d5d3 100644 --- a/homeassistant/components/neato/vacuum.py +++ b/homeassistant/components/neato/vacuum.py @@ -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: