From 5e75d01d8cdc5d69294ce2291fee63e6492b7dbf Mon Sep 17 00:00:00 2001 From: BottlecapDave Date: Sat, 7 Oct 2023 07:52:06 +0100 Subject: [PATCH] chore: Added serial number to raised events --- _docs/events.md | 12 +++++++++++- .../coordinators/previous_consumption_and_rates.py | 4 ++-- .../previous_accumulative_cost_override.py | 2 +- .../octopus_energy/electricity/rates_current_day.py | 2 +- .../octopus_energy/electricity/rates_next_day.py | 2 +- .../electricity/rates_previous_consumption.py | 2 +- .../rates_previous_consumption_override.py | 2 +- .../octopus_energy/electricity/rates_previous_day.py | 2 +- .../gas/previous_accumulative_cost_override.py | 2 +- .../octopus_energy/gas/rates_current_day.py | 2 +- .../octopus_energy/gas/rates_next_day.py | 2 +- .../octopus_energy/gas/rates_previous_consumption.py | 2 +- .../gas/rates_previous_consumption_override.py | 2 +- .../octopus_energy/gas/rates_previous_day.py | 2 +- .../test_previous_consumption_and_rates.py | 11 ++++------- 15 files changed, 29 insertions(+), 22 deletions(-) diff --git a/_docs/events.md b/_docs/events.md index d6453c22..7ca333c6 100644 --- a/_docs/events.md +++ b/_docs/events.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 | \ No newline at end of file +| `mprn` | `string` | The mprn of the meter associated with these rates | +| `serial_number` | `string` | The serial number of the meter associated with these rates | \ No newline at end of file diff --git a/custom_components/octopus_energy/coordinators/previous_consumption_and_rates.py b/custom_components/octopus_energy/coordinators/previous_consumption_and_rates.py index 9a690b00..e12906e9 100644 --- a/custom_components/octopus_energy/coordinators/previous_consumption_and_rates.py +++ b/custom_components/octopus_energy/coordinators/previous_consumption_and_rates.py @@ -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}") diff --git a/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py b/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py index 997a9395..4a719a1e 100644 --- a/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py +++ b/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py @@ -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.""" diff --git a/custom_components/octopus_energy/electricity/rates_current_day.py b/custom_components/octopus_energy/electricity/rates_current_day.py index a7d288d6..ea24af66 100644 --- a/custom_components/octopus_energy/electricity/rates_current_day.py +++ b/custom_components/octopus_energy/electricity/rates_current_day.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/rates_next_day.py b/custom_components/octopus_energy/electricity/rates_next_day.py index 65f721b7..feecaab5 100644 --- a/custom_components/octopus_energy/electricity/rates_next_day.py +++ b/custom_components/octopus_energy/electricity/rates_next_day.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/rates_previous_consumption.py b/custom_components/octopus_energy/electricity/rates_previous_consumption.py index 153a637f..8938158b 100644 --- a/custom_components/octopus_energy/electricity/rates_previous_consumption.py +++ b/custom_components/octopus_energy/electricity/rates_previous_consumption.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/rates_previous_consumption_override.py b/custom_components/octopus_energy/electricity/rates_previous_consumption_override.py index 191057db..7c50e7cd 100644 --- a/custom_components/octopus_energy/electricity/rates_previous_consumption_override.py +++ b/custom_components/octopus_energy/electricity/rates_previous_consumption_override.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/rates_previous_day.py b/custom_components/octopus_energy/electricity/rates_previous_day.py index f19f8e70..65b9e002 100644 --- a/custom_components/octopus_energy/electricity/rates_previous_day.py +++ b/custom_components/octopus_energy/electricity/rates_previous_day.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py b/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py index 953c79dc..d472fc05 100644 --- a/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py +++ b/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py @@ -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.""" diff --git a/custom_components/octopus_energy/gas/rates_current_day.py b/custom_components/octopus_energy/gas/rates_current_day.py index 8aa57104..a422b750 100644 --- a/custom_components/octopus_energy/gas/rates_current_day.py +++ b/custom_components/octopus_energy/gas/rates_current_day.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/rates_next_day.py b/custom_components/octopus_energy/gas/rates_next_day.py index c49cffef..8ab2970f 100644 --- a/custom_components/octopus_energy/gas/rates_next_day.py +++ b/custom_components/octopus_energy/gas/rates_next_day.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/rates_previous_consumption.py b/custom_components/octopus_energy/gas/rates_previous_consumption.py index 0a6739ba..5ffc0628 100644 --- a/custom_components/octopus_energy/gas/rates_previous_consumption.py +++ b/custom_components/octopus_energy/gas/rates_previous_consumption.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/rates_previous_consumption_override.py b/custom_components/octopus_energy/gas/rates_previous_consumption_override.py index b870486a..beadb371 100644 --- a/custom_components/octopus_energy/gas/rates_previous_consumption_override.py +++ b/custom_components/octopus_energy/gas/rates_previous_consumption_override.py @@ -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() \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/rates_previous_day.py b/custom_components/octopus_energy/gas/rates_previous_day.py index 44354151..adfff25d 100644 --- a/custom_components/octopus_energy/gas/rates_previous_day.py +++ b/custom_components/octopus_energy/gas/rates_previous_day.py @@ -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() \ No newline at end of file diff --git a/tests/unit/coordinators/test_previous_consumption_and_rates.py b/tests/unit/coordinators/test_previous_consumption_and_rates.py index 8d795419..6f73b223 100644 --- a/tests/unit/coordinators/test_previous_consumption_and_rates.py +++ b/tests/unit/coordinators/test_previous_consumption_and_rates.py @@ -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, @@ -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] @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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