Skip to content

Commit

Permalink
Remove entry when YAML removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Oct 7, 2023
1 parent 7a64897 commit 40b7743
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
25 changes: 22 additions & 3 deletions custom_components/variable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Variable implementation for Home Assistant."""
import copy
import json
import logging

Expand Down Expand Up @@ -27,6 +28,8 @@
CONF_RESTORE,
CONF_VALUE,
CONF_VARIABLE_ID,
CONF_YAML_PRESENT,
CONF_YAML_VARIABLE,
DEFAULT_REPLACE_ATTRIBUTES,
DOMAIN,
PLATFORMS,
Expand Down Expand Up @@ -115,7 +118,6 @@ async def _async_set_legacy_service(call, var_ent):
variables = json.loads(json.dumps(config.get(DOMAIN, {})))

for var, var_fields in variables.items():

if var is not None:
_LOGGER.debug(f"[YAML] variable_id: {var}")
_LOGGER.debug(f"[YAML] var_fields: {var_fields}")
Expand Down Expand Up @@ -158,14 +160,15 @@ async def _async_set_legacy_service(call, var_ent):
if var == ent.data.get(CONF_VARIABLE_ID):
entry_id = ent.entry_id
break
_LOGGER.debug(f"[YAML Update] entry_id: {entry_id}")
# _LOGGER.debug(f"[YAML Update] entry_id: {entry_id}")
if entry_id:
entry = ent
# _LOGGER.debug(f"[YAML Update] entry before: {entry.as_dict()}")

for m in dict(entry.data).keys():
var_fields.setdefault(m, entry.data[m])
_LOGGER.debug(f"[YAML Update] updated var_fields: {var_fields}")
var_fields.update({CONF_YAML_PRESENT: True})
# _LOGGER.debug(f"[YAML Update] Updated var_fields: {var_fields}")
entry.options = {}
hass.config_entries.async_update_entry(
entry, data=var_fields, options=entry.options
Expand All @@ -186,6 +189,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

entry.options = {}
# _LOGGER.debug(f"[init async_setup_entry] entry: {entry.data}")
if entry.data.get(CONF_YAML_VARIABLE, False) is True:
if entry.data.get(CONF_YAML_PRESENT, False) is False:
_LOGGER.warning(
f"[init async_setup_entry] YAML Entry no longer exists, deleting Entry: {entry.data.get('variable_id')}"
)
# _LOGGER.debug(f"[init async_setup_entry] YAML entry_id: {entry.entry_id}")
hass.async_create_task(hass.config_entries.async_remove(entry.entry_id))
return False
else:
yaml_data = copy.deepcopy(dict(entry.data))
yaml_data.pop(CONF_YAML_PRESENT, None)
entry.options = {}
hass.config_entries.async_update_entry(
entry, data=yaml_data, options=entry.options
)

hass.data.setdefault(DOMAIN, {})
hass_data = dict(entry.data)
hass.data[DOMAIN][entry.entry_id] = hass_data
Expand Down
22 changes: 14 additions & 8 deletions custom_components/variable/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
CONF_VALUE,
CONF_VALUE_TYPE,
CONF_VARIABLE_ID,
CONF_YAML_PRESENT,
CONF_YAML_VARIABLE,
DEFAULT_EXCLUDE_FROM_RECORDER,
DEFAULT_FORCE_UPDATE,
Expand Down Expand Up @@ -208,7 +209,6 @@ async def validate_sensor_input(hass: HomeAssistant, data: dict) -> dict[str, An


class VariableConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 1
# Connection classes in homeassistant/config_entries.py are now deprecated

Expand All @@ -226,6 +226,8 @@ async def async_step_add_sensor(
if user_input is not None:
user_input.update({CONF_ENTITY_PLATFORM: Platform.SENSOR})
user_input.update({CONF_YAML_VARIABLE: yaml_variable})
if yaml_variable:
user_input.update({CONF_YAML_PRESENT: True})
_LOGGER.debug(f"[New Sensor Variable] page_1_input: {user_input}")
self.add_sensor_input = user_input
return await self.async_step_sensor_page_2()
Expand Down Expand Up @@ -453,10 +455,11 @@ async def async_step_add_binary_sensor(
self, user_input=None, errors=None, yaml_variable=False
):
if user_input is not None:

try:
user_input.update({CONF_ENTITY_PLATFORM: Platform.BINARY_SENSOR})
user_input.update({CONF_YAML_VARIABLE: yaml_variable})
if yaml_variable:
user_input.update({CONF_YAML_PRESENT: True})
info = await validate_sensor_input(self.hass, user_input)
_LOGGER.debug(f"[New Binary Sensor] updated user_input: {user_input}")
return self.async_create_entry(
Expand All @@ -482,10 +485,11 @@ async def async_step_add_device_tracker(
self, user_input=None, errors=None, yaml_variable=False
):
if user_input is not None:

try:
user_input.update({CONF_ENTITY_PLATFORM: Platform.DEVICE_TRACKER})
user_input.update({CONF_YAML_VARIABLE: yaml_variable})
if yaml_variable:
user_input.update({CONF_YAML_PRESENT: True})
info = await validate_sensor_input(self.hass, user_input)
_LOGGER.debug(f"[New Device Tracker] updated user_input: {user_input}")
return self.async_create_entry(
Expand All @@ -511,6 +515,7 @@ async def async_step_add_device_tracker(
async def async_step_import(self, import_config=None) -> FlowResult:
"""Import a config entry from configuration.yaml."""

# _LOGGER.debug(f"[async_step_import] import_config: {import_config}")
return await self.async_step_add_sensor(
user_input=import_config, yaml_variable=True
)
Expand Down Expand Up @@ -554,7 +559,6 @@ async def async_step_init(
async def async_step_sensor_options(
self, user_input=None, errors=None
) -> FlowResult:

if user_input is not None:
_LOGGER.debug(f"[Sensor Options Page 1] page_1_input: {user_input}")
self.sensor_options_page_1 = user_input
Expand Down Expand Up @@ -772,7 +776,9 @@ def build_sensor_options_page_2(self):
else:
SENSOR_OPTIONS_PAGE_2_SCHEMA = SENSOR_OPTIONS_PAGE_2_SCHEMA.extend(
{
vol.Optional(CONF_VALUE,): selector.DateTimeSelector(
vol.Optional(
CONF_VALUE,
): selector.DateTimeSelector(
selector.DateTimeSelectorConfig()
)
}
Expand Down Expand Up @@ -885,7 +891,6 @@ def build_sensor_options_page_2(self):
async def async_step_binary_sensor_options(
self, user_input=None, errors=None
) -> FlowResult:

if user_input is not None:
_LOGGER.debug(f"[Binary Sensor Options] user_input: {user_input}")
for m in dict(self.config_entry.data).keys():
Expand Down Expand Up @@ -969,7 +974,6 @@ async def async_step_binary_sensor_options(
async def async_step_device_tracker_options(
self, user_input=None, errors=None
) -> FlowResult:

if user_input is not None:
_LOGGER.debug(f"[Device Tracker Options] user_input: {user_input}")
for m in dict(self.config_entry.data).keys():
Expand Down Expand Up @@ -1059,7 +1063,9 @@ async def async_step_device_tracker_options(
if self.config_entry.data.get(ATTR_BATTERY_LEVEL) is None:
DEVICE_TRACKER_OPTIONS_SCHEMA = DEVICE_TRACKER_OPTIONS_SCHEMA.extend(
{
vol.Optional(ATTR_BATTERY_LEVEL,): selector.NumberSelector(
vol.Optional(
ATTR_BATTERY_LEVEL,
): selector.NumberSelector(
selector.NumberSelectorConfig(
min=0,
max=100,
Expand Down
1 change: 1 addition & 0 deletions custom_components/variable/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
CONF_VALUE = "value"
CONF_VALUE_TYPE = "value_type"
CONF_VARIABLE_ID = "variable_id"
CONF_YAML_PRESENT = "yaml_present"
CONF_YAML_VARIABLE = "yaml_variable"
CONF_EXCLUDE_FROM_RECORDER = "exclude_from_recorder"
CONF_UPDATED = "config_updated"
Expand Down

0 comments on commit 40b7743

Please sign in to comment.