Skip to content

Commit

Permalink
chore: Added serial number to raised events
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Oct 7, 2023
1 parent bbf7082 commit 5e75d01
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 22 deletions.
12 changes: 11 additions & 1 deletion _docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This is fired when the current day rates are updated.
| `rates` | `list` | The list of rates applicable for the current day |
| `tariff_code` | `string` | The tariff code associated with current day's rates |
| `mpan` | `string` | The mpan of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Electricity Previous Day Rates

Expand All @@ -54,6 +55,7 @@ This is fired when the previous day rates are updated.
| `rates` | `list` | The list of rates applicable for the previous day |
| `tariff_code` | `string` | The tariff code associated with previous day's rates |
| `mpan` | `string` | The mpan of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Electricity Next Day Rates

Expand All @@ -66,6 +68,7 @@ This is fired when the next day rates are updated.
| `rates` | `list` | The list of rates applicable for the next day |
| `tariff_code` | `string` | The tariff code associated with next day's rates |
| `mpan` | `string` | The mpan of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Electricity Previous Consumption Rates

Expand All @@ -78,6 +81,7 @@ This is fired when the [previous consumption's](./sensors/electricity.md#previou
| `rates` | `list` | The list of rates applicable for the previous consumption |
| `tariff_code` | `string` | The tariff code associated with previous consumption's rates |
| `mpan` | `string` | The mpan of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Electricity Previous Consumption Override Rates

Expand All @@ -90,6 +94,7 @@ This is fired when the [previous consumption override's](./sensors/electricity.m
| `rates` | `list` | The list of rates applicable for the previous consumption override |
| `tariff_code` | `string` | The tariff code associated with previous consumption override's rates |
| `mpan` | `string` | The mpan of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Gas Current Day Rates

Expand All @@ -102,6 +107,7 @@ This is fired when the current day rates are updated.
| `rates` | `list` | The list of rates applicable for the current day |
| `tariff_code` | `string` | The tariff code associated with current day's rates |
| `mprn` | `string` | The mprn of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Gas Previous Day Rates

Expand All @@ -114,6 +120,7 @@ This is fired when the previous day rates are updated.
| `rates` | `list` | The list of rates applicable for the previous day |
| `tariff_code` | `string` | The tariff code associated with previous day's rates |
| `mprn` | `string` | The mprn of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Gas Next Day Rates

Expand All @@ -126,6 +133,7 @@ This is fired when the next day rates are updated.
| `rates` | `list` | The list of rates applicable for the next day |
| `tariff_code` | `string` | The tariff code associated with next day's rates |
| `mprn` | `string` | The mprn of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Gas Previous Consumption Rates

Expand All @@ -138,6 +146,7 @@ This is fired when the [previous consumption's](./sensors/gas.md#previous-accumu
| `rates` | `list` | The list of rates applicable for the previous consumption |
| `tariff_code` | `string` | The tariff code associated with previous consumption's rates |
| `mprn` | `string` | The mprn of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |

## Gas Previous Consumption Override Rates

Expand All @@ -149,4 +158,5 @@ This is fired when the [previous consumption override's](./sensors/gas.md#tariff
|-----------|------|-------------|
| `rates` | `list` | The list of rates applicable for the previous consumption override |
| `tariff_code` | `string` | The tariff code associated with previous consumption override's rates |
| `mprn` | `string` | The mprn of the meter associated with these rates |
| `mprn` | `string` | The mprn of the meter associated with these rates |
| `serial_number` | `string` | The serial number of the meter associated with these rates |
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ async def async_fetch_consumption_and_rates(
consumption_data = __sort_consumption(consumption_data)

if (is_electricity == True):
fire_event(EVENT_ELECTRICITY_PREVIOUS_CONSUMPTION_RATES, { "mpan": identifier, "tariff_code": tariff_code, "rates": rate_data })
fire_event(EVENT_ELECTRICITY_PREVIOUS_CONSUMPTION_RATES, { "mpan": identifier, "serial_number": serial_number, "tariff_code": tariff_code, "rates": rate_data })
else:
fire_event(EVENT_GAS_PREVIOUS_CONSUMPTION_RATES, { "mprn": identifier, "tariff_code": tariff_code, "rates": rate_data })
fire_event(EVENT_GAS_PREVIOUS_CONSUMPTION_RATES, { "mprn": identifier, "serial_number": serial_number, "tariff_code": tariff_code, "rates": rate_data })

_LOGGER.debug(f"Fired event for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def async_update(self):
}, consumption_and_cost["charges"]))
}

self._hass.bus.async_fire(EVENT_ELECTRICITY_PREVIOUS_CONSUMPTION_OVERRIDE_RATES, { "mpan": self._mpan, "tariff_code": self._tariff_code, "rates": rate_data })
self._hass.bus.async_fire(EVENT_ELECTRICITY_PREVIOUS_CONSUMPTION_OVERRIDE_RATES, { "mpan": self._mpan, "serial_number": self._serial_number, "tariff_code": self._tariff_code, "rates": rate_data })

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan):
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan):
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan):
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan):
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan):
if (event.data is not None and "mpan" in event.data and event.data["mpan"] == self._mpan and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def async_update(self):
"calorific_value": self._calorific_value
}

self._hass.bus.async_fire(EVENT_GAS_PREVIOUS_CONSUMPTION_OVERRIDE_RATES, { "mprn": self._mprn, "tariff_code": self._tariff_code, "rates": rate_data })
self._hass.bus.async_fire(EVENT_GAS_PREVIOUS_CONSUMPTION_OVERRIDE_RATES, { "mprn": self._mprn, "serial_number": self._serial_number, "tariff_code": self._tariff_code, "rates": rate_data })

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/gas/rates_current_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn):
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/gas/rates_next_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn):
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn):
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn):
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
2 changes: 1 addition & 1 deletion custom_components/octopus_energy/gas/rates_previous_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ async def async_added_to_hass(self) -> None:

@callback
def _async_handle_event(self, event) -> None:
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn):
if (event.data is not None and "mprn" in event.data and event.data["mprn"] == self._mprn and "serial_number" in event.data and event.data["serial_number"] == self._serial_number):
self._trigger_event(event.event_type, event.data)
self.async_write_ha_state()
11 changes: 4 additions & 7 deletions tests/unit/coordinators/test_previous_consumption_and_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from custom_components.octopus_energy.coordinators.previous_consumption_and_rates import async_fetch_consumption_and_rates
from custom_components.octopus_energy.api_client import OctopusEnergyApiClient

sensor_serial_number = "123456"

def assert_raised_events(
raised_events: dict,
expected_event_name: str,
Expand All @@ -19,6 +21,8 @@ def assert_raised_events(
assert expected_event_name in raised_events
assert expected_identifier in raised_events[expected_event_name]
assert raised_events[expected_event_name][expected_identifier] == expected_identifier_value
assert "serial_number" in raised_events[expected_event_name]
assert raised_events[expected_event_name]["serial_number"] == sensor_serial_number
assert "rates" in raised_events[expected_event_name]
assert len(raised_events[expected_event_name]["rates"]) > 2
assert "valid_from" in raised_events[expected_event_name]["rates"][0]
Expand All @@ -32,7 +36,6 @@ async def test_when_now_is_not_at_30_minute_mark_and_previous_data_is_available_
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = False
tariff_code = "AB-123"
is_smart_meter = True
Expand Down Expand Up @@ -88,7 +91,6 @@ async def test_when_now_is_at_30_minute_mark_and_previous_data_is_in_requested_p
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = False
tariff_code = "AB-123"
is_smart_meter = True
Expand Down Expand Up @@ -165,7 +167,6 @@ def fire_event(name, metadata):
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = False
tariff_code = "AB-123"
is_smart_meter = True
Expand Down Expand Up @@ -271,7 +272,6 @@ def fire_event(name, metadata):
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = True
tariff_code = "AB-123"
is_smart_meter = True
Expand Down Expand Up @@ -367,7 +367,6 @@ def fire_event(name, metadata):
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = False
tariff_code = "AB-123"
is_smart_meter = True
Expand Down Expand Up @@ -436,7 +435,6 @@ def fire_event(name, metadata):
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = True
tariff_code = "AB-123"
is_smart_meter = True
Expand Down Expand Up @@ -515,7 +513,6 @@ def fire_event(name, metadata):
client = OctopusEnergyApiClient("NOT_REAL")

sensor_identifier = "ABC123"
sensor_serial_number = "123456"
is_electricity = True
tariff_code = "AB-123"
is_smart_meter = True
Expand Down

0 comments on commit 5e75d01

Please sign in to comment.