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

Code before:
        )

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn switch off."""
        await self.send_device_command(
            clusters.OnOff.Commands.Off(),
        )

    @callback
    def _update_from_device(self) -> None:
        """Update from device."""
        self._attr_is_on = self.get_matter_attribute_value(
            self._entity_info.primary_attribute
        )


class MatterGenericCommandSwitch(MatterSwitch):
    """Representation of a Matter switch."""

    entity_description: MatterGenericCommandSwitchEntityDescription

    _platform_translation_key = "switch"

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn switch on."""
        if self.entity_description.on_command:
            # custom command defined to set the new value
            await self.send_device_command(
                self.entity_description.on_command(),
                self.entity_description.command_timeout,
            )

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn switch off."""
        if self.entity_description.off_command:
            await self.send_device_command(
                self.entity_description.off_command(),
                self.entity_description.command_timeout,
            )

    @callback
    def _update_from_device(self) -> None:
        """Update from device."""
        value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
        if value_convert := self.entity_description.device_to_ha:
            value = value_convert(value)
        self._attr_is_on = value

    async def send_device_command(
        self,
        command: ClusterCommand,

Review comment (suggestion):
I'm not sure if this can ever really happen, but if we get `None` for `get_matter_attribute_value` and `inverted=True`, we'd evaluate `not None` which is `True`. We may want to set `self._attr_is_on` to `None` instead, so it shows as `unknown` state in HA though. I guess if we want to care about that, we could add a check like this (and also make sure a test covers that):

```suggestion
        value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
        if self.entity_description.inverted and value is not None:
            value = not value
        self._attr_is_on = value
```

Code after:
        """Get the appropriate command for the desired value.

        Applies inversion if needed (e.g., for inverted logic like mute).
        """
        send_value = not value if self.entity_description.inverted else value
        return (
            clusters.OnOff.Commands.On()
            if send_value
            else clusters.OnOff.Commands.Off()
        )

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn switch on."""
        await self.send_device_command(self._get_command_for_value(True))

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn switch off."""
        await self.send_device_command(self._get_command_for_value(False))

    @callback
    def _update_from_device(self) -> None:
        """Update from device."""
        value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
        if self.entity_description.inverted:
            value = not value
        self._attr_is_on = value


class MatterGenericCommandSwitch(MatterSwitch):
    """Representation of a Matter switch."""

    entity_description: MatterGenericCommandSwitchEntityDescription

    _platform_translation_key = "switch"

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn switch on."""
        if self.entity_description.on_command:
            # custom command defined to set the new value
            await self.send_device_command(
                self.entity_description.on_command(),
                self.entity_description.command_timeout,
            )

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn switch off."""
        if self.entity_description.off_command:
            await self.send_device_command(
                self.entity_description.off_command(),
                self.entity_description.command_timeout,
            )