Hi there.
I’ve decided to write down another topic to ask for help because I’m banging my head for days now about the translation of my custom integration
I currently have 5 issues:
- Translating the name of my sensor. The name was initially set using the
_attr_name
. Based on the documentation, the name should be left alone and I should instead set the_attr_translation_key
which I did (see code below) but the translation doesn’t get picked up. Any idea what I’m doing wrong? - Translating state attribute names. This is documented as well, I’ve put the relevant section in my
translations/en.json
andtranslations/fr.json
but the translations don’t get picked up and I have no idea why. Any idea what I’m doing wrong? - Translating the name of my device. The sensor I create is attached to a device that is automatically created based on the
DeviceInfo
I set on the sensor (see code below) However it seems that I cannot pass atranslation_key
to that object so the question is: how can I translate its name? - Translation placeholders: I have placeholders to reuse translation set by HA core, which is documented. However my config flow show the placeholder rather than interpolated value for some of them, mainly the error message. Any ideas why?
- Translation placeholders with parameters. I’m not sure how to use these. Does the parameters have to be fields on the current object to be interpolated correctly? I couldn’t find documentation on how it works exactly.
For good measure, here is my sensor class:
class WaterSensor(CoordinatorEntity[AgurDataUpdateCoordinator], SensorEntity):
"""Agur water Sensor class."""
_attr_name = None
_attr_translation_key = "last_water_index"
_attr_icon = "mdi:counter"
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_device_class = SensorDeviceClass.WATER
_attr_native_unit_of_measurement = UnitOfVolume.LITERS
def __init__(self, coordinator: AgurDataUpdateCoordinator, unique_id: str, contract_id: str) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator=coordinator)
self.entity_id = f"{SENSOR_PLATFORM}.{DOMAIN}_water_index_{contract_id}"
self._attr_unique_id = unique_id
self._contract_id = contract_id
self._attr_extra_state_attributes = {
"last_read": self.coordinator.data[contract_id].latest_data_point.date,
"contract_id": contract_id,
"contract_address": self.coordinator.data[contract_id].contract.address,
"contract_owner": self.coordinator.data[contract_id].contract.owner,
"meter_serial_number": self.coordinator.data[contract_id].contract.meter_serial_number,
}
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.coordinator.data[contract_id].contract.meter_id)},
default_manufacturer=DEFAULT_NAME,
default_name="Water meter",
# I've tried this but that doesn't work. DeviceInfo type hint doesn't have a parameter like this
translation_key="last_water_index",
model=self.coordinator.data[contract_id].contract.meter_serial_number
)
@property
def native_value(self) -> float:
"""Return the state of the sensor."""
return self.coordinator.data[self._contract_id].latest_data_point.value
and my translations/en.json
file:
{
"title": "Agur",
"config": {
"step": {
"user": {
"title": "Agur account",
"description": "Please enter your login credential for your Agur account.",
"data": {
"username": "Username",
"password": "Password"
}
},
"configuration": {
"title": "Agur configuration",
"data": {
"contract_ids": "Available contract IDs",
"import_statistics": "Import historical statistics for selected contracts?"
},
"data_description": {
"contract_ids": "Please select the Agur contracts you wish to import in Home Assistant.",
"import_statistics": "If this is checked, then the historical data will be imported along side the new data. Please be aware that if you uncheck this after you enabled it, the statistics data will be kept."
}
}
},
"error": {
"auth": "Fail to log into your Agur account. Please check your username and password.",
"config_entry": "Fail to save the configuration. Please try again later."
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
}
},
"options": {
"step": {
"init": {
"title": "Agur configuration",
"data": {
"contract_ids": "Available contract IDs",
"import_statistics": "Import historical statistics for selected contracts?"
},
"data_description": {
"contract_ids": "Please select the Agur contracts you wish to import in Home Assistant.",
"import_statistics": "If this is checked, then the historical data will be imported along side the new data. Please be aware that if you uncheck this after you enabled it, the statistics data will be kept."
}
}
},
"error": {
"contact": "Fail to get the credentials for your Agur account. The configuration might be corrupted. Please remove this account and add it again.",
"auth": "Fail to log into your Agur account. Please check your username and password.",
"contracts": "Fail to fetch the contracts from your Agur account. Please try again later.",
"unknown": "[%key:common::config_flow::error::unknown%]"
}
},
"entity": {
"sensor": {
"last_water_index": {
"name": "Last water index",
"state_attributes": {
"last_read": "Last read",
"contract_id": "Contract ID",
"contract_owner": "Contract owner",
"contract_address": "Contract address",
"meter_serial_number": "Meter serial number"
}
}
}
}
}
In general, I find it hard to work with this translation system. Am I the only one? I’m probably doing something stupid so any help would be appreciated