Repository: home-assistant/core
File: homeassistant/components/pooldose/entity.py (Python)

Code before:
        platform_name: Literal["sensor", "switch", "number", "binary_sensor", "select"],
    ) -> None:
        """Initialize PoolDose entity."""
        super().__init__(coordinator)
        self.entity_description = entity_description
        self.platform_name = platform_name
        self._attr_unique_id = f"{serial_number}_{entity_description.key}"
        self._attr_device_info = device_info(
            device_properties,
            serial_number,
            coordinator.config_entry.data.get(CONF_MAC),
        )

    @property
    def available(self) -> bool:
        """Return if entity is available."""
        return super().available and self.get_data() is not None

    def get_data(self) -> ValueDict | None:
        """Get data for this entity, only if available."""
        platform_data = self.coordinator.data[self.platform_name]
        return platform_data.get(self.entity_description.key)

    async def _async_perform_write(
        self,
        api_call: Callable[[str, Any], Awaitable[bool]],
        key: str,
        value: bool | str | float,
    ) -> None:
        """Perform a write call to the API with unified error handling.

        - `api_call` should be a bound coroutine function like
          `self.coordinator.client.set_number`.
        - Raises ServiceValidationError on connection errors or when the API
          returns False.
        """
        if not await api_call(key, value):
            if not self.coordinator.client.is_connected:
                raise ServiceValidationError(
                    translation_domain=DOMAIN, translation_key="cannot_connect"
                )

            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="write_rejected",
                translation_placeholders={
                    "entity": self.entity_description.key,
                    "value": str(value),
                },
            )


Review comment (suggestion):
```suggestion
        api_call: Callable[[str, Any], Coroutine[Any, Any, bool]],
```

Code after:
        platform_name: Literal["sensor", "switch", "number", "binary_sensor", "select"],
    ) -> None:
        """Initialize PoolDose entity."""
        super().__init__(coordinator)
        self.entity_description = entity_description
        self.platform_name = platform_name
        self._attr_unique_id = f"{serial_number}_{entity_description.key}"
        self._attr_device_info = device_info(
            device_properties,
            serial_number,
            coordinator.config_entry.data.get(CONF_MAC),
        )

    @property
    def available(self) -> bool:
        """Return if entity is available."""
        return super().available and self.get_data() is not None

    def get_data(self) -> ValueDict | None:
        """Get data for this entity, only if available."""
        platform_data = self.coordinator.data[self.platform_name]
        return platform_data.get(self.entity_description.key)

    async def _async_perform_write(
        self,
        api_call: Callable[[str, Any], Coroutine[Any, Any, bool]],
        key: str,
        value: bool | str | float,
    ) -> None:
        """Perform a write call to the API with unified error handling.

        - `api_call` should be a bound coroutine function like
          `self.coordinator.client.set_number`.
        - Raises ServiceValidationError on connection errors or when the API
          returns False.
        """
        if not await api_call(key, value):
            if not self.coordinator.client.is_connected:
                raise ServiceValidationError(
                    translation_domain=DOMAIN, translation_key="cannot_connect"
                )

            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="write_rejected",
                translation_placeholders={
                    "entity": self.entity_description.key,
                    "value": str(value),
                },
            )