Repository: home-assistant/core
File: homeassistant/components/matter/vacuum.py (Python)

Code before:
                )

        return segments

    async def async_get_segments(self) -> list[Segment]:
        """Get the segments that can be cleaned.

        Returns a list of segments containing their ids and names.
        """
        return self._current_segments()

    async def async_clean_segments(self, segment_ids: list[str], **kwargs: Any) -> None:
        """Clean the specified segments.

        Args:
            segment_ids: List of segment IDs to clean.
            **kwargs: Additional arguments (unused).

        """
        # Convert string IDs to integers
        try:
            area_ids = [int(segment_id) for segment_id in segment_ids]
        except (TypeError, ValueError) as err:
            raise ServiceValidationError(
                "Invalid segment ID; segment IDs must be numeric"
            ) from err

        # Ensure a CLEANING run mode is available before changing device state
        mode = self._get_run_mode_by_tag(ModeTag.CLEANING)
        if mode is None:
            raise HomeAssistantError(
                "No supported run mode found to start the vacuum cleaner."
            )

        # Send the SelectAreas command to the vacuum
        await self.send_device_command(
            clusters.ServiceArea.Commands.SelectAreas(newAreas=area_ids)
        )

        # Start cleaning using ChangeToMode with CLEANING tag
        await self.send_device_command(
            clusters.RvcRunMode.Commands.ChangeToMode(newMode=mode.mode)
        )

    @callback
    def _update_from_device(self) -> None:
        """Update from device."""
        self._calculate_features()
        # derive state from the run mode + operational state
        run_mode_raw: int = self.get_matter_attribute_value(
            clusters.RvcRunMode.Attributes.CurrentMode

Review comment (suggestion):
Error checking not needed, you are guaranteed to get only segment IDs from the ones that you provided.

Code after:
        return segments

    async def async_get_segments(self) -> list[Segment]:
        """Get the segments that can be cleaned.

        Returns a list of segments containing their ids and names.
        """
        return list(self._current_segments.values())

    async def async_clean_segments(self, segment_ids: list[str], **kwargs: Any) -> None:
        """Clean the specified segments.

        Args:
            segment_ids: List of segment IDs to clean.
            **kwargs: Additional arguments (unused).

        """
        area_ids = [int(segment_id) for segment_id in segment_ids]

        mode = self._get_run_mode_by_tag(ModeTag.CLEANING)
        if mode is None:
            raise HomeAssistantError(
                "No supported run mode found to start the vacuum cleaner."
            )

        response = await self.send_device_command(
            clusters.ServiceArea.Commands.SelectAreas(newAreas=area_ids)
        )

        if (
            response
            and response.status != clusters.ServiceArea.Enums.SelectAreasStatus.kSuccess
        ):
            raise HomeAssistantError(
                f"Failed to select areas: {response.statusText or response.status.name}"
            )

        await self.send_device_command(
            clusters.RvcRunMode.Commands.ChangeToMode(newMode=mode.mode)
        )

    @callback
    def _update_from_device(self) -> None:
        """Update from device."""
        self._calculate_features()
        # derive state from the run mode + operational state
        run_mode_raw: int = self.get_matter_attribute_value(
            clusters.RvcRunMode.Attributes.CurrentMode
        )
        operational_state: int = self.get_matter_attribute_value(
            clusters.RvcOperationalState.Attributes.OperationalState