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

Code before:
@dataclass(frozen=True, kw_only=True)
class MatterNumericSwitchEntityDescription(
    SwitchEntityDescription, MatterEntityDescription
):
    """Describe Matter Numeric Switch entities."""


class MatterNumericSwitch(MatterSwitch):
    """Representation of a Matter Enum Attribute as a Switch entity."""

    entity_description: MatterNumericSwitchEntityDescription

    async def _async_set_native_value(self, value: bool) -> None:
        """Update the current value."""
        if value_convert := self.entity_description.ha_to_device:
            send_value = value_convert(value)
        await self.write_attribute(
            value=send_value,
        )

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

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

    @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


# Discovery schema(s) to map Matter Attributes to HA entities
DISCOVERY_SCHEMAS = [
    MatterDiscoverySchema(
        platform=Platform.SWITCH,
        entity_description=MatterSwitchEntityDescription(
            key="MatterPlug",
            device_class=SwitchDeviceClass.OUTLET,
            name=None,
        ),
        entity_class=MatterSwitch,
        required_attributes=(clusters.OnOff.Attributes.OnOff,),
        device_type=(device_types.OnOffPlugInUnit,),
    ),
    MatterDiscoverySchema(

Review comment (suggestion):
Note: The `Any` typing for `send_value` here is fine, because even though `value` is a `bool`, the output from `ha_to_device` can be `Any`(thing). It is assigned to `send_value` below if `ha_to_device` exists. `write_attributes` is also typed to accept `Any` (as value to write).

Alternatively, we could also do something like this:
```suggestion
        if value_convert := self.entity_description.ha_to_device:
            await self.write_attribute(value=value_convert(value))
        else:
            await self.write_attribute(value=value)
```

Either is fine IMO.

Code after:
@dataclass(frozen=True, kw_only=True)
class MatterGenericCommandSwitchEntityDescription(MatterSwitchEntityDescription):
    """Describe Matter Generic command Switch entities."""

    # command: a custom callback to create the command to send to the device
    on_command: Callable[[], Any] | None = None
    off_command: Callable[[], Any] | None = None
    command_timeout: int | None = None


@dataclass(frozen=True, kw_only=True)
class MatterNumericSwitchEntityDescription(MatterSwitchEntityDescription):
    """Describe Matter Numeric Switch entities."""


class MatterNumericSwitch(MatterSwitch):
    """Representation of a Matter Enum Attribute as a Switch entity."""

    entity_description: MatterNumericSwitchEntityDescription

    async def _async_set_native_value(self, value: bool) -> None:
        """Update the current value."""
        send_value: Any = value
        if value_convert := self.entity_description.ha_to_device:
            send_value = value_convert(value)
        await self.write_attribute(value=send_value)

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

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

    @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


# Discovery schema(s) to map Matter Attributes to HA entities
DISCOVERY_SCHEMAS = [
    MatterDiscoverySchema(
        platform=Platform.SWITCH,
        entity_description=MatterSwitchEntityDescription(
            key="MatterPlug",
            device_class=SwitchDeviceClass.OUTLET,