Skip to content

Commit

Permalink
Fix energy sensors unit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdelprete committed Feb 10, 2024
1 parent ed1447c commit d2eb435
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 47 deletions.
65 changes: 39 additions & 26 deletions custom_components/4noks_elios4you/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def check_port(self) -> bool:
socket.setdefaulttimeout(sock_timeout)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_res = sock.connect_ex((self._host, self._port))
is_open = sock_res == 0 # True if open, False if not
# True if open, False if not
is_open = sock_res == 0
if is_open:
sock.shutdown(socket.SHUT_RDWR)
_LOGGER.debug(
Expand All @@ -118,31 +119,43 @@ def check_port(self) -> bool:
async def async_get_data(self):
"""Read Data Function."""

try:
reader, writer = await telnetlib3.open_connection(self._host, self._port)

dat_parsed = await self.telnet_send_cmd_parse_data("@dat", reader, writer)
for key, value in dat_parsed.items():
self.data[key] = value

inf_parsed = await self.telnet_send_cmd_parse_data("@inf", reader, writer)
for key, value in inf_parsed.items():
self.data[key] = value

sta_parsed = await self.telnet_send_cmd_parse_data("@sta", reader, writer)
for key, value in sta_parsed.items():
self.data[key] = value

except TimeoutError:
_LOGGER.debug("Connection or operation timed out")

except Exception as e:
_LOGGER.debug(f"An error occurred: {str(e)}")

finally:
if not writer.transport.is_closing():
writer.close()
# await writer.wait_closed()
if self.check_port():
try:
reader, writer = await telnetlib3.open_connection(
self._host, self._port
)

dat_parsed = await self.telnet_send_cmd_parse_data(
"@dat", reader, writer
)
for key, value in dat_parsed.items():
self.data[key] = value

inf_parsed = await self.telnet_send_cmd_parse_data(
"@inf", reader, writer
)
for key, value in inf_parsed.items():
self.data[key] = value

sta_parsed = await self.telnet_send_cmd_parse_data(
"@sta", reader, writer
)
for key, value in sta_parsed.items():
self.data[key] = value

except TimeoutError:
_LOGGER.debug("Connection or operation timed out")

except Exception as e:
_LOGGER.debug(f"An error occurred: {str(e)}")

finally:
if not writer.transport.is_closing():
writer.close()
# await writer.wait_closed()
else:
_LOGGER.debug("Elios4you not ready for telnet connection")
raise ConnectionError(f"Elios4you not active on {self._host}:{self._port}")

async def telnet_send_cmd_parse_data(cmd, reader, writer):
"""Send Telnet Commands and process output."""
Expand Down
7 changes: 5 additions & 2 deletions custom_components/4noks_elios4you/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.selector import selector
from pymodbus.exceptions import ConnectionException

from .api import Elios4YouAPI
from .const import (
Expand All @@ -30,6 +29,10 @@
_LOGGER = logging.getLogger(__name__)


class ConnectionError(Exception):
"""Empty Error Class."""


def host_valid(host):
"""Return True if hostname or IP address is valid."""
try:
Expand Down Expand Up @@ -78,7 +81,7 @@ async def test_connection(self, name, host, port, scan_interval):
_LOGGER.debug("API Client: get data")
_LOGGER.debug(f"API Client Data: {self.api_data}")
return self.api.data["sn"]
except ConnectionException as connerr:
except ConnectionError as connerr:
_LOGGER.error(
f"Failed to connect to host: {host}:{port} - Exception: {connerr}"
)
Expand Down
36 changes: 17 additions & 19 deletions custom_components/4noks_elios4you/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"""

from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import (
UnitOfPower,
)
from homeassistant.const import UnitOfEnergy, UnitOfPower

# Base component constants
NAME = "4-noks Elios4you integration"
Expand Down Expand Up @@ -105,127 +103,127 @@
"Produced_Energy": [
"Produced Energy",
"produced_energy",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Produced_Energy_F1": [
"Produced Energy F1",
"produced_energy_f1",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Produced_Energy_F2": [
"Produced Energy F2",
"produced_energy_f2",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Produced_Energy_F3": [
"Produced Energy F3",
"produced_energy_f3",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Consumed_Energy": [
"Consumed Energy",
"consumed_energy",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Consumed_Energy_F1": [
"Consumed Energy F1",
"consumed_energy_f1",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Consumed_Energy_F2": [
"Consumed Energy F2",
"consumed_energy_f2",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Consumed_Energy_F3": [
"Consumed Energy F3",
"consumed_energy_f3",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Bought_Energy": [
"Bought Energy",
"bought_energy",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Bought_Energy_F1": [
"Bought Energy F1",
"bought_energy_f1",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Bought_Energy_F2": [
"Bought Energy F2",
"bought_energy_f2",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Bought_Energy_F3": [
"Bought Energy F3",
"bought_energy_f3",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Sold_Energy": [
"Sold Energy",
"sold_energy",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Sold_Energy_F1": [
"Sold Energy F1",
"sold_energy_f1",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Sold_Energy_F2": [
"Sold Energy F2",
"sold_energy_f2",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
],
"Sold_Energy_F3": [
"Sold Energy F3",
"sold_energy_f3",
UnitOfPower.WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
"mdi:solar-power",
SensorDeviceClass.ENERGY,
SensorStateClass.TOTAL_INCREASING,
Expand Down

0 comments on commit d2eb435

Please sign in to comment.