From 467ba322582c20c44e9971a8142e62919fd48d1a Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Thu, 25 Apr 2024 15:35:44 +0200 Subject: [PATCH] Run precommit on sources --- .github/workflows/python-publish.yml | 2 +- README.md | 4 ++-- pymyenergi/base_device.py | 8 ++++---- pymyenergi/cli.py | 10 ++++++---- pymyenergi/client.py | 19 +++++++++++++------ pymyenergi/connection.py | 19 ++++++++++++------- pymyenergi/eddi.py | 2 +- pymyenergi/zappi.py | 1 + setup.py | 4 ++-- tests/fixtures/libbi.json | 2 +- tests/test_client.py | 4 ++-- 11 files changed, 45 insertions(+), 30 deletions(-) diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 8f0a51d..87a4fc7 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -12,7 +12,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: '3.x' + python-version: "3.x" - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/README.md b/README.md index f4ca8d2..f68e95e 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ loop.run_until_complete(get_data()) ``` ## Libbi support + Currently supported features: - Reads a few values such as State of Charge, DCPV CT @@ -160,6 +161,7 @@ Currently supported features: - Set charge target (in Wh) cli examples: + ``` myenergi libbi show myenergi libbi mode normal @@ -169,8 +171,6 @@ myenergi libbi chargefromgrid false myenergi libbi chargetarget 10200 ``` - - ## Credits [twonk](https://github.com/twonk/MyEnergi-App-Api) for documenting the unofficial API diff --git a/pymyenergi/base_device.py b/pymyenergi/base_device.py index 0113846..0ad2e37 100644 --- a/pymyenergi/base_device.py +++ b/pymyenergi/base_device.py @@ -134,7 +134,7 @@ async def fetch_history_data( "ivi1": 0, "bdp1": 0, "bcp1": 0, - "pvp1": 0 + "pvp1": 0, } if resolution == MINUTE: url = f"/cgi-jday-{self.prefix}{self._serialno}-{date_from.year}-{date_from.month}-{date_from.day}-{date_from.hour}-0-{how_long}" @@ -169,11 +169,11 @@ async def fetch_history_data( "grid_export": round(energy_wh["exp"] / 1000, 2), "battery_charge": round(energy_wh["bcp1"] / 1000, 2), "battery_discharge": round(energy_wh["bdp1"] / 1000, 2), - "inverter_export": round(energy_wh["ive1"] /1000, 2), - "inverter_import": round(energy_wh["ivi1"] /1000, 2), + "inverter_export": round(energy_wh["ive1"] / 1000, 2), + "inverter_import": round(energy_wh["ivi1"] / 1000, 2), "device_boosted": device_boosted, "device_green": device_green, - "device_total": device_boosted + device_green + "device_total": device_boosted + device_green, } if resolution == MINUTE: return_data["pv_total"] = round(energy_wh["pvp1"] / 1000, 2) diff --git a/pymyenergi/cli.py b/pymyenergi/cli.py index deec9e0..50a3312 100644 --- a/pymyenergi/cli.py +++ b/pymyenergi/cli.py @@ -13,13 +13,13 @@ from pymyenergi.eddi import BOOST_TARGETS from pymyenergi.eddi import EDDI_MODES from pymyenergi.exceptions import WrongCredentials -from pymyenergi.zappi import CHARGE_MODES from pymyenergi.libbi import LIBBI_MODES +from pymyenergi.zappi import CHARGE_MODES from . import EDDI from . import HARVI -from . import ZAPPI from . import LIBBI +from . import ZAPPI logging.basicConfig() logging.root.setLevel(logging.WARNING) @@ -28,11 +28,13 @@ async def main(args): username = args.username or input("Please enter your hub serial number: ") password = args.password or getpass(prompt="Password (apikey): ") - app_email = args.app_email or input("App email (enter to skip; only needed for libbi): ") + app_email = args.app_email or input( + "App email (enter to skip; only needed for libbi): " + ) if app_email: app_password = args.app_password or getpass(prompt="App password: ") else: - app_password = '' + app_password = "" conn = Connection(username, password, app_password, app_email) if app_email and app_password: await conn.discoverLocations() diff --git a/pymyenergi/client.py b/pymyenergi/client.py index 0211b3f..5a9d6b0 100644 --- a/pymyenergi/client.py +++ b/pymyenergi/client.py @@ -12,8 +12,8 @@ from . import EDDI from . import FREQUENCY_GRID from . import HARVI -from . import LIBBI from . import HOUR +from . import LIBBI from . import VOLTAGE_GRID from . import ZAPPI from .eddi import Eddi @@ -83,7 +83,9 @@ def _calculate_history_totals(self): if zappi_or_eddi_or_libbi is not None: for key in energy_keys: if self._history_totals[key] == 0: - self._history_totals[key] = zappi_or_eddi_or_libbi.history_data.get(key, 0) + self._history_totals[key] = zappi_or_eddi_or_libbi.history_data.get( + key, 0 + ) self._history_totals["green"] = self._history_totals.get( "green", 0 ) + zappi_or_eddi_or_libbi.history_data.get("device_green", 0) @@ -133,7 +135,10 @@ def _calculate_totals(self): self._totals[VOLTAGE_GRID] = zappi_or_eddi_or_libbi.supply_voltage if self._totals.get(CT_GRID, 0) == 0 and zappi_or_eddi_or_libbi is not None: self._totals[CT_GRID] = zappi_or_eddi_or_libbi.power_grid - if self._totals.get(CT_GENERATION, 0) == 0 and zappi_or_eddi_or_libbi is not None: + if ( + self._totals.get(CT_GENERATION, 0) == 0 + and zappi_or_eddi_or_libbi is not None + ): self._totals[CT_GENERATION] = zappi_or_eddi_or_libbi.power_generated def get_power_totals(self): @@ -214,8 +219,8 @@ async def refresh(self): for grp in self._data: keys = list(grp.keys()) key = keys[0] - if(len(keys) > 1 and keys[1] == 'fwv'): - self._firmware_version = grp['fwv'] + if len(keys) > 1 and keys[1] == "fwv": + self._firmware_version = grp["fwv"] if key not in DEVICE_TYPES: if key == "fwv": self._firmware_version = grp[key] @@ -225,7 +230,9 @@ async def refresh(self): devices = grp[key] for device_data in devices: serial = device_data.get("sno") - self._update_available = device_data.get('newBootloaderAvailable', False) + self._update_available = device_data.get( + "newBootloaderAvailable", False + ) existing_device = self.devices.get(serial, None) if existing_device is None: existing_device = device_factory( diff --git a/pymyenergi/connection.py b/pymyenergi/connection.py index 9bac9d7..a1dc84f 100644 --- a/pymyenergi/connection.py +++ b/pymyenergi/connection.py @@ -8,7 +8,6 @@ from typing import Text import httpx - from pycognito import Cognito from .exceptions import MyenergiException @@ -16,14 +15,20 @@ from .exceptions import WrongCredentials _LOGGER = logging.getLogger(__name__) -_USER_POOL_ID = 'eu-west-2_E57cCJB20' -_CLIENT_ID = '2fup0dhufn5vurmprjkj599041' +_USER_POOL_ID = "eu-west-2_E57cCJB20" +_CLIENT_ID = "2fup0dhufn5vurmprjkj599041" + class Connection: """Connection to myenergi API.""" def __init__( - self, username: Text = None, password: Text = None, app_password: Text = None, app_email: Text = None, timeout: int = 20 + self, + username: Text = None, + password: Text = None, + app_password: Text = None, + app_email: Text = None, + timeout: int = 20, ) -> None: """Initialize connection object.""" self.timeout = timeout @@ -41,7 +46,7 @@ def __init__( self.oauth.authenticate(password=self.app_password) self.oauth_headers = {"Authorization": f"Bearer {self.oauth.access_token}"} self.do_query_asn = True - self.invitation_id = '' + self.invitation_id = "" _LOGGER.debug("New connection created") def _checkMyenergiServerURL(self, responseHeader): @@ -79,8 +84,8 @@ async def send(self, method, url, json=None, oauth=False): ) as httpclient: theUrl = self.oauth_base_url + url # if we have an invitiation id, we need to add that to the query - if (self.invitation_id != ""): - if ("?" in theUrl): + if self.invitation_id != "": + if "?" in theUrl: theUrl = theUrl + "&invitationId=" + self.invitation_id else: theUrl = theUrl + "?invitationId=" + self.invitation_id diff --git a/pymyenergi/eddi.py b/pymyenergi/eddi.py index 602c536..6b67d8f 100644 --- a/pymyenergi/eddi.py +++ b/pymyenergi/eddi.py @@ -55,7 +55,7 @@ def ct_keys(self): @property def hsk(self): - """ Heatsink temperature """ + """Heatsink temperature""" val = self._data.get("hsk", None) if val is not None: val = val / 10 diff --git a/pymyenergi/zappi.py b/pymyenergi/zappi.py index 69acadb..c8ced08 100644 --- a/pymyenergi/zappi.py +++ b/pymyenergi/zappi.py @@ -16,6 +16,7 @@ } SINGLE_PHASE = "SINGLE_PHASE" + class Zappi(BaseDevice): """Zappi Client for myenergi API.""" diff --git a/setup.py b/setup.py index 64ae9b7..156d0e2 100755 --- a/setup.py +++ b/setup.py @@ -12,8 +12,8 @@ author="Johan Isaksson", author_email="johan@generatorhallen.se", description="Python library and CLI for communicating with myenergi API.", - long_description=open("README.md", 'r').read(), - long_description_content_type='text/markdown', + long_description=open("README.md", "r").read(), + long_description_content_type="text/markdown", package_data={"pymyenergi": ["VERSION"]}, include_package_data=True, url="https://github.com/cjne/pymyenergi", diff --git a/tests/fixtures/libbi.json b/tests/fixtures/libbi.json index 6b7d9a5..05eca1e 100644 --- a/tests/fixtures/libbi.json +++ b/tests/fixtures/libbi.json @@ -38,4 +38,4 @@ "fwv": "3702S5.041", "newAppAvailable": "False", "newBootloaderAvailable": "False" -} \ No newline at end of file +} diff --git a/tests/test_client.py b/tests/test_client.py index f70adb7..68fb8b6 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -2,8 +2,8 @@ from pymyenergi.client import MyenergiClient from pymyenergi.eddi import Eddi from pymyenergi.harvi import Harvi -from pymyenergi.zappi import Zappi from pymyenergi.libbi import Libbi +from pymyenergi.zappi import Zappi # All test coroutines will be treated as marked. pytestmark = pytest.mark.asyncio @@ -69,4 +69,4 @@ async def test_get_libbi_devices(client_fetch_data_fixture): client = MyenergiClient(conn) devices = await client.get_devices("libbi") assert len(devices) == 1 - assert isinstance(devices[0], Libbi) \ No newline at end of file + assert isinstance(devices[0], Libbi)