Skip to content

Commit

Permalink
add support for Ohmpilot system scope request
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Nov 25, 2021
1 parent b466331 commit ed957a2
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def main(loop, host):
power_flow=True,
system_meter=True,
system_inverter=True,
system_ohmpilot=True,
system_storage=True,
device_meter=["0"],
# storage is not necessarily supported by every fronius device
Expand Down
63 changes: 62 additions & 1 deletion pyfronius/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import aiohttp

from .const import INVERTER_DEVICE_TYPE
from .const import INVERTER_DEVICE_TYPE, OHMPILOT_STATE_CODES

_LOGGER = logging.getLogger(__name__)
DEGREE_CELSIUS = "°C"
Expand Down Expand Up @@ -51,6 +51,7 @@ class API_VERSION(enum.Enum):
API_VERSION.V1: "GetInverterRealtimeData.cgi?Scope=System",
}
URL_SYSTEM_LED = {API_VERSION.V1: "GetLoggerLEDInfo.cgi"}
URL_SYSTEM_OHMPILOT = {API_VERSION.V1: "GetOhmPilotRealtimeData.cgi?Scope=System"}
URL_SYSTEM_STORAGE = {
API_VERSION.V1: "GetStorageRealtimeData.cgi?Scope=System"
}
Expand Down Expand Up @@ -265,6 +266,7 @@ async def fetch(
power_flow=True,
system_meter=True,
system_inverter=True,
system_ohmpilot=True,
system_storage=True,
device_meter=frozenset(["0"]),
# storage is not necessarily supported by every fronius device
Expand All @@ -285,6 +287,8 @@ async def fetch(
requests.append(self.current_system_meter_data())
if system_inverter:
requests.append(self.current_system_inverter_data())
if system_ohmpilot:
requests.append(self.current_system_ohmpilot_data())
if system_storage:
requests.append(self.current_system_storage_data())
for i in device_meter:
Expand Down Expand Up @@ -392,6 +396,16 @@ async def current_system_inverter_data(self):
"current system inverter",
)

async def current_system_ohmpilot_data(self):
"""
Get the current ohmpilot data.
"""
return await self._current_data(
Fronius._system_ohmpilot_data,
URL_SYSTEM_OHMPILOT,
"current system ohmpilot",
)

async def current_meter_data(self, device: str = "0") -> Dict[str, Any]:
"""
Get the current meter data for a device.
Expand Down Expand Up @@ -597,6 +611,53 @@ def _system_inverter_data(data):

return sensor

@staticmethod
def _device_ohmpilot_data(data):
_LOGGER.debug("Converting ohmpilot data from '{}'".format(data))
device = {}

if "CodeOfError" in data:
device["error_code"] = {"value": data["CodeOfError"]}

if "CodeOfState" in data:
state_code = data["CodeOfState"]
device["state_code"] = {"value": state_code}
device["state_message"] = {
"value": OHMPILOT_STATE_CODES.get(state_code, "Unknown")
}

if "Details" in data:
device["hardware"] = {"value": data["Details"]["Hardware"]}
device["manufacturer"] = {"value": data["Details"]["Manufacturer"]}
device["model"] = {"value": data["Details"]["Model"]}
device["serial"] = {"value": data["Details"]["Serial"]}
device["software"] = {"value": data["Details"]["Software"]}

if "EnergyReal_WAC_Sum_Consumed" in data:
device["energy_real_ac_consumed"] = {
"value": data["EnergyReal_WAC_Sum_Consumed"], "unit": WATT_HOUR
}

if "PowerReal_PAC_Sum" in data:
device["power_real_ac"] = {"value": data["PowerReal_PAC_Sum"], "unit": WATT}

if "Temperature_Channel_1" in data:
device["temperature_channel_1"] = {
"value": data["Temperature_Channel_1"], "unit": DEGREE_CELSIUS
}

return device

@staticmethod
def _system_ohmpilot_data(data):
_LOGGER.debug("Converting system ohmpilot data: '{}'".format(data))
sensor = {"ohmpilots": {}}

for device_id, device_data in data.items():
sensor["ohmpilots"][device_id] = Fronius._device_ohmpilot_data(device_data)

return sensor

@staticmethod
def _device_meter_data(data):
_LOGGER.debug("Converting meter data: '{}'".format(data))
Expand Down
9 changes: 9 additions & 0 deletions pyfronius/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,12 @@
253: {"manufacturer": "Fronius", "model": "IG 20"},
254: {"manufacturer": "Fronius", "model": "IG 15"},
}

OHMPILOT_STATE_CODES = {
0: "Up and running",
1: "Keep minimum temperature",
2: "Legionella protection",
3: "Critical fault",
4: "Fault",
5: "Boost mode",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Body": {
"Data": {
"0": {
"CodeOfError": 926,
"CodeOfState": 0,
"Details": {
"Hardware": "3",
"Manufacturer": "Fronius",
"Model": "Ohmpilot",
"Serial": "28136344",
"Software": "1.0.19-1"
},
"EnergyReal_WAC_Sum_Consumed": 2964307,
"PowerReal_PAC_Sum": 0,
"Temperature_Channel_1": 23.9
}
}
},
"Head": {
"RequestArguments": {
"DeviceClass": "OhmPilot",
"Scope": "System"
},
"Status": {
"Code": 0,
"Reason": "",
"UserMessage": ""
},
"Timestamp": "2019-06-24T10:10:44+02:00"
}
}
10 changes: 10 additions & 0 deletions pyfronius/tests/test_web_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
GET_INVERTER_REALTIME_DATA_SCOPE_DEVICE,
GET_METER_REALTIME_DATA_SYSTEM,
GET_LOGGER_LED_INFO_STATE,
GET_OHMPILOT_REALTIME_DATA_SYSTEM,
GET_POWER_FLOW_REALTIME_DATA,
GET_LOGGER_INFO,
GET_INVERTER_INFO,
Expand Down Expand Up @@ -211,6 +212,12 @@ def test_fronius_get_inverter_realtime_data_system(self):
)
self.assertDictEqual(res, GET_INVERTER_REALTIME_DATA_SYSTEM)

def test_fronius_get_ohmpilot_realtime_data_system(self):
res = asyncio.get_event_loop().run_until_complete(
self.fronius.current_system_ohmpilot_data()
)
self.assertDictEqual(res, GET_OHMPILOT_REALTIME_DATA_SYSTEM)

def test_fronius_get_led_info_data(self):
res = asyncio.get_event_loop().run_until_complete(
self.fronius.current_led_data()
Expand Down Expand Up @@ -248,9 +255,11 @@ def test_fronius_fetch(self):
self.fronius.fetch(
active_device_info=True,
inverter_info=True,
logger_info=True,
power_flow=True,
system_meter=True,
system_inverter=True,
system_ohmpilot=True,
system_storage=False,
device_meter={0},
device_storage={0},
Expand All @@ -266,6 +275,7 @@ def test_fronius_fetch(self):
GET_POWER_FLOW_REALTIME_DATA,
GET_METER_REALTIME_DATA_SYSTEM,
GET_INVERTER_REALTIME_DATA_SYSTEM,
GET_OHMPILOT_REALTIME_DATA_SYSTEM,
GET_METER_REALTIME_DATA_SCOPE_DEVICE,
GET_STORAGE_REALTIME_DATA_SCOPE_DEVICE,
GET_INVERTER_REALTIME_DATA_SCOPE_DEVICE,
Expand Down
20 changes: 20 additions & 0 deletions pyfronius/tests/web_raw/v1/web_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@
},
}

GET_OHMPILOT_REALTIME_DATA_SYSTEM = {
"timestamp": {"value": "2019-06-24T10:10:44+02:00"},
"status": {"Code": 0, "Reason": "", "UserMessage": ""},
"ohmpilots": {
"0": {
"error_code": {"value": 926},
"state_code": {"value": 0},
"state_message": {"value": "Up and running"},
"hardware": {"value": "3"},
"manufacturer": {"value": "Fronius"},
"model": {"value": "Ohmpilot"},
"serial": {"value": "28136344"},
"software": {"value": "1.0.19-1"},
"energy_real_ac_consumed": {"value": 2964307, "unit": "Wh"},
"power_real_ac": {"value": 0, "unit": "W"},
"temperature_channel_1": {"value": 23.9, "unit": "°C"},
}
},
}

GET_LOGGER_LED_INFO_STATE = {
"timestamp": {"value": "2019-06-23T23:50:16+02:00"},
"status": {"Code": 0, "Reason": "", "UserMessage": ""},
Expand Down

0 comments on commit ed957a2

Please sign in to comment.