mirror of
https://github.com/home-assistant/core.git
synced 2026-07-15 18:44:20 +01:00
Refactor perform action in MELCloud Home (#176449)
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
"""Commonly shared code for the MELCloud Home integration."""
|
||||
|
||||
from collections.abc import Callable, Iterable
|
||||
from collections.abc import Callable, Coroutine, Iterable
|
||||
from typing import Any
|
||||
|
||||
from aiomelcloudhome import ATAUnit, ATWUnit
|
||||
from aiomelcloudhome import (
|
||||
ATAUnit,
|
||||
ATWUnit,
|
||||
MelCloudHomeAuthenticationError,
|
||||
MelCloudHomeConnectionError,
|
||||
MelCloudHomeTimeoutError,
|
||||
)
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MelCloudHomeCoordinator
|
||||
|
||||
|
||||
@@ -33,6 +42,32 @@ def async_setup_unit_entities(
|
||||
_async_add_new_atw_units(list(coordinator.atw_units.values()))
|
||||
|
||||
|
||||
async def perform_action(
|
||||
coordinator: MelCloudHomeCoordinator,
|
||||
coroutine: Coroutine[Any, Any, None],
|
||||
) -> None:
|
||||
"""Perform a MELCloud Home action with error handling and coordinator refresh."""
|
||||
try:
|
||||
await coroutine
|
||||
except MelCloudHomeAuthenticationError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_auth",
|
||||
) from err
|
||||
except MelCloudHomeConnectionError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="cannot_connect",
|
||||
) from err
|
||||
except MelCloudHomeTimeoutError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="timeout_connect",
|
||||
) from err
|
||||
else:
|
||||
await coordinator.async_request_refresh()
|
||||
|
||||
|
||||
def unit_ids(unit: ATAUnit | ATWUnit) -> dict[str, list[str]]:
|
||||
"""Return the client keyword argument selecting this unit."""
|
||||
if isinstance(unit, ATAUnit):
|
||||
|
||||
@@ -5,11 +5,6 @@ from dataclasses import dataclass
|
||||
from typing import Any, override
|
||||
|
||||
from aiomelcloudhome import ATAUnit, ATWUnit, MELCloudHome
|
||||
from aiomelcloudhome.exceptions import (
|
||||
MelCloudHomeAuthenticationError,
|
||||
MelCloudHomeConnectionError,
|
||||
MelCloudHomeTimeoutError,
|
||||
)
|
||||
|
||||
from homeassistant.components.number import (
|
||||
NumberDeviceClass,
|
||||
@@ -21,7 +16,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import async_setup_unit_entities, unit_ids
|
||||
from .common import async_setup_unit_entities, perform_action, unit_ids
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MelCloudHomeConfigEntry, MelCloudHomeCoordinator
|
||||
from .entity import MelCloudHomeATAUnitEntity, MelCloudHomeATWUnitEntity
|
||||
@@ -182,32 +177,6 @@ ATW_NUMBERS: tuple[MelCloudHomeNumberEntityDescription[ATWUnit], ...] = (
|
||||
)
|
||||
|
||||
|
||||
async def _perform_action(
|
||||
coordinator: MelCloudHomeCoordinator,
|
||||
coroutine: Coroutine[Any, Any, None],
|
||||
) -> None:
|
||||
"""Perform a MELCloud Home action with error handling and coordinator refresh."""
|
||||
try:
|
||||
await coroutine
|
||||
except MelCloudHomeAuthenticationError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_auth",
|
||||
) from err
|
||||
except MelCloudHomeConnectionError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="cannot_connect",
|
||||
) from err
|
||||
except MelCloudHomeTimeoutError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="timeout_connect",
|
||||
) from err
|
||||
else:
|
||||
await coordinator.async_request_refresh()
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MelCloudHomeConfigEntry,
|
||||
@@ -269,7 +238,7 @@ class ATANumber(MelCloudHomeATAUnitEntity, NumberEntity):
|
||||
translation_domain=DOMAIN,
|
||||
translation_key=error_key,
|
||||
)
|
||||
await _perform_action(
|
||||
await perform_action(
|
||||
self.coordinator,
|
||||
self.entity_description.set_value_fn(
|
||||
self.coordinator.client, self.unit, value
|
||||
@@ -315,7 +284,7 @@ class ATWNumber(MelCloudHomeATWUnitEntity, NumberEntity):
|
||||
translation_domain=DOMAIN,
|
||||
translation_key=error_key,
|
||||
)
|
||||
await _perform_action(
|
||||
await perform_action(
|
||||
self.coordinator,
|
||||
self.entity_description.set_value_fn(
|
||||
self.coordinator.client, self.unit, value
|
||||
|
||||
@@ -5,11 +5,6 @@ from dataclasses import dataclass
|
||||
from typing import Any, override
|
||||
|
||||
from aiomelcloudhome import ATAUnit, ATWUnit, MELCloudHome
|
||||
from aiomelcloudhome.exceptions import (
|
||||
MelCloudHomeAuthenticationError,
|
||||
MelCloudHomeConnectionError,
|
||||
MelCloudHomeTimeoutError,
|
||||
)
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
SwitchDeviceClass,
|
||||
@@ -18,11 +13,9 @@ from homeassistant.components.switch import (
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import async_setup_unit_entities, unit_ids
|
||||
from .const import DOMAIN
|
||||
from .common import async_setup_unit_entities, perform_action, unit_ids
|
||||
from .coordinator import MelCloudHomeConfigEntry, MelCloudHomeCoordinator
|
||||
from .entity import MelCloudHomeATAUnitEntity, MelCloudHomeATWUnitEntity
|
||||
|
||||
@@ -109,32 +102,6 @@ ATW_SWITCHES: tuple[MelCloudHomeSwitchEntityDescription[ATWUnit], ...] = (
|
||||
)
|
||||
|
||||
|
||||
async def _perform_action(
|
||||
coordinator: MelCloudHomeCoordinator,
|
||||
coroutine: Coroutine[Any, Any, None],
|
||||
) -> None:
|
||||
"""Perform a MELCloud Home action with error handling and coordinator refresh."""
|
||||
try:
|
||||
await coroutine
|
||||
except MelCloudHomeAuthenticationError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_auth",
|
||||
) from err
|
||||
except MelCloudHomeConnectionError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="cannot_connect",
|
||||
) from err
|
||||
except MelCloudHomeTimeoutError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="timeout_connect",
|
||||
) from err
|
||||
else:
|
||||
await coordinator.async_request_refresh()
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MelCloudHomeConfigEntry,
|
||||
@@ -189,7 +156,7 @@ class ATASwitch(MelCloudHomeATAUnitEntity, SwitchEntity):
|
||||
@override
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Enable the protection."""
|
||||
await _perform_action(
|
||||
await perform_action(
|
||||
self.coordinator,
|
||||
self.entity_description.turn_on_fn(self.coordinator.client, self.unit),
|
||||
)
|
||||
@@ -197,7 +164,7 @@ class ATASwitch(MelCloudHomeATAUnitEntity, SwitchEntity):
|
||||
@override
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Disable the protection."""
|
||||
await _perform_action(
|
||||
await perform_action(
|
||||
self.coordinator,
|
||||
self.entity_description.turn_off_fn(self.coordinator.client, self.unit),
|
||||
)
|
||||
@@ -234,7 +201,7 @@ class ATWSwitch(MelCloudHomeATWUnitEntity, SwitchEntity):
|
||||
@override
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Enable the protection."""
|
||||
await _perform_action(
|
||||
await perform_action(
|
||||
self.coordinator,
|
||||
self.entity_description.turn_on_fn(self.coordinator.client, self.unit),
|
||||
)
|
||||
@@ -242,7 +209,7 @@ class ATWSwitch(MelCloudHomeATWUnitEntity, SwitchEntity):
|
||||
@override
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Disable the protection."""
|
||||
await _perform_action(
|
||||
await perform_action(
|
||||
self.coordinator,
|
||||
self.entity_description.turn_off_fn(self.coordinator.client, self.unit),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user