Skip to content

Commit

Permalink
Fix backwards compatibility for tariffs properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamish Findlay committed Feb 7, 2023
1 parent d7ef796 commit f9e5d27
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
45 changes: 26 additions & 19 deletions custom_components/battery_sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions custom_components/battery_sim/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f9e5d27

Please sign in to comment.