Skip to content

Commit 6aad978

Browse files
authored
fix: prefer grid sensor if available (#261)
* fix: prefer grid sensor if available * change logging message level * version bump
1 parent 443474c commit 6aad978

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

openevsehttp/__main__.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,22 +583,33 @@ def _version_check(self, min_version: str, max_version: str = "") -> bool:
583583

584584
# Self production HTTP Posting
585585

586-
async def self_production(self, grid: int, solar: int, invert: bool = True) -> None:
586+
async def self_production(
587+
self, grid: int | None = None, solar: int | None = None, invert: bool = True
588+
) -> None:
587589
"""Send pushed sensor data to self-prodcution."""
588590
if not self._version_check("4.0.0"):
589591
_LOGGER.debug("Feature not supported for older firmware.")
590592
raise UnsupportedFeature
591593

592594
# Invert the sensor -import/+export
593-
if invert:
595+
if invert and grid is not None:
594596
grid = grid * -1
595597

596598
url = f"{self.url}status"
597-
data = {"solar": solar, "grid_ie": grid}
599+
data = {}
600+
601+
# Prefer grid sensor data
602+
if grid is not None:
603+
data = {"grid_ie": grid}
604+
elif solar is not None:
605+
data = {"solar": solar}
598606

599-
_LOGGER.debug("Posting self-production: %s", data)
600-
response = await self.process_request(url=url, method="post", data=data)
601-
_LOGGER.debug("Self-production response: %s", response)
607+
if not data:
608+
_LOGGER.info("No sensor data to send to device.")
609+
else:
610+
_LOGGER.debug("Posting self-production: %s", data)
611+
response = await self.process_request(url=url, method="post", data=data)
612+
_LOGGER.debug("Self-production response: %s", response)
602613

603614
@property
604615
def hostname(self) -> str:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
PROJECT_DIR = Path(__file__).parent.resolve()
77
README_FILE = PROJECT_DIR / "README.md"
8-
VERSION = "0.1.54"
8+
VERSION = "0.1.55"
99

1010
setup(
1111
name="python-openevse-http",

tests/test_main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,16 +1510,21 @@ async def test_self_production(test_charger, test_charger_v2, mock_aioclient, ca
15101510
TEST_URL_STATUS,
15111511
status=200,
15121512
body='{"grid_ie": 3000, "solar": 1000}',
1513+
repeat=True,
15131514
)
15141515
with caplog.at_level(logging.DEBUG):
15151516
await test_charger.self_production(-3000, 1000)
1516-
assert (
1517-
"Posting self-production: {'solar': 1000, 'grid_ie': 3000}" in caplog.text
1518-
)
1517+
assert "Posting self-production: {'grid_ie': 3000}" in caplog.text
15191518
assert (
15201519
"Self-production response: {'grid_ie': 3000, 'solar': 1000}" in caplog.text
15211520
)
15221521

1522+
await test_charger.self_production(None, 1000)
1523+
assert "Posting self-production: {'solar': 1000}" in caplog.text
1524+
1525+
await test_charger.self_production(None, None)
1526+
assert "No sensor data to send to device." in caplog.text
1527+
15231528
with pytest.raises(UnsupportedFeature):
15241529
with caplog.at_level(logging.DEBUG):
15251530
await test_charger_v2.self_production(-3000, 1000)

0 commit comments

Comments
 (0)