Repository: home-assistant/core
File: homeassistant/components/saunum/number.py (Python)

Code before:
    ) -> None:
        """Initialize the number entity."""
        super().__init__(coordinator)
        self._attr_unique_id = f"{coordinator.config_entry.entry_id}-{description.key}"
        self.entity_description = description

    @property
    def native_value(self) -> float | None:
        """Return the current value."""
        return self.entity_description.value_fn(self.coordinator.data)

    async def async_set_native_value(self, value: float) -> None:
        """Set new value."""
        # Prevent changing certain settings when session is active
        session_active = self.coordinator.data.session_active
        if session_active and self.entity_description.key in (
            "sauna_duration",
            "fan_duration",
        ):
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="session_active_cannot_change",
                translation_placeholders={"setting": self.entity_description.key},
            )

        try:
            await self.entity_description.set_value_fn(self.coordinator.client, value)
        except SaunumException as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="set_value_failed",
                translation_placeholders={
                    "entity": self.entity_description.key,
                    "value": str(value),
                },
            ) from err

        await self.coordinator.async_request_refresh()


Review comment (suggestion):
So for now
1. Just keep it like this until you have an entity that is different
2. If you already expect a new variety to be there, just add a `requires_no_session_active` to the entity description. This way you make this class that 🤏🏻 tiny bit more generic :)

Code after:

    entity_description: LeilSaunaNumberEntityDescription

    def __init__(
        self,
        coordinator: LeilSaunaCoordinator,
        description: LeilSaunaNumberEntityDescription,
    ) -> None:
        """Initialize the number entity."""
        super().__init__(coordinator)
        self._attr_unique_id = f"{coordinator.config_entry.entry_id}-{description.key}"
        self.entity_description = description

    @property
    def native_value(self) -> float | None:
        """Return the current value."""
        return self.entity_description.value_fn(self.coordinator.data)

    async def async_set_native_value(self, value: float) -> None:
        """Set new value."""
        # Prevent changing certain settings when session is active
        session_active = self.coordinator.data.session_active
        if session_active and self.entity_description.key in (
            "sauna_duration",
            "fan_duration",
        ):
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key=f"session_active_cannot_change_{self.entity_description.key}",
            )

        try:
            await self.entity_description.set_value_fn(self.coordinator.client, value)
        except SaunumException as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="set_value_failed",
                translation_placeholders={
                    "entity": self.entity_description.key,
                    "value": str(value),
                },
            ) from err

        await self.coordinator.async_request_refresh()