Skip to content

Commit

Permalink
Merge pull request #6 from moerpel/Dev
Browse files Browse the repository at this point in the history
Corrections and new entity
  • Loading branch information
moerpel authored May 23, 2024
2 parents 9813b75 + aae4faa commit 88680b6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
22 changes: 13 additions & 9 deletions custom_components/HA_SMA_EVCharger/evcharger_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def get_data(self):
_LOGGER.debug(f"API response: {json.dumps(data, indent=2)}")

# Data Processing
current_power = None
total_energy = None
evcharger_current_power = None
evcharger_total_energy = None
evcharger_connection_status = None

for item in data:
channel = item['channelId']
Expand Down Expand Up @@ -76,19 +77,22 @@ def get_data(self):
continue

if channel == "Measurement.ChaSess.WhIn":
total_energy = value / 1000 # Convert to kWh
evcharger_total_energy = value / 1000 # Convert to kWh
elif channel == "Measurement.Metering.GridMs.TotWIn":
current_power = value / 1000 # Convert to kW
evcharger_current_power = value / 1000 # Convert to kW
elif channel == "Measurement.Wl.ConnStt":
evcharger_connection_status = value # Connection Status

# Log the parsed values for debugging
_LOGGER.debug(f"Channel: {channel}, Current Power: {current_power}, Total Energy: {total_energy}")
_LOGGER.debug(f"Channel: {channel}, Current Power: {evcharger_current_power}, Total Energy: {evcharger_total_energy}, Connection Status: {evcharger_connection_status}")

if current_power is None or total_energy is None:
if evcharger_current_power is None or evcharger_total_energy is None:
_LOGGER.error("Failed to retrieve necessary data from the API response")
_LOGGER.debug(f"Final parsed values - Current Power: {current_power}, Total Energy: {total_energy}")
_LOGGER.debug(f"Final parsed values - Current Power: {evcharger_current_power}, Total Energy: {evcharger_total_energy}, Connection Status: {evcharger_connection_status}")
raise ValueError("Failed to retrieve necessary data from the API response")

return {
"current_power": current_power,
"total_energy": total_energy
"evcharger_current_power": evcharger_current_power,
"evcharger_total_energy": evcharger_total_energy,
"evcharger_connection_status": evcharger_total_energy
}
28 changes: 23 additions & 5 deletions custom_components/HA_SMA_EVCharger/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import DOMAIN, CONF_USERNAME, CONF_PASSWORD, CONF_API_URL

_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=15)
SCAN_INTERVAL = timedelta(seconds=30)

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the SMA EV Charger sensors."""
Expand All @@ -34,7 +34,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

async_add_entities([
EvChargerPowerSensor(coordinator),
EvChargerEnergySensor(coordinator)
EvChargerEnergySensor(coordinator),
EvChargerConnectionStatus(coordinator)
])

class EvChargerSensor(CoordinatorEntity, SensorEntity):
Expand Down Expand Up @@ -64,12 +65,12 @@ def name(self):
@property
def state(self):
"""Return the state of the sensor."""
return self.coordinator.data.get("current_power")
return self.coordinator.data.get("evcharger_current_power")

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return UnitOfPower.WATT
return UnitOfPower.KILO_WATT_HOUR

class EvChargerEnergySensor(EvChargerSensor):
"""Sensor for total energy charged."""
Expand All @@ -82,9 +83,26 @@ def name(self):
@property
def state(self):
"""Return the state of the sensor."""
return self.coordinator.data.get("total_energy")
return self.coordinator.data.get("evcharger_total_energy")

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return UnitOfEnergy.KILO_WATT_HOUR
class EvChargerConnectionStatus(EvChargerSensor):
"""Sensor for total connection status."""

@property
def name(self):
"""Return the name of the sensor."""
return "EVcharger Connection Status"

@property
def state(self):
"""Return the state of the sensor."""
return self.coordinator.data.get("evcharger_connection_status")

''' @property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return UnitOfEnergy.KILO_WATT_HOUR '''

0 comments on commit 88680b6

Please sign in to comment.