|
10 | 10 | import typing |
11 | 11 | from datetime import timedelta |
12 | 12 |
|
13 | | -import grpc |
14 | 13 | from frequenz.channels import Receiver, Sender |
15 | | -from frequenz.client.microgrid import BatteryData, ComponentCategory, InverterData |
| 14 | +from frequenz.client.microgrid import ( |
| 15 | + BatteryData, |
| 16 | + ClientError, |
| 17 | + ComponentCategory, |
| 18 | + InverterData, |
| 19 | + OperationOutOfRange, |
| 20 | +) |
16 | 21 | from typing_extensions import override |
17 | 22 |
|
18 | 23 | from .... import microgrid |
@@ -647,39 +652,38 @@ def _parse_result( |
647 | 652 |
|
648 | 653 | for inverter_id, aws in tasks.items(): |
649 | 654 | battery_ids = self._inv_bats_map[inverter_id] |
| 655 | + failed = True |
650 | 656 | try: |
651 | 657 | aws.result() |
652 | | - except grpc.aio.AioRpcError as err: |
653 | | - failed_power += distribution[inverter_id] |
654 | | - failed_batteries = failed_batteries.union(battery_ids) |
655 | | - if err.code() == grpc.StatusCode.OUT_OF_RANGE: |
656 | | - _logger.debug( |
657 | | - "Set power for battery %s failed, error %s", |
658 | | - battery_ids, |
659 | | - str(err), |
660 | | - ) |
661 | | - else: |
662 | | - _logger.warning( |
663 | | - "Set power for battery %s failed, error %s. Mark it as broken.", |
664 | | - battery_ids, |
665 | | - str(err), |
666 | | - ) |
| 658 | + failed = False |
| 659 | + except OperationOutOfRange as err: |
| 660 | + _logger.debug( |
| 661 | + "Set power for battery %s failed due to out of range error: %s", |
| 662 | + battery_ids, |
| 663 | + err, |
| 664 | + ) |
| 665 | + except ClientError as err: |
| 666 | + _logger.warning( |
| 667 | + "Set power for battery %s failed, mark it as broken. Error: %s", |
| 668 | + battery_ids, |
| 669 | + err, |
| 670 | + ) |
667 | 671 | except asyncio.exceptions.CancelledError: |
668 | | - failed_power += distribution[inverter_id] |
669 | | - failed_batteries = failed_batteries.union(battery_ids) |
670 | 672 | _logger.warning( |
671 | 673 | "Battery %s didn't respond in %f sec. Mark it as broken.", |
672 | 674 | battery_ids, |
673 | 675 | request_timeout.total_seconds(), |
674 | 676 | ) |
675 | 677 | except Exception: # pylint: disable=broad-except |
676 | | - failed_power += distribution[inverter_id] |
677 | | - failed_batteries = failed_batteries.union(battery_ids) |
678 | 678 | _logger.exception( |
679 | 679 | "Unknown error while setting power to batteries: %s", |
680 | 680 | battery_ids, |
681 | 681 | ) |
682 | 682 |
|
| 683 | + if failed: |
| 684 | + failed_power += distribution[inverter_id] |
| 685 | + failed_batteries.update(battery_ids) |
| 686 | + |
683 | 687 | return failed_power, failed_batteries |
684 | 688 |
|
685 | 689 | async def _cancel_tasks( |
|
0 commit comments