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

Code before:
    """Set up switch entities for the pumps on the tub."""

    controller = entry.runtime_data

    entities = [
        SmartTubPump(controller.coordinator, pump)
        for spa in controller.spas
        for pump in controller.coordinator.data[spa.id][ATTR_PUMPS].values()
    ]

    async_add_entities(entities)


class SmartTubPump(SmartTubEntity, SwitchEntity):
    """A pump on a spa."""

    def __init__(
        self, coordinator: DataUpdateCoordinator[dict[str, Any]], pump: SpaPump
    ) -> None:
        """Initialize the entity."""
        super().__init__(coordinator, pump.spa, "pump")
        self.pump_id = pump.id
        self.pump_type = pump.type
        self._attr_unique_id = f"{super().unique_id}-{pump.id}"
        # Remove _attr_name set by base class so translation_key takes effect
        del self._attr_name
        if pump.type == SpaPump.PumpType.CIRCULATION:
            self._attr_translation_key = "circulation_pump"
        elif pump.type == SpaPump.PumpType.JET:
            self._attr_translation_key = "jet"
            self._attr_translation_placeholders = {"pump_id": str(pump.id)}
        else:
            self._attr_translation_key = "pump"
            self._attr_translation_placeholders = {"pump_id": str(pump.id)}

    @property
    def pump(self) -> SpaPump:
        """Return the underlying SpaPump object for this entity."""
        return self.coordinator.data[self.spa.id][ATTR_PUMPS][self.pump_id]

    @property
    def is_on(self) -> bool:
        """Return True if the pump is on."""
        return self.pump.state != SpaPump.PumpState.OFF

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn the pump on."""

        # the API only supports toggling
        if not self.is_on:
            await self.async_toggle()

Review comment (suggestion):
Please avoid this `del`
You should instead remove the explicit set in the base entity, and use an explicit set only where required.

Code after:
    """Set up switch entities for the pumps on the tub."""

    controller = entry.runtime_data

    entities = [
        SmartTubPump(controller.coordinator, pump)
        for spa in controller.spas
        for pump in controller.coordinator.data[spa.id][ATTR_PUMPS].values()
    ]

    async_add_entities(entities)


class SmartTubPump(SmartTubEntity, SwitchEntity):
    """A pump on a spa."""

    def __init__(
        self, coordinator: DataUpdateCoordinator[dict[str, Any]], pump: SpaPump
    ) -> None:
        """Initialize the entity."""
        super().__init__(coordinator, pump.spa, "pump")
        self.pump_id = pump.id
        self.pump_type = pump.type
        self._attr_unique_id = f"{super().unique_id}-{pump.id}"
        if pump.type == SpaPump.PumpType.CIRCULATION:
            self._attr_translation_key = "circulation_pump"
        elif pump.type == SpaPump.PumpType.JET:
            self._attr_translation_key = "jet"
            self._attr_translation_placeholders = {"pump_id": str(pump.id)}
        else:
            self._attr_translation_key = "pump"
            self._attr_translation_placeholders = {"pump_id": str(pump.id)}

    @property
    def pump(self) -> SpaPump:
        """Return the underlying SpaPump object for this entity."""
        return self.coordinator.data[self.spa.id][ATTR_PUMPS][self.pump_id]

    @property
    def is_on(self) -> bool:
        """Return True if the pump is on."""
        return self.pump.state != SpaPump.PumpState.OFF

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn the pump on."""

        # the API only supports toggling
        if not self.is_on:
            await self.async_toggle()

    async def async_turn_off(self, **kwargs: Any) -> None: