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:
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?