Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions homeassistant/components/indevolt/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@
type IndevoltConfigEntry = ConfigEntry[IndevoltCoordinator]


class DeviceTimeoutError(HomeAssistantError):
"""Raised when device push times out."""


class DeviceConnectionError(HomeAssistantError):
"""Raised when device push fails due to connection issues."""


class IndevoltCoordinator(DataUpdateCoordinator[dict[str, Any]]):
"""Coordinator for fetching and pushing data to indevolt devices."""

Expand Down Expand Up @@ -96,12 +88,7 @@ async def _async_update_data(self) -> dict[str, Any]:

async def async_push_data(self, sensor_key: str, value: Any) -> bool:
"""Push/write data values to given key on the device."""
try:
return await self.api.set_data(sensor_key, value)
except TimeoutError as err:
raise DeviceTimeoutError(f"Device push timed out: {err}") from err
except (ClientError, OSError) as err:
raise DeviceConnectionError(f"Device push failed: {err}") from err
return await self.api.set_data(sensor_key, value)
Comment thread
Xirt marked this conversation as resolved.

async def async_switch_energy_mode(
self, target_mode: IndevoltEnergyMode, refresh: bool = True
Expand All @@ -125,15 +112,9 @@ async def async_switch_energy_mode(

# Switch energy mode if required
if current_mode != target_mode:
try:
success = await self.async_push_data(
IndevoltConfig.WRITE_ENERGY_MODE, target_mode
)
except (DeviceTimeoutError, DeviceConnectionError) as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="failed_to_switch_energy_mode",
) from err
success = await self.async_push_data(
IndevoltConfig.WRITE_ENERGY_MODE, target_mode
)

if not success:
raise HomeAssistantError(
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/indevolt/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import IndevoltConfigEntry
from .const import DOMAIN
from .coordinator import IndevoltCoordinator
from .entity import IndevoltEntity

Expand Down Expand Up @@ -138,4 +139,8 @@ async def async_set_native_value(self, value: float) -> None:
await self.coordinator.async_request_refresh()

else:
raise HomeAssistantError(f"Failed to set value {int_value} for {self.name}")
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="write_error",
translation_placeholders={"name": str(self.name)},
)
3 changes: 2 additions & 1 deletion homeassistant/components/indevolt/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ rules:
entity-device-class: done
entity-disabled-by-default: done
entity-translations: done
exception-translations: todo
exception-translations: done
Comment thread
Xirt marked this conversation as resolved.
icon-translations: done
reconfiguration-flow: done
repair-issues:
Expand All @@ -69,6 +69,7 @@ rules:
stale-devices:
status: exempt
comment: Integration represents a single device, not a hub with multiple devices

# Platinum
async-dependency: done
inject-websession: done
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/indevolt/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import IndevoltConfigEntry
from .const import DOMAIN
from .coordinator import IndevoltCoordinator
from .entity import IndevoltEntity

Expand Down Expand Up @@ -108,4 +109,8 @@ async def async_select_option(self, option: str) -> None:
await self.coordinator.async_request_refresh()

else:
raise HomeAssistantError(f"Failed to set option {option} for {self.name}")
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="write_error",
translation_placeholders={"name": str(self.name)},
)
Comment thread
Xirt marked this conversation as resolved.
3 changes: 3 additions & 0 deletions homeassistant/components/indevolt/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@
},
"soc_below_minimum": {
"message": "Target SOC ({target}%) is below the device minimum ({minimum_soc}%)"
},
"write_error": {
"message": "Cannot update value for {name}"
}
Comment thread
Xirt marked this conversation as resolved.
},
"services": {
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/indevolt/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import IndevoltConfigEntry
from .const import DOMAIN
from .coordinator import IndevoltCoordinator
from .entity import IndevoltEntity

Expand Down Expand Up @@ -128,4 +129,8 @@ async def _async_toggle(self, value: int) -> None:
await self.coordinator.async_request_refresh()

else:
raise HomeAssistantError(f"Failed to set value {value} for {self.name}")
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="write_error",
translation_placeholders={"name": str(self.name)},
)
5 changes: 1 addition & 4 deletions tests/components/indevolt/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ async def test_number_set_value_error(
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.NUMBER]):
await setup_integration(hass, mock_config_entry)

# Mock set_data to raise an error
mock_indevolt.set_data.side_effect = HomeAssistantError(
"Device communication failed"
)
mock_indevolt.set_data.return_value = False

# Attempt to set value
with pytest.raises(HomeAssistantError):
Expand Down
5 changes: 1 addition & 4 deletions tests/components/indevolt/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ async def test_select_set_option_error(
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SELECT]):
await setup_integration(hass, mock_config_entry)

# Mock set_data to raise an error
mock_indevolt.set_data.side_effect = HomeAssistantError(
"Device communication failed"
)
mock_indevolt.set_data.return_value = False

# Attempt to change option
with pytest.raises(HomeAssistantError):
Expand Down
4 changes: 2 additions & 2 deletions tests/components/indevolt/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ async def test_single_device_execution_failure(
await setup_integration(hass, mock_config_entry)

# Simulate an API push failure
mock_indevolt.set_data.side_effect = OSError("Device push failed")
mock_indevolt.set_data.return_value = False

# Mock call to start charging
with pytest.raises(HomeAssistantError) as exc_info:
Expand Down Expand Up @@ -409,7 +409,7 @@ async def test_multi_device_execution_failure(
await setup_integration(hass, alt_mock_config_entry)

# Simulate an API push failure (triggers for both coordinators)
mock_indevolt.set_data.side_effect = OSError("Device push failed")
mock_indevolt.set_data.return_value = False

# Mock call to start charging both devices
with pytest.raises(HomeAssistantError) as exc_info:
Expand Down
5 changes: 1 addition & 4 deletions tests/components/indevolt/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ async def test_switch_set_value_error(
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SWITCH]):
await setup_integration(hass, mock_config_entry)

# Mock set_data to raise an error
mock_indevolt.set_data.side_effect = HomeAssistantError(
"Device communication failed"
)
mock_indevolt.set_data.return_value = False

# Attempt to switch on
with pytest.raises(HomeAssistantError):
Expand Down