Skip to content

Commit

Permalink
fix(sensor): Updated to reevaluate current rating every minute
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Dec 29, 2022
1 parent a4d4aca commit 03fce94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion custom_components/carbon_intensity/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def async_get_intensity_and_generation_rates(self, period_from, region):
results.append({
"from": as_utc(parse_datetime(item["from"])),
"to": as_utc(parse_datetime(item["to"])),
"intensity_forecast": item["intensity"]["forecast"],
"intensity_forecast": int(item["intensity"]["forecast"]),
"generation_mix": item["generationmix"]
})
except:
Expand Down
37 changes: 18 additions & 19 deletions custom_components/carbon_intensity/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,25 @@ def state(self):
"""The state of the sensor."""
# Find the current rate. We only need to do this every half an hour
now = utcnow()
if (now.minute % 30) == 0 or self._state == None:
self._state = 0
self._attributes = {}
self._state = None
self._attributes = {}

_LOGGER.info(f"Updating CarbonIntensityCurrentRating")

current_rate = None
if self.coordinator.data != None:
for period in self.coordinator.data:
if now >= period["from"] and now <= period["to"]:
current_rate = period
break

if current_rate != None:
self._attributes = {
"rate": current_rate,
"all_rates": self.coordinator.data
}

_LOGGER.info(f"Updating CarbonIntensityCurrentRating")

current_rate = None
if self.coordinator.data != None:
for period in self.coordinator.data:
if now >= period["from"] and now <= period["to"]:
current_rate = period
break

if current_rate != None:
self._attributes = {
"rate": current_rate,
"all_rates": self.coordinator.data
}

if current_rate != None:
self._state = current_rate["intensity_forecast"]
self._state = current_rate["intensity_forecast"]

return self._state

0 comments on commit 03fce94

Please sign in to comment.