Skip to content

Commit

Permalink
feat: Update device to include make, model and firmware where available
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Apr 8, 2023
1 parent 34003f4 commit f65c34a
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 59 deletions.
39 changes: 37 additions & 2 deletions custom_components/octopus_energy/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
meters(includeInactive: false) {{
makeAndType
serialNumber
makeAndType
smartExportElectricityMeter {{
deviceId
manufacturer
model
firmwareVersion
}}
smartImportElectricityMeter {{
deviceId
manufacturer
model
firmwareVersion
}}
}}
agreements {{
Expand Down Expand Up @@ -67,8 +74,12 @@
meters(includeInactive: false) {{
serialNumber
consumptionUnits
modelName
smartGasMeter {{
deviceId
manufacturer
model
firmwareVersion
}}
}}
agreements {{
Expand Down Expand Up @@ -179,7 +190,22 @@ async def async_get_account(self, account_id):
"serial_number": m["serialNumber"],
"is_export": m["smartExportElectricityMeter"] is not None,
"is_smart_meter": m["smartImportElectricityMeter"] is not None or m["smartExportElectricityMeter"] is not None,
"device_id": m["smartImportElectricityMeter"]["deviceId"] if m["smartImportElectricityMeter"] is not None else None
"device_id": m["smartImportElectricityMeter"]["deviceId"] if m["smartImportElectricityMeter"] is not None else None,
"manufacturer": m["smartImportElectricityMeter"]["manufacturer"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["manufacturer"]
if m["smartExportElectricityMeter"] is not None
else mp["meterPoint"]["makeAndType"],
"model": m["smartImportElectricityMeter"]["model"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["model"]
if m["smartExportElectricityMeter"] is not None
else None,
"firmware": m["smartImportElectricityMeter"]["firmwareVersion"]
if m["smartImportElectricityMeter"] is not None
else m["smartExportElectricityMeter"]["firmwareVersion"]
if m["smartExportElectricityMeter"] is not None
else None
},
mp["meterPoint"]["meters"]
if "meterPoint" in mp and "meters" in mp["meterPoint"] and mp["meterPoint"]["meters"] is not None
Expand All @@ -206,7 +232,16 @@ async def async_get_account(self, account_id):
"serial_number": m["serialNumber"],
"consumption_units": m["consumptionUnits"],
"is_smart_meter": m["smartGasMeter"] is not None,
"device_id": m["smartGasMeter"]["deviceId"] if m["smartGasMeter"] is not None else None
"device_id": m["smartGasMeter"]["deviceId"] if m["smartGasMeter"] is not None else None,
"manufacturer": m["smartGasMeter"]["manufacturer"]
if m["smartGasMeter"] is not None
else mp["meterPoint"]["modelName"],
"model": m["smartGasMeter"]["model"]
if m["smartGasMeter"] is not None
else None,
"firmware": m["smartGasMeter"]["firmwareVersion"]
if m["smartGasMeter"] is not None
else None
},
mp["meterPoint"]["meters"]
if "meterPoint" in mp and "meters" in mp["meterPoint"] and mp["meterPoint"]["meters"] is not None
Expand Down
34 changes: 17 additions & 17 deletions custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

SCAN_INTERVAL = timedelta(minutes=1)

def create_reading_coordinator(hass, client: OctopusEnergyApiClient, is_electricity, identifier, serial_number):
def create_reading_coordinator(hass, client: OctopusEnergyApiClient, is_electricity: bool, identifier: str, serial_number: str):
"""Create reading coordinator"""

async def async_update_data():
Expand Down Expand Up @@ -191,20 +191,20 @@ async def async_setup_default_sensors(hass, entry, async_add_entities):
if electricity_tariff_code != None:
for meter in point["meters"]:
_LOGGER.info(f'Adding electricity meter; mpan: {point["mpan"]}; serial number: {meter["serial_number"]}')
coordinator = create_reading_coordinator(hass, client, True, point["mpan"], meter["serial_number"])
entities.append(OctopusEnergyElectricityCurrentRate(rate_coordinator, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"], electricity_price_cap))
entities.append(OctopusEnergyElectricityPreviousRate(rate_coordinator, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
entities.append(OctopusEnergyElectricityNextRate(rate_coordinator, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
entities.append(OctopusEnergyElectricityCurrentStandingCharge(client, electricity_tariff_code, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
entities.append(OctopusEnergyElectricityCurrentRate(rate_coordinator, meter, point, electricity_price_cap))
entities.append(OctopusEnergyElectricityPreviousRate(rate_coordinator, meter, point))
entities.append(OctopusEnergyElectricityNextRate(rate_coordinator, meter, point))
entities.append(OctopusEnergyElectricityCurrentStandingCharge(client, electricity_tariff_code, meter, point))

if meter["is_smart_meter"] == True:
entities.append(OctopusEnergyPreviousAccumulativeElectricityConsumption(coordinator, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
entities.append(OctopusEnergyPreviousAccumulativeElectricityCost(coordinator, client, electricity_tariff_code, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
coordinator = create_reading_coordinator(hass, client, True, point["mpan"], meter["serial_number"])
entities.append(OctopusEnergyPreviousAccumulativeElectricityConsumption(coordinator, meter, point))
entities.append(OctopusEnergyPreviousAccumulativeElectricityCost(coordinator, client, electricity_tariff_code, meter, point))

if meter["is_export"] == False and CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION in config and config[CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION] == True:
consumption_coordinator = create_current_consumption_coordinator(hass, client, meter["device_id"], True)
entities.append(OctopusEnergyCurrentElectricityConsumption(consumption_coordinator, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
entities.append(OctopusEnergyCurrentElectricityDemand(consumption_coordinator, point["mpan"], meter["serial_number"], meter["is_export"], meter["is_smart_meter"]))
entities.append(OctopusEnergyCurrentElectricityConsumption(consumption_coordinator, meter, point))
entities.append(OctopusEnergyCurrentElectricityDemand(consumption_coordinator, meter, point))
else:
for meter in point["meters"]:
_LOGGER.info(f'Skipping electricity meter due to no active agreement; mpan: {point["mpan"]}; serial number: {meter["serial_number"]}')
Expand All @@ -229,18 +229,18 @@ async def async_setup_default_sensors(hass, entry, async_add_entities):
for meter in point["meters"]:
_LOGGER.info(f'Adding gas meter; mprn: {point["mprn"]}; serial number: {meter["serial_number"]}')
rate_coordinator = create_gas_rate_coordinator(hass, client, gas_tariff_code)
entities.append(OctopusEnergyGasCurrentRate(rate_coordinator, gas_tariff_code, point["mprn"], meter["serial_number"], gas_price_cap))
entities.append(OctopusEnergyGasCurrentStandingCharge(client, gas_tariff_code, point["mprn"], meter["serial_number"]))
entities.append(OctopusEnergyGasCurrentRate(rate_coordinator, gas_tariff_code, meter, point, gas_price_cap))
entities.append(OctopusEnergyGasCurrentStandingCharge(client, gas_tariff_code, meter, point))

if meter["is_smart_meter"] == True:
coordinator = create_reading_coordinator(hass, client, False, point["mprn"], meter["serial_number"])
entities.append(OctopusEnergyPreviousAccumulativeGasConsumption(coordinator, point["mprn"], meter["serial_number"], meter["consumption_units"], calorific_value))
entities.append(OctopusEnergyPreviousAccumulativeGasConsumptionKwh(coordinator, point["mprn"], meter["serial_number"], meter["consumption_units"], calorific_value))
entities.append(OctopusEnergyPreviousAccumulativeGasCost(coordinator, client, gas_tariff_code, point["mprn"], meter["serial_number"], meter["consumption_units"], calorific_value))
previous_consumption_coordinator = create_reading_coordinator(hass, client, False, point["mprn"], meter["serial_number"])
entities.append(OctopusEnergyPreviousAccumulativeGasConsumption(previous_consumption_coordinator, meter, point, calorific_value))
entities.append(OctopusEnergyPreviousAccumulativeGasConsumptionKwh(previous_consumption_coordinator, meter, point, calorific_value))
entities.append(OctopusEnergyPreviousAccumulativeGasCost(previous_consumption_coordinator, client, gas_tariff_code, meter, point, calorific_value))

if CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION in config and config[CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION] == True:
consumption_coordinator = create_current_consumption_coordinator(hass, client, meter["device_id"], False)
entities.append(OctopusEnergyCurrentGasConsumption(consumption_coordinator, point["mprn"], meter["serial_number"]))
entities.append(OctopusEnergyCurrentGasConsumption(consumption_coordinator, meter, point))
else:
for meter in point["meters"]:
_LOGGER.info(f'Skipping gas meter due to no active agreement; mprn: {point["mprn"]}; serial number: {meter["serial_number"]}')
Expand Down
18 changes: 12 additions & 6 deletions custom_components/octopus_energy/sensors/electricity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
)

class OctopusEnergyElectricitySensor(SensorEntity, RestoreEntity):
def __init__(self, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, meter, point):
"""Init sensor"""
self._mpan = mpan
self._serial_number = serial_number
self._is_export = is_export
self._is_smart_meter = is_smart_meter
self._point = point
self._meter = meter

self._mpan = point["mpan"]
self._serial_number = meter["serial_number"]
self._is_export = meter["is_export"]
self._is_smart_meter = meter["is_smart_meter"]
self._export_id_addition = "_export" if self._is_export == True else ""
self._export_name_addition = " Export" if self._is_export == True else ""

Expand All @@ -31,5 +34,8 @@ def device_info(self):
# Serial numbers/mpan are unique identifiers within a specific domain
(DOMAIN, f"electricity_{self._serial_number}_{self._mpan}")
},
"default_name": "Electricity Meter",
"default_name": f"Electricity Meter{self._export_name_addition}",
"manufacturer": self._meter["manufacturer"],
"model": self._meter["model"],
"sw_version": self._meter["firmware"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
class OctopusEnergyCurrentElectricityConsumption(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the current electricity consumption."""

def __init__(self, coordinator, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, coordinator, meter, point):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._state = None
self._latest_date = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
class OctopusEnergyCurrentElectricityDemand(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the current electricity demand."""

def __init__(self, coordinator, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, coordinator, meter, point):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._state = None
self._latest_date = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
class OctopusEnergyElectricityCurrentRate(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the current rate."""

def __init__(self, coordinator, mpan, serial_number, is_export, is_smart_meter, electricity_price_cap):
def __init__(self, coordinator, meter, point, electricity_price_cap):
"""Init sensor."""
# Pass coordinator to base class
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._state = None
self._last_updated = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
class OctopusEnergyElectricityNextRate(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the next rate."""

def __init__(self, coordinator, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, coordinator, meter, point):
"""Init sensor."""
# Pass coordinator to base class
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._state = None
self._last_updated = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
class OctopusEnergyPreviousAccumulativeElectricityConsumption(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the previous days accumulative electricity reading."""

def __init__(self, coordinator, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, coordinator, meter, point):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._state = None
self._latest_date = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
class OctopusEnergyPreviousAccumulativeElectricityCost(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the previous days accumulative electricity cost."""

def __init__(self, coordinator, client: OctopusEnergyApiClient, tariff_code, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, coordinator, client: OctopusEnergyApiClient, tariff_code, meter, point):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._client = client
self._tariff_code = tariff_code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
class OctopusEnergyElectricityPreviousRate(CoordinatorEntity, OctopusEnergyElectricitySensor):
"""Sensor for displaying the previous rate."""

def __init__(self, coordinator, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, coordinator, meter, point):
"""Init sensor."""
# Pass coordinator to base class
super().__init__(coordinator)
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._state = None
self._last_updated = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
class OctopusEnergyElectricityCurrentStandingCharge(OctopusEnergyElectricitySensor):
"""Sensor for displaying the current standing charge."""

def __init__(self, client: OctopusEnergyApiClient, tariff_code, mpan, serial_number, is_export, is_smart_meter):
def __init__(self, client: OctopusEnergyApiClient, tariff_code, meter, point):
"""Init sensor."""
OctopusEnergyElectricitySensor.__init__(self, mpan, serial_number, is_export, is_smart_meter)
OctopusEnergyElectricitySensor.__init__(self, meter, point)

self._client = client
self._tariff_code = tariff_code
Expand Down
12 changes: 9 additions & 3 deletions custom_components/octopus_energy/sensors/gas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
)

class OctopusEnergyGasSensor(SensorEntity, RestoreEntity):
def __init__(self, mprn, serial_number):
def __init__(self, meter, point):
"""Init sensor"""
self._mprn = mprn
self._serial_number = serial_number
self._point = point
self._meter = meter

self._mprn = point["mprn"]
self._serial_number = meter["serial_number"]

self._attributes = {
"mprn": self._mprn,
Expand All @@ -26,4 +29,7 @@ def device_info(self):
(DOMAIN, f"electricity_{self._serial_number}_{self._mprn}")
},
"default_name": "Gas Meter",
"manufacturer": self._meter["manufacturer"],
"model": self._meter["model"],
"sw_version": self._meter["firmware"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
class OctopusEnergyCurrentGasConsumption(CoordinatorEntity, OctopusEnergyGasSensor):
"""Sensor for displaying the current gas consumption."""

def __init__(self, coordinator, mprn, serial_number):
def __init__(self, coordinator, meter, point):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyGasSensor.__init__(self, mprn, serial_number)
OctopusEnergyGasSensor.__init__(self, meter, point)

self._state = None
self._latest_date = None
Expand Down
4 changes: 2 additions & 2 deletions custom_components/octopus_energy/sensors/gas/current_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
class OctopusEnergyGasCurrentRate(CoordinatorEntity, OctopusEnergyGasSensor):
"""Sensor for displaying the current rate."""

def __init__(self, coordinator, tariff_code, mprn, serial_number, gas_price_cap):
def __init__(self, coordinator, tariff_code, meter, point, gas_price_cap):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyGasSensor.__init__(self, mprn, serial_number)
OctopusEnergyGasSensor.__init__(self, meter, point)

self._tariff_code = tariff_code
self._gas_price_cap = gas_price_cap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
class OctopusEnergyPreviousAccumulativeGasConsumption(CoordinatorEntity, OctopusEnergyGasSensor):
"""Sensor for displaying the previous days accumulative gas reading."""

def __init__(self, coordinator, mprn, serial_number, native_consumption_units, calorific_value):
def __init__(self, coordinator, meter, point, calorific_value):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyGasSensor.__init__(self, mprn, serial_number)
OctopusEnergyGasSensor.__init__(self, meter, point)

self._native_consumption_units = native_consumption_units
self._native_consumption_units = meter["consumption_units"]
self._state = None
self._latest_date = None
self._calorific_value = calorific_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
class OctopusEnergyPreviousAccumulativeGasConsumptionKwh(CoordinatorEntity, OctopusEnergyGasSensor):
"""Sensor for displaying the previous days accumulative gas consumption in kwh."""

def __init__(self, coordinator, mprn, serial_number, native_consumption_units, calorific_value):
def __init__(self, coordinator, meter, point, calorific_value):
"""Init sensor."""
super().__init__(coordinator)
OctopusEnergyGasSensor.__init__(self, mprn, serial_number)
OctopusEnergyGasSensor.__init__(self, meter, point)

self._native_consumption_units = native_consumption_units
self._native_consumption_units = meter["consumption_units"]
self._state = None
self._latest_date = None
self._calorific_value = calorific_value
Expand Down
Loading

0 comments on commit f65c34a

Please sign in to comment.