Skip to content

Commit 95cc3e3

Browse files
authored
Add exceptions translations for Shelly integration (#141071)
* Add exceptions translations * Improve exception strings for update platform * Fix tests * Improve device_communication_error * Remove error placeholder * Improve tests * Fix test_rpc_set_state_errors * Strings improvement * Remove `device` * Remove `entity` * Fix tests
1 parent 5f09318 commit 95cc3e3

File tree

15 files changed

+222
-54
lines changed

15 files changed

+222
-54
lines changed

homeassistant/components/shelly/__init__.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,25 @@ async def _async_setup_block_entry(
189189
if not device.firmware_supported:
190190
async_create_issue_unsupported_firmware(hass, entry)
191191
await device.shutdown()
192-
raise ConfigEntryNotReady
192+
raise ConfigEntryNotReady(
193+
translation_domain=DOMAIN,
194+
translation_key="firmware_unsupported",
195+
translation_placeholders={"device": entry.title},
196+
)
193197
except (DeviceConnectionError, MacAddressMismatchError) as err:
194198
await device.shutdown()
195-
raise ConfigEntryNotReady(repr(err)) from err
199+
raise ConfigEntryNotReady(
200+
translation_domain=DOMAIN,
201+
translation_key="device_communication_error",
202+
translation_placeholders={"device": entry.title},
203+
) from err
196204
except InvalidAuthError as err:
197205
await device.shutdown()
198-
raise ConfigEntryAuthFailed(repr(err)) from err
206+
raise ConfigEntryAuthFailed(
207+
translation_domain=DOMAIN,
208+
translation_key="auth_error",
209+
translation_placeholders={"device": entry.title},
210+
) from err
199211

200212
runtime_data.block = ShellyBlockCoordinator(hass, entry, device)
201213
runtime_data.block.async_setup()
@@ -272,16 +284,28 @@ async def _async_setup_rpc_entry(hass: HomeAssistant, entry: ShellyConfigEntry)
272284
if not device.firmware_supported:
273285
async_create_issue_unsupported_firmware(hass, entry)
274286
await device.shutdown()
275-
raise ConfigEntryNotReady
287+
raise ConfigEntryNotReady(
288+
translation_domain=DOMAIN,
289+
translation_key="firmware_unsupported",
290+
translation_placeholders={"device": entry.title},
291+
)
276292
runtime_data.rpc_script_events = await get_rpc_scripts_event_types(
277293
device, ignore_scripts=[BLE_SCRIPT_NAME]
278294
)
279295
except (DeviceConnectionError, MacAddressMismatchError, RpcCallError) as err:
280296
await device.shutdown()
281-
raise ConfigEntryNotReady(repr(err)) from err
297+
raise ConfigEntryNotReady(
298+
translation_domain=DOMAIN,
299+
translation_key="device_communication_error",
300+
translation_placeholders={"device": entry.title},
301+
) from err
282302
except InvalidAuthError as err:
283303
await device.shutdown()
284-
raise ConfigEntryAuthFailed(repr(err)) from err
304+
raise ConfigEntryAuthFailed(
305+
translation_domain=DOMAIN,
306+
translation_key="auth_error",
307+
translation_placeholders={"device": entry.title},
308+
) from err
285309

286310
runtime_data.rpc = ShellyRpcCoordinator(hass, entry, device)
287311
runtime_data.rpc.async_setup()

homeassistant/components/shelly/button.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ async def async_press(self) -> None:
193193
translation_key="device_communication_action_error",
194194
translation_placeholders={
195195
"entity": self.entity_id,
196-
"device": self.coordinator.device.name,
197-
"error": repr(err),
196+
"device": self.coordinator.name,
198197
},
199198
) from err
200199
except RpcCallError as err:
@@ -203,8 +202,7 @@ async def async_press(self) -> None:
203202
translation_key="rpc_call_action_error",
204203
translation_placeholders={
205204
"entity": self.entity_id,
206-
"device": self.coordinator.device.name,
207-
"error": repr(err),
205+
"device": self.coordinator.name,
208206
},
209207
) from err
210208
except InvalidAuthError:

homeassistant/components/shelly/climate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,12 @@ async def set_state_full_path(self, **kwargs: Any) -> Any:
326326
except DeviceConnectionError as err:
327327
self.coordinator.last_update_success = False
328328
raise HomeAssistantError(
329-
f"Setting state for entity {self.name} failed, state: {kwargs}, error:"
330-
f" {err!r}"
329+
translation_domain=DOMAIN,
330+
translation_key="device_communication_action_error",
331+
translation_placeholders={
332+
"entity": self.entity_id,
333+
"device": self.coordinator.name,
334+
},
331335
) from err
332336
except InvalidAuthError:
333337
await self.coordinator.async_shutdown_device_and_start_reauth()

homeassistant/components/shelly/coordinator.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,23 @@ async def _async_update_data(self) -> None:
378378
if self.sleep_period:
379379
# Sleeping device, no point polling it, just mark it unavailable
380380
raise UpdateFailed(
381-
f"Sleeping device did not update within {self.sleep_period} seconds interval"
381+
translation_domain=DOMAIN,
382+
translation_key="update_error_sleeping_device",
383+
translation_placeholders={
384+
"device": self.name,
385+
"period": str(self.sleep_period),
386+
},
382387
)
383388

384389
LOGGER.debug("Polling Shelly Block Device - %s", self.name)
385390
try:
386391
await self.device.update()
387392
except DeviceConnectionError as err:
388-
raise UpdateFailed(repr(err)) from err
393+
raise UpdateFailed(
394+
translation_domain=DOMAIN,
395+
translation_key="update_error",
396+
translation_placeholders={"device": self.name},
397+
) from err
389398
except InvalidAuthError:
390399
await self.async_shutdown_device_and_start_reauth()
391400

@@ -470,7 +479,11 @@ async def _async_update_data(self) -> None:
470479
return
471480
await self.device.update_shelly()
472481
except (DeviceConnectionError, MacAddressMismatchError) as err:
473-
raise UpdateFailed(repr(err)) from err
482+
raise UpdateFailed(
483+
translation_domain=DOMAIN,
484+
translation_key="update_error",
485+
translation_placeholders={"device": self.name},
486+
) from err
474487
except InvalidAuthError:
475488
await self.async_shutdown_device_and_start_reauth()
476489
else:
@@ -636,15 +649,24 @@ async def _async_update_data(self) -> None:
636649
if self.sleep_period:
637650
# Sleeping device, no point polling it, just mark it unavailable
638651
raise UpdateFailed(
639-
f"Sleeping device did not update within {self.sleep_period} seconds interval"
652+
translation_domain=DOMAIN,
653+
translation_key="update_error_sleeping_device",
654+
translation_placeholders={
655+
"device": self.name,
656+
"period": str(self.sleep_period),
657+
},
640658
)
641659

642660
async with self._connection_lock:
643661
if self.device.connected: # Already connected
644662
return
645663

646664
if not await self._async_device_connect_task():
647-
raise UpdateFailed("Device reconnect error")
665+
raise UpdateFailed(
666+
translation_domain=DOMAIN,
667+
translation_key="update_error_reconnect_error",
668+
translation_placeholders={"device": self.name},
669+
)
648670

649671
async def _async_disconnected(self, reconnect: bool) -> None:
650672
"""Handle device disconnected."""
@@ -820,13 +842,21 @@ def __init__(
820842
async def _async_update_data(self) -> None:
821843
"""Fetch data."""
822844
if not self.device.connected:
823-
raise UpdateFailed("Device disconnected")
845+
raise UpdateFailed(
846+
translation_domain=DOMAIN,
847+
translation_key="update_error_device_disconnected",
848+
translation_placeholders={"device": self.name},
849+
)
824850

825851
LOGGER.debug("Polling Shelly RPC Device - %s", self.name)
826852
try:
827853
await self.device.poll()
828854
except (DeviceConnectionError, RpcCallError) as err:
829-
raise UpdateFailed(f"Device disconnected: {err!r}") from err
855+
raise UpdateFailed(
856+
translation_domain=DOMAIN,
857+
translation_key="update_error",
858+
translation_placeholders={"device": self.name},
859+
) from err
830860
except InvalidAuthError:
831861
await self.async_shutdown_device_and_start_reauth()
832862

homeassistant/components/shelly/device_trigger.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ async def async_validate_trigger_config(
105105
return config
106106

107107
raise InvalidDeviceAutomationConfig(
108-
f"Invalid ({CONF_TYPE},{CONF_SUBTYPE}): {trigger}"
108+
translation_domain=DOMAIN,
109+
translation_key="invalid_trigger",
110+
translation_placeholders={"trigger": str(trigger)},
109111
)
110112

111113

@@ -137,7 +139,11 @@ async def async_get_triggers(
137139

138140
return triggers
139141

140-
raise InvalidDeviceAutomationConfig(f"Device not found: {device_id}")
142+
raise InvalidDeviceAutomationConfig(
143+
translation_domain=DOMAIN,
144+
translation_key="device_not_found",
145+
translation_placeholders={"device": device_id},
146+
)
141147

142148

143149
async def async_attach_trigger(

homeassistant/components/shelly/entity.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from homeassistant.helpers.typing import StateType
2020
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2121

22-
from .const import CONF_SLEEP_PERIOD, LOGGER
22+
from .const import CONF_SLEEP_PERIOD, DOMAIN, LOGGER
2323
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
2424
from .utils import (
2525
async_remove_shelly_entity,
@@ -345,8 +345,12 @@ async def set_state(self, **kwargs: Any) -> Any:
345345
except DeviceConnectionError as err:
346346
self.coordinator.last_update_success = False
347347
raise HomeAssistantError(
348-
f"Setting state for entity {self.name} failed, state: {kwargs}, error:"
349-
f" {err!r}"
348+
translation_domain=DOMAIN,
349+
translation_key="device_communication_action_error",
350+
translation_placeholders={
351+
"entity": self.entity_id,
352+
"device": self.coordinator.name,
353+
},
350354
) from err
351355
except InvalidAuthError:
352356
await self.coordinator.async_shutdown_device_and_start_reauth()
@@ -406,13 +410,21 @@ async def call_rpc(
406410
except DeviceConnectionError as err:
407411
self.coordinator.last_update_success = False
408412
raise HomeAssistantError(
409-
f"Call RPC for {self.name} connection error, method: {method}, params:"
410-
f" {params}, error: {err!r}"
413+
translation_domain=DOMAIN,
414+
translation_key="device_communication_action_error",
415+
translation_placeholders={
416+
"entity": self.entity_id,
417+
"device": self.coordinator.name,
418+
},
411419
) from err
412420
except RpcCallError as err:
413421
raise HomeAssistantError(
414-
f"Call RPC for {self.name} request error, method: {method}, params:"
415-
f" {params}, error: {err!r}"
422+
translation_domain=DOMAIN,
423+
translation_key="rpc_call_action_error",
424+
translation_placeholders={
425+
"entity": self.entity_id,
426+
"device": self.coordinator.name,
427+
},
416428
) from err
417429
except InvalidAuthError:
418430
await self.coordinator.async_shutdown_device_and_start_reauth()

homeassistant/components/shelly/number.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2626
from homeassistant.helpers.entity_registry import RegistryEntry
2727

28-
from .const import CONF_SLEEP_PERIOD, LOGGER, VIRTUAL_NUMBER_MODE_MAP
28+
from .const import CONF_SLEEP_PERIOD, DOMAIN, LOGGER, VIRTUAL_NUMBER_MODE_MAP
2929
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
3030
from .entity import (
3131
BlockEntityDescription,
@@ -324,8 +324,12 @@ async def _set_state_full_path(self, path: str, params: Any) -> Any:
324324
except DeviceConnectionError as err:
325325
self.coordinator.last_update_success = False
326326
raise HomeAssistantError(
327-
f"Setting state for entity {self.name} failed, state: {params}, error:"
328-
f" {err!r}"
327+
translation_domain=DOMAIN,
328+
translation_key="device_communication_action_error",
329+
translation_placeholders={
330+
"entity": self.entity_id,
331+
"device": self.coordinator.name,
332+
},
329333
) from err
330334
except InvalidAuthError:
331335
await self.coordinator.async_shutdown_device_and_start_reauth()

homeassistant/components/shelly/strings.json

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,44 @@
204204
}
205205
},
206206
"exceptions": {
207+
"auth_error": {
208+
"message": "Authentication failed for {device}, please update your credentials"
209+
},
210+
"device_communication_error": {
211+
"message": "Device communication error occurred for {device}"
212+
},
207213
"device_communication_action_error": {
208-
"message": "Device communication error occurred while calling the entity {entity} action for {device} device: {error}"
214+
"message": "Device communication error occurred while calling action for {entity} of {device}"
215+
},
216+
"device_not_found": {
217+
"message": "{device} not found while configuring device automation triggers"
218+
},
219+
"firmware_unsupported": {
220+
"message": "{device} is running an unsupported firmware, please update the firmware"
221+
},
222+
"invalid_trigger": {
223+
"message": "Invalid device automation trigger (type, subtype): {trigger}"
224+
},
225+
"ota_update_connection_error": {
226+
"message": "Device communication error occurred while triggering OTA update for {device}"
227+
},
228+
"ota_update_rpc_error": {
229+
"message": "RPC call error occurred while triggering OTA update for {device}"
209230
},
210231
"rpc_call_action_error": {
211-
"message": "RPC call error occurred while calling the entity {entity} action for {device} device: {error}"
232+
"message": "RPC call error occurred while calling action for {entity} of {device}"
233+
},
234+
"update_error": {
235+
"message": "An error occurred while retrieving data from {device}"
236+
},
237+
"update_error_device_disconnected": {
238+
"message": "An error occurred while retrieving data from {device} because it is disconnected"
239+
},
240+
"update_error_reconnect_error": {
241+
"message": "An error occurred while reconnecting to {device}"
242+
},
243+
"update_error_sleeping_device": {
244+
"message": "Sleeping device did not update within {period} seconds interval"
212245
}
213246
},
214247
"issues": {

homeassistant/components/shelly/update.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2626
from homeassistant.helpers.restore_state import RestoreEntity
2727

28-
from .const import CONF_SLEEP_PERIOD, OTA_BEGIN, OTA_ERROR, OTA_PROGRESS, OTA_SUCCESS
28+
from .const import (
29+
CONF_SLEEP_PERIOD,
30+
DOMAIN,
31+
OTA_BEGIN,
32+
OTA_ERROR,
33+
OTA_PROGRESS,
34+
OTA_SUCCESS,
35+
)
2936
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
3037
from .entity import (
3138
RestEntityDescription,
@@ -198,7 +205,11 @@ async def async_install(
198205
try:
199206
result = await self.coordinator.device.trigger_ota_update(beta=beta)
200207
except DeviceConnectionError as err:
201-
raise HomeAssistantError(f"Error starting OTA update: {err!r}") from err
208+
raise HomeAssistantError(
209+
translation_domain=DOMAIN,
210+
translation_key="ota_update_connection_error",
211+
translation_placeholders={"device": self.coordinator.name},
212+
) from err
202213
except InvalidAuthError:
203214
await self.coordinator.async_shutdown_device_and_start_reauth()
204215
else:
@@ -310,9 +321,20 @@ async def async_install(
310321
try:
311322
await self.coordinator.device.trigger_ota_update(beta=beta)
312323
except DeviceConnectionError as err:
313-
raise HomeAssistantError(f"OTA update connection error: {err!r}") from err
324+
raise HomeAssistantError(
325+
translation_domain=DOMAIN,
326+
translation_key="ota_update_connection_error",
327+
translation_placeholders={"device": self.coordinator.name},
328+
) from err
314329
except RpcCallError as err:
315-
raise HomeAssistantError(f"OTA update request error: {err!r}") from err
330+
raise HomeAssistantError(
331+
translation_domain=DOMAIN,
332+
translation_key="ota_update_rpc_error",
333+
translation_placeholders={
334+
"entity": self.entity_id,
335+
"device": self.coordinator.name,
336+
},
337+
) from err
316338
except InvalidAuthError:
317339
await self.coordinator.async_shutdown_device_and_start_reauth()
318340
else:

0 commit comments

Comments
 (0)