mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Use rpc_call
decorator in the Shelly entity module (#143484)
This commit is contained in:
parent
896da4abbd
commit
db0cbf1ea9
@ -315,6 +315,41 @@ class RestEntityDescription(EntityDescription):
|
|||||||
value: Callable[[dict, Any], Any] | None = None
|
value: Callable[[dict, Any], Any] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def rpc_call[_T: ShellyRpcEntity, **_P](
|
||||||
|
func: Callable[Concatenate[_T, _P], Awaitable[None]],
|
||||||
|
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
|
||||||
|
"""Catch rpc_call exceptions."""
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
async def cmd_wrapper(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
||||||
|
"""Wrap all command methods."""
|
||||||
|
try:
|
||||||
|
await func(self, *args, **kwargs)
|
||||||
|
except DeviceConnectionError as err:
|
||||||
|
self.coordinator.last_update_success = False
|
||||||
|
raise HomeAssistantError(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="device_communication_action_error",
|
||||||
|
translation_placeholders={
|
||||||
|
"entity": self.entity_id,
|
||||||
|
"device": self.coordinator.name,
|
||||||
|
},
|
||||||
|
) from err
|
||||||
|
except RpcCallError as err:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="rpc_call_action_error",
|
||||||
|
translation_placeholders={
|
||||||
|
"entity": self.entity_id,
|
||||||
|
"device": self.coordinator.name,
|
||||||
|
},
|
||||||
|
) from err
|
||||||
|
except InvalidAuthError:
|
||||||
|
await self.coordinator.async_shutdown_device_and_start_reauth()
|
||||||
|
|
||||||
|
return cmd_wrapper
|
||||||
|
|
||||||
|
|
||||||
class ShellyBlockEntity(CoordinatorEntity[ShellyBlockCoordinator]):
|
class ShellyBlockEntity(CoordinatorEntity[ShellyBlockCoordinator]):
|
||||||
"""Helper class to represent a block entity."""
|
"""Helper class to represent a block entity."""
|
||||||
|
|
||||||
@ -393,6 +428,7 @@ class ShellyRpcEntity(CoordinatorEntity[ShellyRpcCoordinator]):
|
|||||||
"""Handle device update."""
|
"""Handle device update."""
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@rpc_call
|
||||||
async def call_rpc(
|
async def call_rpc(
|
||||||
self, method: str, params: Any, timeout: float | None = None
|
self, method: str, params: Any, timeout: float | None = None
|
||||||
) -> Any:
|
) -> Any:
|
||||||
@ -404,31 +440,9 @@ class ShellyRpcEntity(CoordinatorEntity[ShellyRpcCoordinator]):
|
|||||||
params,
|
params,
|
||||||
timeout,
|
timeout,
|
||||||
)
|
)
|
||||||
try:
|
if timeout:
|
||||||
if timeout:
|
return await self.coordinator.device.call_rpc(method, params, timeout)
|
||||||
return await self.coordinator.device.call_rpc(method, params, timeout)
|
return await self.coordinator.device.call_rpc(method, params)
|
||||||
return await self.coordinator.device.call_rpc(method, params)
|
|
||||||
except DeviceConnectionError as err:
|
|
||||||
self.coordinator.last_update_success = False
|
|
||||||
raise HomeAssistantError(
|
|
||||||
translation_domain=DOMAIN,
|
|
||||||
translation_key="device_communication_action_error",
|
|
||||||
translation_placeholders={
|
|
||||||
"entity": self.entity_id,
|
|
||||||
"device": self.coordinator.name,
|
|
||||||
},
|
|
||||||
) from err
|
|
||||||
except RpcCallError as err:
|
|
||||||
raise HomeAssistantError(
|
|
||||||
translation_domain=DOMAIN,
|
|
||||||
translation_key="rpc_call_action_error",
|
|
||||||
translation_placeholders={
|
|
||||||
"entity": self.entity_id,
|
|
||||||
"device": self.coordinator.name,
|
|
||||||
},
|
|
||||||
) from err
|
|
||||||
except InvalidAuthError:
|
|
||||||
await self.coordinator.async_shutdown_device_and_start_reauth()
|
|
||||||
|
|
||||||
|
|
||||||
class ShellyBlockAttributeEntity(ShellyBlockEntity, Entity):
|
class ShellyBlockAttributeEntity(ShellyBlockEntity, Entity):
|
||||||
@ -708,38 +722,3 @@ def get_entity_class(
|
|||||||
return description.entity_class
|
return description.entity_class
|
||||||
|
|
||||||
return sensor_class
|
return sensor_class
|
||||||
|
|
||||||
|
|
||||||
def rpc_call[_T: ShellyRpcEntity, **_P](
|
|
||||||
func: Callable[Concatenate[_T, _P], Awaitable[None]],
|
|
||||||
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
|
|
||||||
"""Catch rpc_call exceptions."""
|
|
||||||
|
|
||||||
@wraps(func)
|
|
||||||
async def cmd_wrapper(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
|
||||||
"""Wrap all command methods."""
|
|
||||||
try:
|
|
||||||
await func(self, *args, **kwargs)
|
|
||||||
except DeviceConnectionError as err:
|
|
||||||
self.coordinator.last_update_success = False
|
|
||||||
raise HomeAssistantError(
|
|
||||||
translation_domain=DOMAIN,
|
|
||||||
translation_key="device_communication_action_error",
|
|
||||||
translation_placeholders={
|
|
||||||
"entity": self.entity_id,
|
|
||||||
"device": self.coordinator.name,
|
|
||||||
},
|
|
||||||
) from err
|
|
||||||
except RpcCallError as err:
|
|
||||||
raise HomeAssistantError(
|
|
||||||
translation_domain=DOMAIN,
|
|
||||||
translation_key="rpc_call_action_error",
|
|
||||||
translation_placeholders={
|
|
||||||
"entity": self.entity_id,
|
|
||||||
"device": self.coordinator.name,
|
|
||||||
},
|
|
||||||
) from err
|
|
||||||
except InvalidAuthError:
|
|
||||||
await self.coordinator.async_shutdown_device_and_start_reauth()
|
|
||||||
|
|
||||||
return cmd_wrapper
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user