#fan_mode value translate in climate.py for custom integration

1 messages · Page 1 of 1 (latest)

blazing bloom
#

I’m using climate.py defined some custom fan_modes, my codes in fan_modes is

FAN_SILENT = "silent"
FAN_FULL_SPEED = "full"

self._attr_fan_modes = list(self._fan_speeds.keys())

        @property
    def fan_mode(self) -> str:
        """AC Climate fan mode."""
        fan_speed = cast("int", self._device.get_attribute(ACAttributes.fan_speed))
        if fan_speed > FanSpeed.AUTO:
            return str(FAN_AUTO)
        if fan_speed > FanSpeed.FULL_SPEED:
            return str(FAN_FULL_SPEED)
        if fan_speed > FanSpeed.HIGH:
            return str(FAN_HIGH)
        if fan_speed > FanSpeed.MEDIUM:
            return str(FAN_MEDIUM)
        if fan_speed > FanSpeed.LOW:
            return str(FAN_LOW)
        return str(FAN_SILENT)

add the translate files, for exmaple custom_components/midea_ac_lan/translations/en.json

{
  "state": {
    "climate": {
      "fan_mode": {
        "silent": "Silent",
        "full": "Full"
      }
    }
  },
  "component": {
    "midea_ac_lan": {
      "state": {
        "fan_mode": {
          "silent": "Silent",
          "full": "Full"
        }
      }
    }
  },
  "entity": {
    "climate": {
      "state": {
        "fan_mode": {
          "silent": "Silent",
          "full": "Full"
        }
      },
      "midea_ac_lan": {
        "state_attributes": {
          "fan_mode": {
            "silent": "Silent",
            "full": "Full"
          }
        }
      }
    }
  }
}

also tried with :

"entity_component": {
    "climate": {
      "state": {
        "silent": "Silent",
        "full": "Full"
      }
    }
  },

but the translate in en.json still can’t works for the web UI, transltae value always the original slient and full , any changes in en.json can’t show in web UI, as it always display the origin english value in climate.py with

FAN_SILENT = "silent"
FAN_FULL_SPEED = "full"

any suggestion or debug steps?

cyan bridge
#

You need to have a translation key and use has_entity_name set to True

blazing bloom
# cyan bridge You need to have a translation key and use `has_entity_name` set to `True`

thank you very much for your help, it should available and I forgot to post it:

 0xAC: {
        "name": "Air Conditioner",
        "entities": {
            "climate": {
                "type": Platform.CLIMATE,
                "has_entity_name": True,
                "icon": "mdi:air-conditioner",
                "default": True,
            },
            "fresh_air": {
                "type": Platform.FAN,
                "has_entity_name": True,
                "translation_key": "fresh_air",
                "name": "Fresh Air",
                "icon": "mdi:fan",
            },

continue with

class MideaEntity(Entity):
    """Base Midea entity."""

    def __init__(self, device: MideaDevice, entity_key: str) -> None:
        """Initialize Midea base entity."""
        self._device = device
        self._device.register_update(self.update_state)
        self._config = cast(
            "dict",
            MIDEA_DEVICES[self._device.device_type]["entities"],
        )[entity_key]
        self._entity_key = entity_key
        self._unique_id = f"{DOMAIN}.{self._device.device_id}_{entity_key}"
        self.entity_id = self._unique_id
        self._device_name = self._device.name

        self._attr_translation_key = self._config.get("translation_key")
        self._attr_has_entity_name = self._config.get("has_entity_name", False)
        if self.has_entity_name:
            if self._config.get("name") is None:
                self._attr_name = None
        else:
            # old behavior
            self._attr_name = (
                f"{self._device_name} {self._config.get('name')}"
                if "name" in self._config
                else self._device_name
            )

most of the default HA core builtin fan_mode value (like HIGH/AUTO/LOW) can be display in different language, only these two value display in the origin value.
some custom attr and value also works well.