Skip to content

Commit

Permalink
Fix timezone issue with EVC data
Browse files Browse the repository at this point in the history
  • Loading branch information
springfall2008 authored Jul 21, 2024
1 parent 838bcf4 commit 5d8c502
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
9 changes: 5 additions & 4 deletions custom_components/ge_cloud/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import random
from datetime import datetime
from datetime import timedelta
from datetime import timezone

_LOGGER = logging.getLogger(__name__)
TIMEOUT = 240
Expand Down Expand Up @@ -234,7 +235,7 @@ async def async_get_evc_sessions(self, uuid):
"""
Get list of EVC sessions
"""
now = datetime.now()
now = datetime.now(timezone.utc)
start = now - timedelta(hours=24)
start_time=start.strftime("%Y-%m-%dT%H:%M:%SZ")
end_time=now.strftime("%Y-%m-%dT%H:%M:%SZ")
Expand All @@ -249,8 +250,8 @@ async def async_get_evc_device_data(self, uuid):
"""
Get smart device data points
"""
now = datetime.now()
start = now - timedelta(minutes=5)
now = datetime.now(timezone.utc)
start = now - timedelta(minutes=10)
start_time=start.strftime("%Y-%m-%dT%H:%M:%SZ")
end_time=now.strftime("%Y-%m-%dT%H:%M:%SZ")

Expand All @@ -266,7 +267,7 @@ async def async_get_evc_device_data(self, uuid):
if meter_id == EVC_METER_CHARGER:
for point in meter.get("measurements", []):
measurand = point.get("measurand", None)
if measurand and measurand in EVC_DATA_POINTS:
if (measurand is not None) and measurand in EVC_DATA_POINTS:
value = point.get("value", None)
unit = point.get("unit", None)
result[EVC_DATA_POINTS[measurand]] = value
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ge_cloud/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import homeassistant.helpers.config_validation as cv

DOMAIN = "ge_cloud"
INTEGRATION_VERSION = "1.1.10"
INTEGRATION_VERSION = "1.1.11"
CONFIG_VERSION = 1

CONFIG_KIND = "kind"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ge_cloud/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ async def _async_update_data(self, first=False):
if self.type == "evc_device":
self.data["evc_device"] = await self.api.async_get_evc_device(self.serial)
self.data["point"] = await self.api.async_get_evc_device_data(self.serial)
if (self.update_count % 5) == 0:
self.data["commands"] = await self.api.async_get_evc_commands(self.serial)
if (self.update_count % 10) == 0:
self.data["sessions"] = await self.api.async_get_evc_sessions(self.serial)
if (self.update_count % 5) == 0:
self.data["commands"] = await self.api.async_get_evc_commands(self.serial)

_LOGGER.info("Coordinator data Update for device {}".format(self.device_name))
if not first:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ge_cloud/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"iot_class": "cloud_polling",
"requirements": ["pytz"],
"ssdp": [],
"version": "1.1.10",
"version": "1.1.11",
"zeroconf": []
}
52 changes: 27 additions & 25 deletions custom_components/ge_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,35 +455,35 @@ def extra_state_attributes(self) -> dict[str, Any]:
sessions = self.coordinator.data.get("sessions", [])
smart_point = self.coordinator.data.get("point", {})
session = {}
consumption_24h = 0
meter_start = None
meter_stop = None
consumption = 0
consumption_24h = 0

if sessions:
for session in sessions:
try:
meter_start = float(session.get("meter_start", 0)) / 1000.0
except (ValueError, TypeError):
pass
try:
if session.get("meter_stop", None) is None:
meter_start = session.get("meter_start", None)
meter_stop = session.get("meter_stop", None)

if meter_start is not None:
if meter_stop is None:
meter_stop = smart_point.get("Energy.Active.Import.Register", meter_start)
else:
meter_stop = session.get("meter_stop", None)

try:
meter_start = float(meter_start) / 1000.0
meter_stop = float(meter_stop) / 1000.0
except (ValueError, TypeError):
pass
consumption = float(meter_stop) - float(meter_start)
consumption_24h += consumption

if isinstance(meter_start, float) and isinstance(meter_stop, float):
consumption = float(meter_stop) - float(meter_start)
consumption_24h += consumption

return {
"started_by": session.get("started_by", None),
"meter_start": meter_start,
"started_at": session.get("started_at", None),
"meter_start": session.get("meter_start", None),
"stopped_by": session.get("stopped_by", None),
"meter_stop": meter_stop,
"stopped_at": session.get("stopped_at", None),
"meter_stop": session.get("meter_stop", None),
"stop_reason": session.get("stop_reason", None),
"consumption": consumption,
"consumption_24h": consumption_24h,
Expand All @@ -507,31 +507,33 @@ def native_value(self) -> float | datetime | None:
elif self.coordinator.type == "evc_device":
evc_device = self.coordinator.data.get("evc_device", {})
smart_point = self.coordinator.data.get("point", {})

if key == 'status':
value = evc_device.get("status", None)
elif key == 'last_session':
sessions = self.coordinator.data.get("sessions", [])
smart_point = self.coordinator.data.get("point", {})
session = {}
consumption = 0
meter_start = None
meter_stop = None
if sessions:
session = sessions[-1]
try:
meter_start = float(session.get("meter_start", 0)) / 1000.0
except (ValueError, TypeError):
pass
try:
if session.get("meter_stop", None) is None:
meter_start = session.get("meter_start", None)
meter_stop = session.get("meter_stop", None)

if meter_start is not None:
if meter_stop is None:
meter_stop = smart_point.get("Energy.Active.Import.Register", meter_start)
else:
meter_stop = session.get("meter_stop", None)

try:
meter_start = float(meter_start) / 1000.0
meter_stop = float(meter_stop) / 1000.0
except (ValueError, TypeError):
pass
if meter_start and meter_stop:

if isinstance(meter_start, float) and isinstance(meter_stop, float):
consumption = round(float(meter_stop) - float(meter_start), 2)

value = consumption
elif key == "voltage":
value = smart_point.get("Voltage", None)
Expand Down

0 comments on commit 5d8c502

Please sign in to comment.