Skip to content

Commit

Permalink
fix: sensor.grid_returned_energy
Browse files Browse the repository at this point in the history
  • Loading branch information
acesyde authored Jan 31, 2024
1 parent 0de0c30 commit 433a45d
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 312 deletions.
1 change: 0 additions & 1 deletion .actrc

This file was deleted.

2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
rev: v0.1.15
hooks:
- id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ _Integration to integrate with [MyLight Systems][mylight_systems]._
| `sensor.total_msb_discharge` | My Smart Battery Discharge. | W/h | :white_check_mark: |
| `sensor.battery_state` | Current battery state. | kW | :white_check_mark: |
| `sensor.total_green_energy` | Total power consumned (from the production) by you home. | W/h | :white_check_mark: |
| `sensor.grid_returned_energy` | Total power returned to the grid. | W/h | :white_check_mark: |

## Installation

Expand All @@ -33,7 +34,7 @@ _Integration to integrate with [MyLight Systems][mylight_systems]._

1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
2. If you do not have a `custom_components` directory (folder) there, you need to create it.
3In the `custom_components` directory (folder) create a new folder called `mylight_systems`.
3In the `custom_components` directory (folder) create a new folder called `mylight_systems`.
3. Download _all_ the files from the `custom_components/mylight_systems/` directory (folder) in this repository.
4. Place the files you downloaded in the new directory (folder) you created.
5. Restart Home Assistant
Expand Down
5 changes: 1 addition & 4 deletions custom_components/mylight_systems/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
NAME = "MyLight Systems"
DOMAIN = "mylight_systems"
PLATFORMS = [Platform.SENSOR]
VERSION = "0.0.6"
VERSION = "0.0.7"
COORDINATOR = "coordinator"
ATTRIBUTION = "Data provided by https://www.mylight-systems.com/"
SCAN_INTERVAL_IN_MINUTES = 15
Expand All @@ -22,6 +22,3 @@
CONF_SUBSCRIPTION_ID = "subscription_id"
CONF_GRID_TYPE = "grid_type"
CONF_MASTER_RELAY_ID = "master_relay_id"

# MyLight
# DEFAULT_BASE_URL: str = "https://myhome.mylight-systems.com"
2 changes: 1 addition & 1 deletion custom_components/mylight_systems/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"documentation": "https://github.com/acesyde/hassio_mylight_integration",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/acesyde/hassio_mylight_integration/issues",
"version": "0.0.6"
"version": "0.0.7"
}
23 changes: 18 additions & 5 deletions custom_components/mylight_systems/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,31 @@ class MyLightSensorEntityDescription(
)


def _calculate_grid_returned_energy(data):
def _calculate_grid_returned_energy(data: MyLightSystemsCoordinatorData) -> float:
"""Calculate grid returned energy."""
if data is None:
return 0

# Energy produced by the solar panels
produced_energy = round(data.produced_energy.value / 36e2, 2) if data.produced_energy.value is not None else 0
produced_energy = 0

if data.produced_energy is not None and data.produced_energy.value is not None:
produced_energy = round(data.produced_energy.value / 36e2, 2)

# Energy consumed from the solar panels
green_energy = round(data.green_energy.value / 36e2, 2) if data.green_energy.value is not None else 0
green_energy = 0

if data.green_energy is not None and data.green_energy.value is not None:
green_energy = round(data.green_energy.value / 36e2, 2)

msb_charge = 0

# Virtual battery charge
msb_charge = round(data.msb_charge.value / 36e2, 2) if data.msb_charge.value is not None else 0
result = produced_energy - green_energy - msb_charge
if data.msb_charge is not None and data.msb_charge.value is not None:
msb_charge = round(data.msb_charge.value / 36e2, 2)

result = round(produced_energy - green_energy - msb_charge, 2)

if result > 0:
return result
else:
Expand Down
599 changes: 301 additions & 298 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mylight_systems"
version = "0.0.6"
version = "0.0.7"
description = "MyLight Systems integration for Home Assistant"
authors = ["Pierre-Emmanuel Mercier <[email protected]>"]
license = "MIT"
Expand Down
117 changes: 117 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from custom_components.mylight_systems.api.models import Measure
from custom_components.mylight_systems.coordinator import MyLightSystemsCoordinatorData
from custom_components.mylight_systems.sensor import _calculate_grid_returned_energy


def test_calculate_grid_returned_energy_with_none_data_should_return_0():
# Arrange
data = MyLightSystemsCoordinatorData(
grid_energy=None,
green_energy=None,
produced_energy=None,
grid_energy_without_battery=None,
msb_charge=None,
msb_discharge=None,
self_conso=None,
autonomy_rate=None,
battery_state=None,
)

# Act

result = _calculate_grid_returned_energy(data)

# Assert

assert result == 0


def test_calculate_grid_returned_energy_with_produced_energy_should_return_produced_energy():
# Arrange
data = MyLightSystemsCoordinatorData(
grid_energy=None,
green_energy=None,
produced_energy=Measure(key="abcd", value=3600, unit="kwh"),
grid_energy_without_battery=None,
msb_charge=None,
msb_discharge=None,
self_conso=None,
autonomy_rate=None,
battery_state=None,
)

# Act

result = _calculate_grid_returned_energy(data)

# Assert

assert result == 1.0


def test_calculate_grid_returned_energy_with_produced_and_green_return():
# Arrange
data = MyLightSystemsCoordinatorData(
grid_energy=None,
green_energy=Measure(key="abcd", value=1800, unit="kwh"),
produced_energy=Measure(key="abcd", value=3600, unit="kwh"),
grid_energy_without_battery=None,
msb_charge=None,
msb_discharge=None,
self_conso=None,
autonomy_rate=None,
battery_state=None,
)

# Act

result = _calculate_grid_returned_energy(data)

# Assert

assert result == 0.5


def test_calculate_grid_returned_energy_with_produced_and_msb_charge_return():
# Arrange
data = MyLightSystemsCoordinatorData(
grid_energy=None,
green_energy=None,
produced_energy=Measure(key="abcd", value=3600, unit="kwh"),
grid_energy_without_battery=None,
msb_charge=Measure(key="abcd", value=1800, unit="kwh"),
msb_discharge=None,
self_conso=None,
autonomy_rate=None,
battery_state=None,
)

# Act

result = _calculate_grid_returned_energy(data)

# Assert

assert result == 0.5

def test_calculate_grid_returned_energy_with_all_return():
# Arrange
data = MyLightSystemsCoordinatorData(
grid_energy=None,
green_energy=Measure(key="abcd", value=800, unit="kwh"),
produced_energy=Measure(key="abcd", value=3600, unit="kwh"),
grid_energy_without_battery=None,
msb_charge=Measure(key="abcd", value=700, unit="kwh"),
msb_discharge=None,
self_conso=None,
autonomy_rate=None,
battery_state=None,
)

# Act

result = _calculate_grid_returned_energy(data)

# Assert

assert result == 0.59

0 comments on commit 433a45d

Please sign in to comment.