Skip to content

Refactor direct access to private attributes #888

Description

@ic-dev21

Version of the custom_component

Configuration

N/A

Describe the bug

Currently, there are places in the codebase where private attributes (those prefixed with an underscore) are accessed directly. This practice circumvents encapsulation and can make the code harder to maintain or refactor in the future.

Below are some examples where direct access to private attributes can be found and should be refactored:


Example 1: Access to _events in Hilo class

custom_components/hilo/init.py

if event_data := self._events.get(event_id):
    # ...
    del self._events[event_id]
# ...
if event_id not in self._events:
    self._events[event_id] = await self._api.get_gd_events(
        self.devices.location_id, event_id=event_id
    )
return self._events[event_id]

Example 2: Access to _hass in Hilo class

custom_components/hilo/init.py

parent_unit_state = self._hass.states.get(attrs.get("source"))

Other instances may exist across the codebase. Refactor by introducing getter/setter methods or other encapsulation-friendly approaches to access private attributes.


This change will improve the maintainability and robustness of the codebase.

Debug log

N/A

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions