From 1f3b9bc70cc5e6c7db1522933c2182968d2e2162 Mon Sep 17 00:00:00 2001 From: RogerSelwyn Date: Thu, 13 Aug 2020 10:47:32 +0100 Subject: [PATCH] Fix creation of unrequired sensors in OVO energy (#38835) --- homeassistant/components/ovo_energy/sensor.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/ovo_energy/sensor.py b/homeassistant/components/ovo_energy/sensor.py index 5fe1bb056e7..4b9e2e70806 100644 --- a/homeassistant/components/ovo_energy/sensor.py +++ b/homeassistant/components/ovo_energy/sensor.py @@ -27,18 +27,20 @@ async def async_setup_entry( ] client: OVOEnergy = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] - currency = coordinator.data.electricity[ - len(coordinator.data.electricity) - 1 - ].cost.currency_unit + entities = [] + + if coordinator.data.electricity: + currency = coordinator.data.electricity[ + len(coordinator.data.electricity) - 1 + ].cost.currency_unit + entities.append(OVOEnergyLastElectricityReading(coordinator, client)) + entities.append(OVOEnergyLastElectricityCost(coordinator, client, currency)) + if coordinator.data.gas: + entities.append(OVOEnergyLastGasReading(coordinator, client)) + entities.append(OVOEnergyLastGasCost(coordinator, client, currency)) async_add_entities( - [ - OVOEnergyLastElectricityReading(coordinator, client), - OVOEnergyLastGasReading(coordinator, client), - OVOEnergyLastElectricityCost(coordinator, client, currency), - OVOEnergyLastGasCost(coordinator, client, currency), - ], - True, + entities, True, )