Custom component translation entity name

Hello everyone,

I need your help. I am creating a new custom component and I want to translate the entities in it. I have read the documentation and I can’t understand why my entities are not translated.

Now I created a new custom component (example) just to try to understand this topic without having big complex function.

The example has two sensors and one binary sensor.
I have deleted and updated the browser cache. I just can’t get it to work.

Structure of this component:
grafik

en.json:

{
  "title": "My Component",
  "state": {
    "motion": {
      "on": "Motion detected",
      "off": "No motion"
    }
  },
  "entity": {
    "sensor": {
      "watt": {
        "name": "Watt Sensor"
      },
      "volt": {
        "name": "Volt Sensor"
      }
    },
    "binary_sensor": {
      "motion": {
        "name": "Motion Sensor"
      }
    }
  },
  "service": {
    "set_text": {
      "name": "Set Text",
      "description": "Set a custom text in the component.",
      "field": {
        "text": "The text to set."
      }
    }
  }
}

de.json/strings.json:

{
  "title": "Meine Komponente",
  "state": {
    "motion": {
      "on": "Bewegung erkannt",
      "off": "Keine Bewegung"
    }
  },
  "entity": {
    "sensor": {
      "watt": {
        "name": "Watt-Sensor"
      },
      "volt": {
        "name": "Volt-Sensor"
      }
    },
    "binary_sensor": {
      "motion": {
        "name": "Bewegungssensor"
      }
    }
  },
  "service": {
    "set_text": {
      "name": "Text setzen",
      "description": "Einen benutzerdefinierten Text in der Komponente setzen.",
      "field": {
        "text": "Der zu setzende Text."
      }
    }
  }
}

sensor.py

from homeassistant.components.sensor import (
    SensorEntity,
    SensorEntityDescription,
    SensorDeviceClass,
)
from homeassistant.const import (
    UnitOfPower,
    UnitOfElectricPotential,
)
from .const import DOMAIN

SENSOR_DESCRIPTIONS = (
    SensorEntityDescription(
        key="watt",
        name="Watt Sensor",
        unit_of_measurement=UnitOfPower.WATT,
        device_class=SensorDeviceClass.POWER,
    ),
    SensorEntityDescription(
        key="volt",
        name="Voltage Sensor",
        unit_of_measurement=UnitOfElectricPotential.VOLT,
        device_class=SensorDeviceClass.VOLTAGE,
    ),
)


async def async_setup_entry(hass, entry, async_add_entities):
    """Set up sensor platform for my_component."""
    async_add_entities(
        [
            PowerSensor(description, entry.entry_id)
            for description in SENSOR_DESCRIPTIONS
        ]
    )


class PowerSensor(SensorEntity):

    def __init__(self, description: SensorEntityDescription, entry_id):
        self.entity_description = description
        self._entry_id = entry_id
        self._state = None

    # @property
    # def name(self):
    #    return (
    #        self.hass.data[DOMAIN]
    #        .get("translations", {})
    #        .get(self.translation_key, {})
    #        .get("name", self.entity_description.name)
    #    )
    @property
    def name(self):
        return None

    @property
    def unique_id(self):
        return f"{self._entry_id}_{self.entity_description.key}"

    @property
    def translation_key(self):
        return self.entity_description.key

    @property
    def state(self):
        return self._state

    @property
    def unit_of_measurement(self):
        return self.entity_description.unit_of_measurement

    @property
    def device_class(self):
        return self.entity_description.device_class

    async def async_update(self):
        if self.entity_description.key == "watt":
            self._state = 123
        elif self.entity_description.key == "volt":
            self._state = 230

binary_sensor.py:

from homeassistant.components.binary_sensor import (
    BinarySensorEntity,
    BinarySensorEntityDescription,
)
from .const import DOMAIN

BINARY_SENSOR_DESCRIPTIONS = (
    BinarySensorEntityDescription(
        key="motion",
        name="Motion Sensor",
        device_class="motion",
    ),
)


async def async_setup_entry(hass, entry, async_add_entities):
    """Set up binary sensor platform for my_component."""
    async_add_entities(
        [
            MotionSensor(description, entry.entry_id)
            for description in BINARY_SENSOR_DESCRIPTIONS
        ]
    )


class MotionSensor(BinarySensorEntity):
    def __init__(self, description: BinarySensorEntityDescription, entry_id):
        self.entity_description = description
        self._entry_id = entry_id
        self._state = False

    @property
    def unique_id(self):
        return f"{self._entry_id}_{self.entity_description.key}"

    # @property
    # def name(self):
    #    return (
    #        self.hass.data[DOMAIN]
    #        .get("translations", {})
    #        .get(self.translation_key, {})
    #        .get("name", self.entity_description.name)
    #    )
    @property
    def name(self):
        return None

    @property
    def translation_key(self):
        return self.entity_description.key

    @property
    def is_on(self):
        return self._state

    async def async_update(self):
        self._state = True

What have I overlooked/forgotten?

  • you don’t need strings.json, it’s not used by custom components
  • if you set the name to None explicitly, the entity won’t have a name, but follow the device’s name
  • you need to set _attr_has_entity_name = True

Thank you for this hints.

My changes:

  • delete string.json (I thought that was the fallback translation for non-translated languages).
  • Remove def name(self): function in sensor.py and binary_sensor.py
  • Added self._attr_has_entity_name = True in the SesnorClasses

I can see it’s working. The entities are translated :+1:
screen

If I now switch to English, German is still displayed. Why is that?
I have restart, clear Cache!

Comprehension question:

Can I set unique_id with an English keyword and the name is still translated into German? Or is this not intended by Home Assistant?

Example:

self._attr_has_entity_name = True
self._attr_unique_id = f"{entry_id}_{description.key}"
self._attr_translation_key = description.key

self.entity_description = description

When testing, unique_id is taken from the xx.json (language file)

Entity name translation is done based on server language not user language. You need to change the Settings → System → General language option.

I discovered this after having the same head scratching issue. Not sure why it is like this but possibly because if affects the entity id when creating.