27
27
from homeassistant .exceptions import HomeAssistantError , ServiceValidationError
28
28
from homeassistant .helpers import device_registry as dr
29
29
from homeassistant .helpers .storage import Store
30
+ from homeassistant .helpers .translation import async_get_exception_message
30
31
from homeassistant .helpers .update_coordinator import DataUpdateCoordinator
31
32
32
33
from .const import DOMAIN
@@ -97,6 +98,16 @@ def get_device_uid_and_ch(
97
98
return (device_uid , ch , is_chime )
98
99
99
100
101
+ def check_translation_key (err : ReolinkError ) -> str | None :
102
+ """Check if the translation key from the upstream library is present."""
103
+ if not err .translation_key :
104
+ return None
105
+ if async_get_exception_message (DOMAIN , err .translation_key ) == err .translation_key :
106
+ # translation key not found in strings.json
107
+ return None
108
+ return err .translation_key
109
+
110
+
100
111
# Decorators
101
112
def raise_translated_error [** P , R ](
102
113
func : Callable [P , Awaitable [R ]],
@@ -110,73 +121,73 @@ async def decorator_raise_translated_error(*args: P.args, **kwargs: P.kwargs) ->
110
121
except InvalidParameterError as err :
111
122
raise ServiceValidationError (
112
123
translation_domain = DOMAIN ,
113
- translation_key = "invalid_parameter" ,
124
+ translation_key = check_translation_key ( err ) or "invalid_parameter" ,
114
125
translation_placeholders = {"err" : str (err )},
115
126
) from err
116
127
except ApiError as err :
117
128
raise HomeAssistantError (
118
129
translation_domain = DOMAIN ,
119
- translation_key = "api_error" ,
130
+ translation_key = check_translation_key ( err ) or "api_error" ,
120
131
translation_placeholders = {"err" : str (err )},
121
132
) from err
122
133
except InvalidContentTypeError as err :
123
134
raise HomeAssistantError (
124
135
translation_domain = DOMAIN ,
125
- translation_key = "invalid_content_type" ,
136
+ translation_key = check_translation_key ( err ) or "invalid_content_type" ,
126
137
translation_placeholders = {"err" : str (err )},
127
138
) from err
128
139
except CredentialsInvalidError as err :
129
140
raise HomeAssistantError (
130
141
translation_domain = DOMAIN ,
131
- translation_key = "invalid_credentials" ,
142
+ translation_key = check_translation_key ( err ) or "invalid_credentials" ,
132
143
translation_placeholders = {"err" : str (err )},
133
144
) from err
134
145
except LoginError as err :
135
146
raise HomeAssistantError (
136
147
translation_domain = DOMAIN ,
137
- translation_key = "login_error" ,
148
+ translation_key = check_translation_key ( err ) or "login_error" ,
138
149
translation_placeholders = {"err" : str (err )},
139
150
) from err
140
151
except NoDataError as err :
141
152
raise HomeAssistantError (
142
153
translation_domain = DOMAIN ,
143
- translation_key = "no_data" ,
154
+ translation_key = check_translation_key ( err ) or "no_data" ,
144
155
translation_placeholders = {"err" : str (err )},
145
156
) from err
146
157
except UnexpectedDataError as err :
147
158
raise HomeAssistantError (
148
159
translation_domain = DOMAIN ,
149
- translation_key = "unexpected_data" ,
160
+ translation_key = check_translation_key ( err ) or "unexpected_data" ,
150
161
translation_placeholders = {"err" : str (err )},
151
162
) from err
152
163
except NotSupportedError as err :
153
164
raise HomeAssistantError (
154
165
translation_domain = DOMAIN ,
155
- translation_key = "not_supported" ,
166
+ translation_key = check_translation_key ( err ) or "not_supported" ,
156
167
translation_placeholders = {"err" : str (err )},
157
168
) from err
158
169
except SubscriptionError as err :
159
170
raise HomeAssistantError (
160
171
translation_domain = DOMAIN ,
161
- translation_key = "subscription_error" ,
172
+ translation_key = check_translation_key ( err ) or "subscription_error" ,
162
173
translation_placeholders = {"err" : str (err )},
163
174
) from err
164
175
except ReolinkConnectionError as err :
165
176
raise HomeAssistantError (
166
177
translation_domain = DOMAIN ,
167
- translation_key = "connection_error" ,
178
+ translation_key = check_translation_key ( err ) or "connection_error" ,
168
179
translation_placeholders = {"err" : str (err )},
169
180
) from err
170
181
except ReolinkTimeoutError as err :
171
182
raise HomeAssistantError (
172
183
translation_domain = DOMAIN ,
173
- translation_key = "timeout" ,
184
+ translation_key = check_translation_key ( err ) or "timeout" ,
174
185
translation_placeholders = {"err" : str (err )},
175
186
) from err
176
187
except ReolinkError as err :
177
188
raise HomeAssistantError (
178
189
translation_domain = DOMAIN ,
179
- translation_key = "unexpected" ,
190
+ translation_key = check_translation_key ( err ) or "unexpected" ,
180
191
translation_placeholders = {"err" : str (err )},
181
192
) from err
182
193
0 commit comments