From f9e5d27b6055dbfac89d7a1f61ebe26a7b82373d Mon Sep 17 00:00:00 2001 From: Hamish Findlay Date: Tue, 7 Feb 2023 20:07:48 +0000 Subject: [PATCH] Fix backwards compatibility for tariffs properly --- custom_components/battery_sim/__init__.py | 45 +++++++++++++---------- custom_components/battery_sim/const.py | 1 + 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/custom_components/battery_sim/__init__.py b/custom_components/battery_sim/__init__.py index 568d570..6dedd56 100644 --- a/custom_components/battery_sim/__init__.py +++ b/custom_components/battery_sim/__init__.py @@ -26,6 +26,7 @@ CONF_BATTERY_MAX_DISCHARGE_RATE, CONF_BATTERY_MAX_CHARGE_RATE, CONF_BATTERY_SIZE, + CONF_ENERGY_TARIFF, CONF_ENERGY_IMPORT_TARIFF, CONF_ENERGY_EXPORT_TARIFF, CONF_IMPORT_SENSOR, @@ -81,25 +82,26 @@ async def async_setup(hass, config): """Set up platform from a YAML.""" hass.data.setdefault(DOMAIN, {}) - if config.get(DOMAIN)!= None: - for battery, conf in config.get(DOMAIN).items(): - _LOGGER.debug("Setup %s.%s", DOMAIN, battery) - handle = SimulatedBatteryHandle(conf, hass) - if (battery in hass.data[DOMAIN]): - _LOGGER.warning("Battery name not unique - not able to create.") - continue - hass.data[DOMAIN][battery] = handle - - for platform in BATTERY_PLATFORMS: - hass.async_create_task( - discovery.async_load_platform( - hass, - platform, - DOMAIN, - [{CONF_BATTERY: battery, CONF_NAME: conf.get(CONF_NAME, battery)}], - config, - ) + if config.get(DOMAIN)== None: + return True + for battery, conf in config.get(DOMAIN).items(): + _LOGGER.debug("Setup %s.%s", DOMAIN, battery) + handle = SimulatedBatteryHandle(conf, hass) + if (battery in hass.data[DOMAIN]): + _LOGGER.warning("Battery name not unique - not able to create.") + continue + hass.data[DOMAIN][battery] = handle + + for platform in BATTERY_PLATFORMS: + hass.async_create_task( + discovery.async_load_platform( + hass, + platform, + DOMAIN, + [{CONF_BATTERY: battery, CONF_NAME: conf.get(CONF_NAME, battery)}], + config, ) + ) return True async def async_setup_entry(hass, entry) -> bool: @@ -137,7 +139,12 @@ def __init__( self._second_import_sensor_id = config[CONF_SECOND_IMPORT_SENSOR] if (CONF_ENERGY_IMPORT_TARIFF not in config or len(config[CONF_ENERGY_IMPORT_TARIFF]) < 6): - self._import_tariff_sensor_id = None + """For backwards compatibility""" + if (CONF_ENERGY_TARIFF not in config or + len(config[CONF_ENERGY_TARIFF]) < 6): + self._import_tariff_sensor_id = None + else: + self._import_tariff_sensor_id = config[CONF_ENERGY_TARIFF] else: self._import_tariff_sensor_id = config[CONF_ENERGY_IMPORT_TARIFF] if (CONF_ENERGY_EXPORT_TARIFF not in config or diff --git a/custom_components/battery_sim/const.py b/custom_components/battery_sim/const.py index 15ea595..ce4fbaf 100644 --- a/custom_components/battery_sim/const.py +++ b/custom_components/battery_sim/const.py @@ -30,6 +30,7 @@ CONF_BATTERY_MAX_DISCHARGE_RATE = "max_discharge_rate_kw" CONF_BATTERY_MAX_CHARGE_RATE = "max_charge_rate_kw" CONF_BATTERY_EFFICIENCY = "efficiency" +CONF_ENERGY_TARIFF = "energy_tariff" CONF_ENERGY_IMPORT_TARIFF = "energy_import_tariff" CONF_ENERGY_EXPORT_TARIFF = "energy_export_tariff" ATTR_VALUE = "value"