31
31
from homeassistant .const import CONDUCTIVITY
32
32
33
33
from ..core .converters .base import BaseConv
34
+ from ..core .converters .const import ENTITY_LAZY
34
35
35
36
# just to reduce the code
36
37
CELSIUS = UnitOfTemperature .CELSIUS
51
52
"statistics" : "_attr_state_class" ,
52
53
"units" : "_attr_native_unit_of_measurement" ,
53
54
"visible" : "_attr_entity_registry_visible_default" ,
55
+ "translation_key" : "_attr_translation_key" ,
54
56
}
55
57
56
58
# description with class should be used with "domain.attr"
63
65
"sensor.illuminance" : {"class" : SENSOR .ILLUMINANCE , "units" : LIGHT_LUX },
64
66
"sensor.humidity" : {"class" : SENSOR .HUMIDITY , "units" : PERCENTAGE },
65
67
"sensor.moisture" : {"class" : SENSOR .MOISTURE , "units" : PERCENTAGE },
66
- "sensor.power" : {"class" : SENSOR .POWER , "units" : UnitOfPower .WATT },
68
+ "sensor.power" : {"class" : SENSOR .POWER , "units" : UnitOfPower .WATT , "translation_key" : "power" },
67
69
"sensor.pressure" : {"class" : SENSOR .PRESSURE , "units" : UnitOfPressure .HPA },
68
70
"sensor.temperature" : {"class" : SENSOR .TEMPERATURE , "units" : CELSIUS },
69
71
"sensor.voltage" : {"class" : SENSOR .VOLTAGE , "units" : UnitOfElectricPotential .VOLT },
70
72
##
71
73
# binary sensors with device class
72
- "binary_sensor.contact" : {"class" : BinarySensorDeviceClass .DOOR },
73
- "binary_sensor.latch" : {"class" : BinarySensorDeviceClass .LOCK },
74
+ "binary_sensor.contact" : {"class" : BinarySensorDeviceClass .DOOR , "translation_key" : "contact" },
75
+ "binary_sensor.latch" : {"class" : BinarySensorDeviceClass .LOCK , "translation_key" : "latch" },
74
76
"binary_sensor.moisture" : {"class" : BinarySensorDeviceClass .MOISTURE },
75
77
"binary_sensor.plug_detection" : {"class" : BinarySensorDeviceClass .PLUG },
76
- "binary_sensor.pressure" : {"class" : BinarySensorDeviceClass .VIBRATION },
77
- "binary_sensor.reverse" : {"class" : BinarySensorDeviceClass .LOCK },
78
- "binary_sensor.square" : {"class" : BinarySensorDeviceClass .LOCK },
78
+ "binary_sensor.pressure" : {"class" : BinarySensorDeviceClass .VIBRATION , "translation_key" : "pressure" },
79
+ "binary_sensor.reverse" : {"class" : BinarySensorDeviceClass .LOCK , "translation_key" : "reverse" },
80
+ "binary_sensor.square" : {"class" : BinarySensorDeviceClass .LOCK , "translation_key" : "square" },
79
81
"binary_sensor.water_leak" : {"class" : BinarySensorDeviceClass .MOISTURE },
80
82
##
81
83
# sensors without device class
82
- "action" : {"icon" : "mdi:bell" },
83
- "conductivity" : {"icon" : "mdi:flower" , "units" : CONDUCTIVITY },
84
- "formaldehyde" : {"units" : CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER },
85
- "gas_density" : {"icon" : "mdi:google-circles-communities" , "units" : "% LEL" },
86
- "rssi" : {"units" : SIGNAL_STRENGTH_DECIBELS_MILLIWATT },
87
- "smoke_density" : {"icon" : "mdi:google-circles-communities" , "units" : "% obs/ft" },
88
- "supply" : {"icon" : "mdi:gauge" , "units" : PERCENTAGE },
89
- "tvoc" : {"icon" : "mdi:cloud" , "units" : CONCENTRATION_PARTS_PER_BILLION },
90
- "eco_two" : {"name" : "eCO2" , " icon" : "mdi:molecule-co2" , "units" : CONCENTRATION_PARTS_PER_MILLION },
84
+ "action" : {"icon" : "mdi:bell" , "translation_key" : "action" },
85
+ "conductivity" : {"icon" : "mdi:flower" , "units" : CONDUCTIVITY , "translation_key" : "conductivity" },
86
+ "formaldehyde" : {"units" : CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER , "translation_key" : "formaldehyde" },
87
+ "gas_density" : {"icon" : "mdi:google-circles-communities" , "units" : "% LEL" , "translation_key" : "gas_density" },
88
+ "rssi" : {"units" : SIGNAL_STRENGTH_DECIBELS_MILLIWATT , "translation_key" : "rssi" },
89
+ "smoke_density" : {"icon" : "mdi:google-circles-communities" , "units" : "% obs/ft" , "translation_key" : "smoke_density" },
90
+ "supply" : {"icon" : "mdi:gauge" , "units" : PERCENTAGE , "translation_key" : "supply" },
91
+ "tvoc" : {"icon" : "mdi:cloud" , "units" : CONCENTRATION_PARTS_PER_BILLION , "translation_key" : "tvoc" },
92
+ "eco_two" : {"icon" : "mdi:molecule-co2" , "units" : CONCENTRATION_PARTS_PER_MILLION , "translation_key" : "e_co2" },
91
93
##
92
94
# stats sensors
93
95
"binary_sensor.gateway" : {
94
96
"class" : BinarySensorDeviceClass .CONNECTIVITY ,
95
97
"icon" : "mdi:router-wireless" ,
98
+ "translation_key" : "gateway" ,
96
99
},
97
100
"binary_sensor.ble" : {
98
101
"class" : BinarySensorDeviceClass .CONNECTIVITY ,
99
102
"category" : DIAGNOSTIC ,
100
103
"icon" : "mdi:bluetooth" ,
104
+ "translation_key" : "ble" ,
101
105
},
102
106
"binary_sensor.mesh" : {
103
107
"class" : BinarySensorDeviceClass .CONNECTIVITY ,
104
108
"category" : DIAGNOSTIC ,
105
109
"icon" : "mdi:bluetooth" ,
110
+ "translation_key" : "mesh" ,
106
111
},
107
112
"binary_sensor.zigbee" : {
108
113
"class" : BinarySensorDeviceClass .CONNECTIVITY ,
109
114
"category" : DIAGNOSTIC ,
110
115
"icon" : "mdi:zigbee" ,
116
+ "translation_key" : "zigbee" ,
111
117
},
112
118
"sensor.ble" : {
113
119
"class" : SENSOR .TIMESTAMP ,
114
120
"category" : DIAGNOSTIC ,
115
121
"icon" : "mdi:bluetooth" ,
122
+ "translation_key" : "ble" ,
116
123
},
117
124
"sensor.mesh" : {
118
125
"class" : SENSOR .TIMESTAMP ,
119
126
"category" : DIAGNOSTIC ,
120
127
"icon" : "mdi:bluetooth" ,
128
+ "translation_key" : "mesh" ,
121
129
},
122
130
"sensor.zigbee" : {
123
131
"class" : SENSOR .TIMESTAMP ,
124
132
"category" : DIAGNOSTIC ,
125
133
"icon" : "mdi:zigbee" ,
134
+ "translation_key" : "zigbee" ,
126
135
},
127
136
##
128
137
# main controls
129
- "alarm_trigger" : {"icon" : "mdi:alarm-bell" },
130
- "fan" : {"icon" : "mdi:fan" },
131
- "outlet" : {"icon" : "mdi:power-socket-us" },
132
- "plug" : {"icon" : "mdi:power-plug" },
138
+ "alarm_trigger" : {"icon" : "mdi:alarm-bell" , "translation_key" : "alarm_trigger" },
139
+ "fan" : {"icon" : "mdi:fan" , "translation_key" : "fan" },
140
+ "light.light" : {"name" : None },
141
+ "switch.channel" : {"class" : SwitchDeviceClass .SWITCH , "translation_key" : "channel" },
142
+ "switch.outlet" : {"class" : SwitchDeviceClass .OUTLET , "icon" : "mdi:power-socket-us" , "translation_key" : "outlet" },
143
+ "switch.plug" : {"class" : SwitchDeviceClass .OUTLET , "icon" : "mdi:power-plug" , "translation_key" : "plug" },
144
+ "switch.switch" : {"class" : SwitchDeviceClass .SWITCH , "translation_key" : "switch" },
133
145
"usb" : {"icon" : "mdi:usb-port" },
134
146
##
135
147
# batteries and energy sensors
143
155
"units" : PERCENTAGE ,
144
156
"category" : DIAGNOSTIC ,
145
157
"enabled" : False ,
158
+ "translation_key" : "battery_original" ,
146
159
},
147
160
"sensor.battery_voltage" : {
148
161
"class" : SENSOR .VOLTAGE ,
149
162
"units" : UnitOfElectricPotential .MILLIVOLT ,
150
163
"category" : DIAGNOSTIC ,
164
+ "translation_key" : "battery_voltage" ,
151
165
},
152
166
"binary_sensor.battery_charging" : {
153
167
"class" : BinarySensorDeviceClass .BATTERY_CHARGING ,
163
177
"class" : SENSOR .ENERGY ,
164
178
"statistics" : SensorStateClass .TOTAL ,
165
179
"units" : UnitOfEnergy .KILO_WATT_HOUR ,
180
+ "translation_key" : "power_consumption" ,
166
181
},
167
182
##
168
183
# CONFIG controls
169
- "backlight" : {"category" : CONFIG , "enabled" : False },
170
- "blind_time" : {"category" : CONFIG , "enabled" : False },
171
- "charge_protect" : {"category" : CONFIG , "enabled" : False },
172
- "child_lock" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:baby-carriage" },
173
- "display_unit" : {"category" : CONFIG , "enabled" : False },
174
- "flex_switch" : {"category" : CONFIG , "enabled" : False },
175
- "led" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:led-off" },
176
- "led_reverse" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:led-off" },
177
- "mode" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:cog" },
178
- "motor_reverse" : {"category" : CONFIG , "enabled" : False },
179
- "motor_speed" : {"category" : CONFIG , "enabled" : False },
180
- "power_off_memory" : {"category" : CONFIG , "enabled" : False },
181
- "power_on_state" : {"category" : CONFIG , "enabled" : False },
182
- "sensitivity" : {"category" : CONFIG , "enabled" : False },
183
- "wireless" : {"category" : CONFIG , "enabled" : False },
184
+ "backlight" : {"category" : CONFIG , "enabled" : False , "translation_key" : "backlight" },
185
+ "blind_time" : {"category" : CONFIG , "enabled" : False , "translation_key" : "blind_time" },
186
+ "charge_protect" : {"category" : CONFIG , "enabled" : False , "translation_key" : "charge_protect" },
187
+ "child_lock" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:baby-carriage" , "translation_key" : "child_lock" },
188
+ "display_unit" : {"category" : CONFIG , "enabled" : False , "translation_key" : "display_unit" },
189
+ "flex_switch" : {"category" : CONFIG , "enabled" : False , "translation_key" : "flex_switch" },
190
+ "led" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:led-off" , "translation_key" : "led" },
191
+ "led_reverse" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:led-off" , "translation_key" : "led_reverse" },
192
+ "mode" : {"category" : CONFIG , "enabled" : False , "icon" : "mdi:cog" , "translation_key" : "mode" },
193
+ "motor_reverse" : {"category" : CONFIG , "enabled" : False , "translation_key" : "motor_reverse" },
194
+ "motor_speed" : {"category" : CONFIG , "enabled" : False , "translation_key" : "motor_speed" },
195
+ "power_off_memory" : {"category" : CONFIG , "enabled" : False , "translation_key" : "power_off_memory" },
196
+ "power_on_state" : {"category" : CONFIG , "enabled" : False , "translation_key" : "power_on_state" },
197
+ "sensitivity" : {"category" : CONFIG , "enabled" : False , "translation_key" : "sensitivity" },
198
+ "wireless" : {"category" : CONFIG , "enabled" : False , "translation_key" : "wireless" },
184
199
##
185
200
# DIAGNOSTIC controls
186
- "command" : {"category" : DIAGNOSTIC , "icon" : "mdi:apple-keyboard-command" },
187
- "data" : {"category" : DIAGNOSTIC , "icon" : "mdi:information-box" },
201
+ "command" : {"category" : DIAGNOSTIC , "icon" : "mdi:apple-keyboard-command" , "translation_key" : "command" },
202
+ "data" : {"category" : DIAGNOSTIC , "icon" : "mdi:information-box" , "translation_key" : "data" },
188
203
##
189
204
# CONFIG and DIAGNOSTIC sensors
190
205
"sensor.chip_temperature" : {
191
206
"class" : SENSOR .TEMPERATURE ,
192
207
"units" : UnitOfTemperature .CELSIUS ,
193
208
"category" : DIAGNOSTIC ,
194
209
"enabled" : False ,
210
+ "translation_key" : "chip_temperature" ,
195
211
},
196
- "fault" : {"category" : DIAGNOSTIC },
212
+ "fault" : {"category" : DIAGNOSTIC , "translation_key" : "fault" },
197
213
"sensor.idle_time" : {
198
214
"class" : SENSOR .DURATION ,
199
215
"icon" : "mdi:timer" ,
200
216
"units" : UnitOfTime .SECONDS ,
201
217
"category" : DIAGNOSTIC ,
202
218
"enabled" : False ,
219
+ "translation_key" : "idle_time" ,
203
220
},
204
221
}
205
222
215
232
def setup_entity_description (entity : Entity , conv : BaseConv ) -> bool :
216
233
# 1. auto match entity description based on converter domain and attr name
217
234
key = conv .attr .rstrip ("_01234567890" ) # remove tail _1, _2, _3
235
+ tail_index = conv .attr [len (key )+ 1 :] # get tail 1, 2, 3
218
236
domain_key = f"{ conv .domain } .{ key } "
219
237
desc = ENTITY_DESCRIPTIONS .get (domain_key ) or ENTITY_DESCRIPTIONS .get (key )
220
238
@@ -223,21 +241,24 @@ def setup_entity_description(entity: Entity, conv: BaseConv) -> bool:
223
241
desc = desc | conv .entity if desc else conv .entity
224
242
225
243
# 3. auto match only device_class based on converter domain
226
- if not desc :
244
+ if not desc or desc == ENTITY_LAZY :
227
245
if domain_class := DOMAIN_CLASSES .get (conv .domain ):
228
246
if key in iter (domain_class ):
229
247
entity ._attr_device_class = domain_class (key )
230
248
return True
231
249
return False
232
250
233
251
for k , v in desc .items ():
234
- if k == "lazy" or v is None :
252
+ if k == "lazy" or ( v is None and k != "name" ) :
235
253
continue
236
254
if k == "category" and type (v ) is str :
237
255
v = EntityCategory (v )
238
256
elif k == "class" and type (v ) is str :
239
257
if domain_class := DOMAIN_CLASSES .get (conv .domain ):
240
258
v = domain_class (v )
259
+ elif k == "translation_key" and tail_index :
260
+ v = f"{ v } _n"
261
+ setattr (entity , "_attr_translation_placeholders" , {"n" : tail_index })
241
262
setattr (entity , ENTITY_KEYS .get (k ) or k , v )
242
263
243
264
# sensor with unit_of_measurement and without state_class will be MEASUREMENT
0 commit comments