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

Code before:
            if area_name:
                segments.append(
                    Segment(
                        id=str(area.areaID),
                        name=area_name,
                        group=None,
                    )
                )

        return 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
        area_ids = [int(segment_id) for segment_id in segment_ids]

        # 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
        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."
            )

        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
        )
        state: VacuumActivity | None = None
        if TYPE_CHECKING:
            assert self._supported_run_modes is not None

Review comment (bug):
Does this not already start cleaning, at least on some vacuums? If yes, then we will likely need to deal with the next command (`ChangeToMode`) failing?

Code after:

        segments: dict[str, Segment] = {}
        for area in supported_areas:
            area_name = None
            if area.areaInfo and area.areaInfo.locationInfo:
                area_name = area.areaInfo.locationInfo.locationName

            if area_name:
                segment_id = str(area.areaID)
                segments[segment_id] = Segment(id=segment_id, name=area_name)

        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)
        )