Skip to content

Commit

Permalink
Cleanup PR 57
Browse files Browse the repository at this point in the history
- Add Root PM Constant
- Add translation keys to en.json
- Remove duplicate string ID
- Simplify connect code
- Fix Ruff errors
  • Loading branch information
torbennehmer committed Oct 13, 2023
1 parent 5193bf5 commit 46d7162
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
1 change: 1 addition & 0 deletions custom_components/e3dc_rscp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
DOMAIN = "e3dc_rscp"
ERROR_AUTH_INVALID = "invalid_auth"
ERROR_CANNOT_CONNECT = "cannot_connect"
POWERMETER_ID_ROOT = 0
SERVICE_CLEAR_POWER_LIMITS = "clear_power_limits"
SERVICE_SET_POWER_LIMITS = "set_power_limits"
SERVICE_MANUAL_CHARGE = "manual_charge"
Expand Down
42 changes: 20 additions & 22 deletions custom_components/e3dc_rscp/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
DataUpdateCoordinator,
)

from .const import CONF_RSCPKEY, DOMAIN
from .const import CONF_RSCPKEY, DOMAIN, POWERMETER_ID_ROOT

_LOGGER = logging.getLogger(__name__)
_STAT_REFRESH_INTERVAL = 60
Expand Down Expand Up @@ -90,11 +90,10 @@ async def async_connect(self):
await self._load_timezone_settings()

async def _async_connect_additional_powermeters(self):
"""Identifies the installed powermeters and re-establishes the connection to the E3DC with the corresponding powermeters config."""
ROOT_PM_INDEX = 0 # Index 0 is always the root powermeter of the E3DC
"""Identify the installed powermeters and reconnect to E3DC with the corresponding powermeters config."""
self._e3dcconfig["powermeters"] = self.e3dc.get_powermeters()
for powermeter in self._e3dcconfig["powermeters"]:
if powermeter["index"] == ROOT_PM_INDEX:
if powermeter["index"] == POWERMETER_ID_ROOT:
powermeter["name"] = "Root PM"
powermeter["key"] = "root-pm"
else:
Expand Down Expand Up @@ -255,7 +254,7 @@ def _process_manual_charge(self, request_data) -> None:

def _process_powermeters_data(self, powermeters_data) -> None:
for powermeter_data in powermeters_data:
if powermeter_data["index"] != 0:
if powermeter_data["index"] != POWERMETER_ID_ROOT:
for powermeter_config in self._e3dcconfig["powermeters"]:
if powermeter_data["index"] == powermeter_config["index"]:
self._mydata[powermeter_config["key"]] = (
Expand Down Expand Up @@ -517,26 +516,25 @@ def get_e3dcconfig(self) -> dict:


def create_e3dcinstance(
username: str, password: str, host: str, rscpkey: str, config: dict = None
username: str,
password: str,
host: str,
rscpkey: str,
config: dict[str, Any] | None = None,
) -> E3DC:
"""Create the actual E3DC instance, this will try to connect and authenticate."""
if config is None:
e3dc = E3DC(
E3DC.CONNECT_LOCAL,
username=username,
password=password,
ipAddress=host,
key=rscpkey,
)
else:
e3dc = E3DC(
E3DC.CONNECT_LOCAL,
username=username,
password=password,
ipAddress=host,
key=rscpkey,
configuration=config,
)
config = {}

e3dc = E3DC(
E3DC.CONNECT_LOCAL,
username=username,
password=password,
ipAddress=host,
key=rscpkey,
configuration=config,
)

return e3dc


Expand Down
6 changes: 3 additions & 3 deletions custom_components/e3dc_rscp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .const import DOMAIN, POWERMETER_ID_ROOT
from .coordinator import E3DCCoordinator

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -386,9 +386,9 @@ async def async_setup_entry(
for description in SENSOR_DESCRIPTIONS
]

# Add Sensor descriptions for additional powermeters
# Add Sensor descriptions for additional powermeters, skipp root PM
for powermeter_config in coordinator.get_e3dcconfig()["powermeters"]:
if powermeter_config["index"] != 0:
if powermeter_config["index"] != POWERMETER_ID_ROOT:
energy_description = SensorEntityDescription(
has_entity_name=True,
name=powermeter_config["name"] + " - total",
Expand Down
3 changes: 0 additions & 3 deletions custom_components/e3dc_rscp/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@
"battery-discharge": {
"name": "Battery discharge"
},
"additional-production": {
"name": "Additional source production"
},
"grid-production": {
"name": "Export to grid"
},
Expand Down
57 changes: 54 additions & 3 deletions custom_components/e3dc_rscp/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@
"battery-discharge": {
"name": "Battery discharge"
},
"additional-production": {
"name": "Additional source production"
},
"grid-production": {
"name": "Export to grid"
},
Expand Down Expand Up @@ -142,6 +139,60 @@
},
"manual-charge-energy": {
"name": "Energy charged from grid"
},
"undefined": {
"name": "Undefined Powermeter"
},
"root-pm": {
"name": "Root Powermeter"
},
"additional": {
"name": "Additional powermeter"
},
"additional-production": {
"name": "Additional production"
},
"additional-consumption": {
"name": "Additional consumption"
},
"farm": {
"name": "Farm"
},
"unused": {
"name": "Unused powermeter"
},
"wallbox": {
"name": "Wallbox"
},
"farm-additional": {
"name": "Farm additional powermeter"
},
"undefined-total": {
"name": "Undefined Powermeter - total"
},
"root-pm-total": {
"name": "Root Powermeter - total"
},
"additional-total": {
"name": "Additional Powermeter - total"
},
"additional-production-total": {
"name": "Additional production - total"
},
"additional-consumption-total": {
"name": "Additional consumption - total"
},
"farm-total": {
"name": "Farm - total"
},
"unused-total": {
"name": "Unused powermeter - total"
},
"wallbox-total": {
"name": "Wallbox - total"
},
"farm-additional-total": {
"name": "Farm additional powermeter - total"
}
},
"switch": {
Expand Down

0 comments on commit 46d7162

Please sign in to comment.