ServiceValidationError in HVAC code

Hi,

Newby here… (so bear with me… …)

I’m looking for some examples to code the ServiceValidationError in my function… I have a function to set the hvac mode of a thermostat based on an array of possible modes… if the user still sends a mode that is not supported, it needs to raise a ServiceValidationError…

This is my original code

    @api_call
    async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
        """Set the new hvac mode."""
        if hvac_mode not in self.hvac_mode:
            await self._channel.set_mode(hvac_mode)
            self.async_write_ha_state()

in which i need to raise an exception; the feedback language was:

      if hvac_mode not in self.hvac_modes:
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key=unsupported_hvac_mode
                translation_placeholders={"hvac_mode":hvac_mode}

but that obviously is not yet fully complete… but not sure how to make it completely work…

The translation_key needs to be a string and then you need to make the translation in the translation json files

But in climate we will anyway validate your now setting a mode which isn’t part of hvac_modes so this validation your making won’t be necessary mid-term (6m deprecation before failing)

I’ve rewritten it like this… but Ruff keeps failing … and I dont know why

    @api_call
    async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
        """Set the new hvac mode."""
        if hvac_mode not in self._attr_hvac_modes:
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="invalid_hvac_mode",
                translation_placeholders={"hvac_mode": hvac_mode},
            )
        await self._channel.set_mode(hvac_mode)
        self.async_write_ha_state()

also fails when I make it like this…

                translation_placeholders={"hvac_mode": hvac_mode})

I dont know what to do… any ideas?

Probably need to make it a string:
translation_placeholders={"hvac_mode": str(hvac_mode)})