Skip to content

Commit

Permalink
fix(sensor): Fixes to various sensors and how they update their values
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave authored Jul 21, 2023
1 parent 946f518 commit 92d8a57
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 104 deletions.
6 changes: 5 additions & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"changelogFile": "CHANGELOG.md"
}
],
"@semantic-release/github",
[
"@semantic-release/exec", {
"generateNotesCmd" : "NOTES=\"${nextRelease.notes}\n---\nEnjoying the integration? Why not make a one time or monthly [GitHub sponsorship](https://github.com/sponsors/bottlecapdave) or use my [Octopus Energy referral link](https://share.octopus.energy/gray-jade-372)?\" && echo $NOTES"
}
],
[
"@semantic-release/exec", {
"prepareCmd" : "node .build/update-manifest ${nextRelease.version}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity
Expand Down Expand Up @@ -68,22 +68,17 @@ def last_reset(self):
"""Return the time when the sensor was last reset, if any."""
return self._latest_date

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@property
def state(self):
"""Retrieve the latest electricity consumption"""
_LOGGER.debug('Updating OctopusEnergyCurrentElectricityConsumption')
consumption_result = self.coordinator.data

if (consumption_result is not None):
self._latest_date = consumption_result["startAt"]
self._state = consumption_result["consumption"] / 1000
self._attributes["last_updated_timestamp"] = consumption_result["startAt"]

self.async_write_ha_state()

@property
def state(self):
"""Retrieve the latest electricity consumption"""

return self._state

async def async_added_to_hass(self):
Expand Down
11 changes: 3 additions & 8 deletions custom_components/octopus_energy/electricity/current_demand.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity
Expand Down Expand Up @@ -65,8 +65,8 @@ def last_reset(self):
"""Return the time when the sensor was last reset, if any."""
return self._latest_date

@callback
def _handle_coordinator_update(self) -> None:
@property
def state(self):
"""Handle updated data from the coordinator."""
_LOGGER.debug('Updating OctopusEnergyCurrentElectricityConsumption')
consumption_result = self.coordinator.data
Expand All @@ -76,11 +76,6 @@ def _handle_coordinator_update(self) -> None:
self._state = consumption_result["demand"]
self._attributes["last_updated_timestamp"] = consumption_result["startAt"]

self.async_write_ha_state()

@property
def state(self):
"""Retrieve the latest electricity demand"""
return self._state

async def async_added_to_hass(self):
Expand Down
13 changes: 4 additions & 9 deletions custom_components/octopus_energy/electricity/current_rate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant

from homeassistant.util.dt import (now)
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -66,9 +66,9 @@ def extra_state_attributes(self):
"""Attributes of the sensor."""
return self._attributes

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@property
def state(self):
"""Retrieve the current rate for the sensor."""
# Find the current rate. We only need to do this every half an hour
current = now()
if (self._last_updated is None or self._last_updated < (current - timedelta(minutes=30)) or (current.minute % 30) == 0):
Expand Down Expand Up @@ -105,11 +105,6 @@ def _handle_coordinator_update(self) -> None:

self._last_updated = current

self.async_write_ha_state()

@property
def state(self):
"""The state of the sensor."""
return self._state

async def async_added_to_hass(self):
Expand Down
15 changes: 5 additions & 10 deletions custom_components/octopus_energy/electricity/next_rate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant

from homeassistant.util.dt import (now)
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -64,9 +64,9 @@ def extra_state_attributes(self):
"""Attributes of the sensor."""
return self._attributes

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@property
def state(self):
"""Retrieve the next rate for the sensor."""
# Find the next rate. We only need to do this every half an hour
current = now()
if (self._last_updated is None or self._last_updated < (current - timedelta(minutes=30)) or (current.minute % 30) == 0):
Expand Down Expand Up @@ -101,12 +101,7 @@ def _handle_coordinator_update(self) -> None:
self._state = None

self._last_updated = current

self.async_write_ha_state()

@property
def state(self):
"""The state of the sensor."""

return self._state

async def async_added_to_hass(self):
Expand Down
13 changes: 4 additions & 9 deletions custom_components/octopus_energy/electricity/previous_rate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant

from homeassistant.util.dt import (now)
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -64,9 +64,9 @@ def extra_state_attributes(self):
"""Attributes of the sensor."""
return self._attributes

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@property
def state(self):
"""Retrieve the previous rate."""
# Find the previous rate. We only need to do this every half an hour
current = now()
if (self._last_updated is None or self._last_updated < (current - timedelta(minutes=30)) or (current.minute % 30) == 0):
Expand Down Expand Up @@ -102,11 +102,6 @@ def _handle_coordinator_update(self) -> None:

self._last_updated = current

self.async_write_ha_state()

@property
def state(self):
"""The state of the sensor."""
return self._state

async def async_added_to_hass(self):
Expand Down
13 changes: 4 additions & 9 deletions custom_components/octopus_energy/gas/current_consumption.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity
Expand Down Expand Up @@ -68,21 +68,16 @@ def last_reset(self):
"""Return the time when the sensor was last reset, if any."""
return self._latest_date

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@property
def state(self):
"""The current consumption for the meter."""
_LOGGER.debug('Updating OctopusEnergyCurrentGasConsumption')
consumption_result = self.coordinator.data

if (consumption_result is not None):
self._latest_date = consumption_result["startAt"]
self._state = consumption_result["consumption"] / 1000

self.async_write_ha_state()

@property
def state(self):
"""Retrieve the latest gas consumption"""
return self._state

async def async_added_to_hass(self):
Expand Down
14 changes: 5 additions & 9 deletions custom_components/octopus_energy/intelligent/bump_charge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id

from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -50,20 +50,16 @@ def icon(self):
def extra_state_attributes(self):
"""Attributes of the sensor."""
return self._attributes

@property
def is_on(self):
"""The state of the sensor."""
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
"""Determine if the bump charge is on."""
if (self.coordinator.data is None) or (self._last_updated is not None and "last_updated" in self.coordinator.data and self._last_updated > self.coordinator.data["last_updated"]):
return self._state

self._state = is_in_bump_charge(utcnow(), self.coordinator.data["planned"])
self.async_write_ha_state()

return self._state

async def async_turn_on(self):
"""Turn on the switch."""
Expand Down
12 changes: 4 additions & 8 deletions custom_components/octopus_energy/intelligent/charge_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import time

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id

from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -64,19 +64,15 @@ def extra_state_attributes(self):

@property
def native_value(self) -> float:
"""The state of the sensor."""
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
"""The value of the charge limit."""
if (self.coordinator.data is None) or (self._last_updated is not None and "last_updated" in self.coordinator.data and self._last_updated > self.coordinator.data["last_updated"]):
self._attributes["last_updated_timestamp"] = self._last_updated
return self._state

self._attributes["last_updated_timestamp"] = self.coordinator.data["last_updated"]
self._state = self.coordinator.data["charge_limit_weekday"]
self.async_write_ha_state()

return self._state

async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
Expand Down
12 changes: 4 additions & 8 deletions custom_components/octopus_energy/intelligent/dispatching.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id

from homeassistant.util.dt import (now)
Expand Down Expand Up @@ -60,12 +60,7 @@ def extra_state_attributes(self):

@property
def is_on(self):
"""The state of the sensor."""
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
"""Determine if OE is currently dispatching energy."""
dispatches = self.coordinator.data
if (dispatches is not None):
self._attributes["planned_dispatches"] = self.coordinator.data["planned"]
Expand All @@ -79,7 +74,8 @@ def _handle_coordinator_update(self) -> None:

current_date = now()
self._state = is_in_planned_dispatch(current_date, self._attributes["planned_dispatches"])
self.async_write_ha_state()

return self._state

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
12 changes: 4 additions & 8 deletions custom_components/octopus_energy/intelligent/ready_time.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from datetime import time

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id

from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -53,19 +53,15 @@ def extra_state_attributes(self):

@property
def native_value(self) -> time:
"""The state of the sensor."""
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
"""The time that the car should be ready by."""
if (self.coordinator.data is None) or (self._last_updated is not None and "last_updated" in self.coordinator.data and self._last_updated > self.coordinator.data["last_updated"]):
self._attributes["last_updated_timestamp"] = self._last_updated
return self._state

self._attributes["last_updated_timestamp"] = self.coordinator.data["last_updated"]
self._state = self.coordinator.data["ready_time_weekday"]
self.async_write_ha_state()

self._state

async def async_set_value(self, value: time) -> None:
"""Set new value."""
Expand Down
10 changes: 3 additions & 7 deletions custom_components/octopus_energy/intelligent/smart_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ def extra_state_attributes(self):

@property
def is_on(self):
"""The state of the sensor."""
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
"""Determines if smart charge is currently on."""
if (self.coordinator.data is None) or (self._last_updated is not None and "last_updated" in self.coordinator.data and self._last_updated > self.coordinator.data["last_updated"]):
return self._state

self._state = self.coordinator.data["smart_charge"]
self.async_write_ha_state()

return self._state

async def async_turn_on(self):
"""Turn on the switch."""
Expand Down
11 changes: 3 additions & 8 deletions custom_components/octopus_energy/target_rates/target_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def extra_state_attributes(self):
"""Attributes of the sensor."""
return self._attributes

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@property
def is_on(self):
"""Determines if the target rate sensor is active."""
if CONFIG_TARGET_OFFSET in self._config:
offset = self._config[CONFIG_TARGET_OFFSET]
else:
Expand Down Expand Up @@ -189,11 +189,6 @@ def _handle_coordinator_update(self) -> None:

self._state = active_result["is_active"]

self.async_write_ha_state()

@property
def is_on(self):
"""The state of the sensor."""
return self._state

@callback
Expand Down

0 comments on commit 92d8a57

Please sign in to comment.