Skip to content

Remove helper entities on entry unload or options update - #2

Merged
FutureTense merged 6 commits into
FutureTense:mainfrom
raman325:remove_entities
Dec 9, 2020
Merged

Remove helper entities on entry unload or options update#2
FutureTense merged 6 commits into
FutureTense:mainfrom
raman325:remove_entities

Conversation

@raman325

@raman325 raman325 commented Dec 8, 2020

Copy link
Copy Markdown
Collaborator

Breaking change

Given that these aren't removed today, this might be considered a breaking change, but I'm not sure.

Proposed change

When removing a keymaster entry, all helper entities will now be removed. This ensures that on restart, all entities created by keymaster have been cleaned up (previously, binary_sensors and sensors were removed on restart automatically but helper entities persisted).

There is also logic to remove extra helper entities on an options update (when the number of slots or start slot changes).

This PR follows #1 and should only be merged after the first is merged so that I can rebase on it.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Here are the relevant changes, which are hard to see because this currently includes the diff from #1

def _get_entities_to_remove(
lock_name: str,
file_path: str,
code_slots_to_remove: Union[List[int], range],
remove_common_file: bool,
) -> List[str]:
"""Gets list of entities to remove."""
output_path = os.path.join(file_path, lock_name)
filenames = [f"{lock_name}_keymaster_{x}.yaml" for x in code_slots_to_remove]
if remove_common_file:
filenames.append(f"{lock_name}_keymaster_common.yaml")
entities = []
for filename in filenames:
file_dict = load_yaml(os.path.join(output_path, filename))
# get all entities from all helper domains that exist in package files
for domain in (
COUNTER_DOMAIN,
IN_BOOL_DOMAIN,
IN_DT_DOMAIN,
IN_NUM_DOMAIN,
IN_SELECT_DOMAIN,
IN_TXT_DOMAIN,
TIMER_DOMAIN,
):
entities.extend(
[f"{domain}.{ent_id}" for ent_id in file_dict.get(domain, {})]
)
return entities
async def _remove_entities(
hass: HomeAssistant,
config_entry: ConfigEntry,
code_slots_to_remove: Union[List[int], range],
remove_common_file: bool,
) -> List[str]:
"""Remove entities and return removed list."""
ent_reg = await async_get_registry(hass)
entities_to_remove = await hass.async_add_executor_job(
_get_entities_to_remove,
config_entry.data[CONF_LOCK_NAME],
os.path.join(hass.config.path(), config_entry.data[CONF_PATH]),
code_slots_to_remove,
remove_common_file,
)
for entity_id in entities_to_remove:
ent_reg.async_remove(entity_id)
return entities_to_remove

curr_slots = range(config_entry.data[CONF_START], config_entry.data[CONF_SLOTS] + 1)
new_slots = range(
config_entry.options[CONF_START], config_entry.options[CONF_SLOTS] + 1
)
await _remove_entities(
hass, config_entry, list(set(curr_slots) - set(new_slots)), False
)

await _remove_entities(
hass,
config_entry,
range(config_entry.data[CONF_START], config_entry.data[CONF_SLOTS] + 1),
True,
)

@firstof9

firstof9 commented Dec 8, 2020

Copy link
Copy Markdown
Collaborator

Good stuff

@firstof9 firstof9 added the enhancement New feature or request label Dec 8, 2020

@FutureTense FutureTense left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflicts occurring. Probably due to the other PR being accepted. Try resolving on your end and resubmit. I dont like resolving conflicts on code that isn’t mine

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging
New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging
@raman325

raman325 commented Dec 9, 2020

Copy link
Copy Markdown
Collaborator Author

Can you reopen this? I just resolved the conflicts.

@FutureTense FutureTense reopened this Dec 9, 2020
@FutureTense
FutureTense merged commit 29edb9f into FutureTense:main Dec 9, 2020
@raman325
raman325 deleted the remove_entities branch December 9, 2020 18:05
github-actions Bot pushed a commit that referenced this pull request Dec 9, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 9, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
FutureTense pushed a commit that referenced this pull request Dec 9, 2020
* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* fix path migration logic

* fix imports

* fix imports

* fix imports

* fix imports

* add pyproject.toml to configure black, isort, pytest, etc (from homeassistant repo)
github-actions Bot pushed a commit that referenced this pull request Dec 9, 2020
* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* fix path migration logic

* fix imports

* fix imports

* fix imports

* fix imports

* add pyproject.toml to configure black, isort, pytest, etc (from homeassistant repo)
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 9, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
FutureTense pushed a commit that referenced this pull request Dec 9, 2020
…, other potential bug fixes (#6)

* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* require unique name during config flow, delete base folder when empty, other bug fixes

* add missing typehints
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 9, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
FutureTense pushed a commit that referenced this pull request Dec 9, 2020
* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* reduce sensor polling frequency significantly
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 9, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
FutureTense pushed a commit that referenced this pull request Dec 10, 2020
* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* fix options flow

* properly fix options flow and a couple of other bugs

* black

* await setting unique ID
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 10, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
FutureTense pushed a commit that referenced this pull request Dec 13, 2020
* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* move code into submodules to make it easier to write tests for

* fix tests and bugfix

* forward unload to platforms

* change __init__ docstring

* add requirements_test.txt
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 13, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 14, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 22, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
FutureTense pushed a commit that referenced this pull request Dec 22, 2020
…tions update (#13)

* Remove helper entities on entry unload or options update (#2)

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix

* delete old files and folders when path or lock name changes during options update

* shorten function name
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 22, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 22, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
raman325 added a commit to raman325/keymaster that referenced this pull request Dec 23, 2020
* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* fix typo

* refactor, bug fixes, and improvements

New Features
- cleanup packages folder on entry removal

Changes
- switch to using relative path for CONF_PATH to reduce changes of error (existing config entries will be updated automatically)

Bug Fixes
- Switch _generate_package from async to sync because it does I/O on the filesystem
- fix output file not being opened with context manager (so never closed)
- fix sensor creation loop to only create necessary sensors instead of sensors for all code slots
- fix CodeSlotsData sensor (now a DataUpdateCoordinator) logic for OZW integrations

Code Cleanup/Improvements
- move CodeSlotsData logic into DataUpdateCoordinator since that's what it is being used for
- reduce polling frequency significantly by only polling any time a slot is set or cleared with a backup hourly polling in case updates are made outside of KeyMaster (this will significantly improve battery life over current implementation with the same level of accuracy)
- Update config entry using update method instead of directly manipulating attributes
- use slugify when fixing CONF_NAME
- use os.path.join to create paths for better OS compatibility
- switch to using f-strings
- only attempt cleanup of packages directory if it exists
- always rely on entry.data so that options is only used to capture options updates
- simplify code and reduced repeat logic by moving things into common functions
- raise Exceptions in services when there is a failure (this will be shown in UI)
- use more list comprehensions
- switch while loops to for loops
- added typing
- gitignore venv
- improve debug messaging

* remove helper entities on entry unload or options update

* remove counter since it's not actually used, it was just there for futureproofing

* bugfix
@tykeal
tykeal requested a review from Copilot May 31, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds cleanup of YAML-package-generated helper entities (input_boolean, input_datetime, input_number, input_select, input_text, timer) when a keymaster config entry is unloaded or when its options change. Previously these helpers persisted on restart, so the integration left orphaned entities behind. The PR also tweaks update_listener so options updates compute slot diffs and remove only the helpers for slots that disappeared.

Changes:

  • New _get_entities_to_remove / _remove_entities helpers that parse generated YAML and remove matching entries from the entity registry.
  • async_unload_entry now removes all helpers (including common ones) before shutil.rmtree-ing the package directory.
  • update_listener removes helpers for slots present before but absent after an options update, then regenerates packages and reloads.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
custom_components/keymaster/init.py Adds helper-entity cleanup in unload and options-update paths and the supporting _get_entities_to_remove / _remove_entities helpers.
custom_components/keymaster/const.py Edits ISSUE_URL (introduces a typo: keymasterkeypaster).
custom_components/keymaster/config_flow.py Whitespace-only reordering of imports.
custom_components/keymaster/sensor.py Whitespace-only import reordering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

DOMAIN = "keymaster"
VERSION = "0.0.45"
ISSUE_URL = "https://github.com/FutureTense/keymaster"
ISSUE_URL = "https://github.com/FutureTense/keypaster"
Comment on lines 500 to 519
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""

# Remove all generated helper entries
await _remove_entities(
hass,
config_entry,
range(config_entry.data[CONF_START], config_entry.data[CONF_SLOTS] + 1),
True,
)

# Remove all package files
output_path = os.path.join(
hass.config.path(),
config_entry.data[CONF_PATH],
config_entry.data[CONF_LOCK_NAME],
)
await hass.async_add_executor_job(shutil.rmtree, output_path)

return True
Comment on lines +522 to +533
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Update listener."""
# Get current code slots and new code slots, and remove entities for current code
# slots that are being removed
curr_slots = range(config_entry.data[CONF_START], config_entry.data[CONF_SLOTS] + 1)
new_slots = range(
config_entry.options[CONF_START], config_entry.options[CONF_SLOTS] + 1
)

await _remove_entities(
hass, config_entry, list(set(curr_slots) - set(new_slots)), False
)
Comment on lines +137 to +138
for filename in filenames:
file_dict = load_yaml(os.path.join(output_path, filename))
Comment on lines +171 to +172
for entity_id in entities_to_remove:
ent_reg.async_remove(entity_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants