Skip to content

Commit

Permalink
Fix active power control services
Browse files Browse the repository at this point in the history
  • Loading branch information
wlcrs committed Oct 29, 2022
1 parent b259351 commit 70fcb34
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/huawei_solar",
"issue_tracker": "https://github.com/wlcrs/huawei_solar/issues",
"requirements": ["huawei-solar==2.1.4"],
"requirements": ["huawei-solar==2.1.5"],
"codeowners": ["@wlcrs"],
"iot_class": "local_polling",
"version": "1.1.4",
Expand Down
46 changes: 34 additions & 12 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
from huawei_solar import register_names as rn
from huawei_solar import register_values as rv

from .const import (CONF_ENABLE_PARAMETER_CONFIGURATION, DOMAIN,
SERVICE_FORCIBLE_CHARGE, SERVICE_FORCIBLE_CHARGE_SOC,
SERVICE_FORCIBLE_DISCHARGE, SERVICE_FORCIBLE_DISCHARGE_SOC,
SERVICE_RESET_MAXIMUM_FEED_GRID_POWER,
SERVICE_SET_MAXIMUM_FEED_GRID_POWER,
SERVICE_SET_MAXIMUM_FEED_GRID_POWER_PERCENT,
SERVICE_STOP_FORCIBLE_CHARGE)
from .const import (
CONF_ENABLE_PARAMETER_CONFIGURATION,
DOMAIN,
SERVICE_FORCIBLE_CHARGE,
SERVICE_FORCIBLE_CHARGE_SOC,
SERVICE_FORCIBLE_DISCHARGE,
SERVICE_FORCIBLE_DISCHARGE_SOC,
SERVICE_RESET_MAXIMUM_FEED_GRID_POWER,
SERVICE_SET_MAXIMUM_FEED_GRID_POWER,
SERVICE_SET_MAXIMUM_FEED_GRID_POWER_PERCENT,
SERVICE_STOP_FORCIBLE_CHARGE,
)

if TYPE_CHECKING:
from . import HuaweiInverterBridgeDeviceInfos
Expand Down Expand Up @@ -110,6 +115,23 @@ def get_battery_bridge(service_call: ServiceCall):
"Not a valid 'Connected Energy Storage' device"
)

def get_inverter_bridge(service_call: ServiceCall):
dev_reg = device_registry.async_get(hass)
device_entry = dev_reg.async_get(service_call.data[DATA_DEVICE_ID])

if not device_entry:
raise HuaweiSolarServiceException("No such device found")

for bridge, device_infos in bridges_with_device_infos:

for identifier in device_infos["inverter"]["identifiers"]:
for device_identifier in device_entry.identifiers:
if identifier == device_identifier:
return bridge

_LOGGER.error("The provided device is not an inverter")
raise HuaweiSolarServiceException("Not a valid 'Inverter' device")

async def _validate_power_value(
power: Any, bridge: HuaweiSolarBridge, max_value_key
):
Expand Down Expand Up @@ -222,7 +244,7 @@ async def stop_forcible_charge(service_call: ServiceCall) -> None:
async def reset_maximum_feed_grid_power(service_call: ServiceCall) -> None:
"""Sets Active Power Control to 'Power-limited grid connection' with the given wattage."""

bridge = get_battery_bridge(service_call)
bridge = get_inverter_bridge(service_call)
await bridge.set(
rn.ACTIVE_POWER_CONTROL_MODE,
rv.ActivePowerControlMode.UNLIMITED,
Expand All @@ -236,7 +258,7 @@ async def reset_maximum_feed_grid_power(service_call: ServiceCall) -> None:
async def set_maximum_feed_grid_power(service_call: ServiceCall) -> None:
"""Sets Active Power Control to 'Power-limited grid connection' with the given wattage."""

bridge = get_battery_bridge(service_call)
bridge = get_inverter_bridge(service_call)
power = await _validate_power_value(
service_call.data[DATA_POWER], bridge, rn.P_MAX
)
Expand All @@ -250,7 +272,7 @@ async def set_maximum_feed_grid_power(service_call: ServiceCall) -> None:
async def set_maximum_feed_grid_power_percentage(service_call: ServiceCall) -> None:
"""Sets Active Power Control to 'Power-limited grid connection' with the given percentage."""

bridge = get_battery_bridge(service_call)
bridge = get_inverter_bridge(service_call)
power_percentage = service_call.data[DATA_POWER_PERCENTAGE]

await bridge.set(rn.MAXIMUM_FEED_GRID_POWER_PERCENT, power_percentage)
Expand Down Expand Up @@ -296,12 +318,12 @@ async def set_maximum_feed_grid_power_percentage(service_call: ServiceCall) -> N
DOMAIN,
SERVICE_SET_MAXIMUM_FEED_GRID_POWER,
set_maximum_feed_grid_power,
schema=DEVICE_SCHEMA,
schema=MAXIMUM_FEED_GRID_POWER_SCHEMA,
)

hass.services.async_register(
DOMAIN,
SERVICE_SET_MAXIMUM_FEED_GRID_POWER_PERCENT,
set_maximum_feed_grid_power_percentage,
schema=DEVICE_SCHEMA,
schema=MAXIMUM_FEED_GRID_POWER_PERCENTAGE_SCHEMA,
)
24 changes: 12 additions & 12 deletions services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ forcible_charge:
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
description: Must be a 'Battery' device
required: true
selector:
device:
Expand Down Expand Up @@ -34,7 +34,7 @@ forcible_discharge:
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
description: Must be a 'Battery' device
required: true
selector:
device:
Expand Down Expand Up @@ -64,7 +64,7 @@ forcible_charge_soc:
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
description: Must be a 'Battery' device
required: true
selector:
device:
Expand Down Expand Up @@ -93,7 +93,7 @@ forcible_discharge_soc:
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
description: Must be a 'Battery' device
required: true
selector:
device:
Expand Down Expand Up @@ -122,7 +122,7 @@ stop_forcible_charge:
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
description: Must be a 'Battery' device
required: true
selector:
device:
Expand All @@ -133,8 +133,8 @@ reset_maximum_feed_grid_power:
description: Set Active Power Control to the default Unlimited mode
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
name: Inverter
description: Must be an 'Inverter' device
required: true
selector:
device:
Expand All @@ -145,8 +145,8 @@ set_maximum_feed_grid_power:
description: Sets Active Power Control to 'Power-limited grid connection' with the given wattage
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
name: Inverter
description: Must be an 'Inverter' device
required: true
selector:
device:
Expand All @@ -160,12 +160,12 @@ set_maximum_feed_grid_power:
text:

set_maximum_feed_grid_power_percent:
name: Limit the power fed to the grid
name: Limit the power fed to the grid to percentage
description: Sets Active Power Control to 'Power-limited grid connection (%)' with the given percentage
fields:
device_id:
name: Battery
description: Must be a 'Connected Energy Storage' device
name: Inverter
description: Must be an 'Inverter' device
required: true
selector:
device:
Expand Down

0 comments on commit 70fcb34

Please sign in to comment.