dvd-dev / dvd-dev/hilo

Refactor direct access to private attributes

Open
#888 0 comments 0 reactions 0 assignees View on GitHub
refactoring
Dominant language
Python
Stars
248
Forks
46
Avg merge
4h 4m
Merged PRs (30d)
26

Description

## Version of the custom_component

## Configuration

```yaml
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](https://github.com/dvd-dev/hilo/blob/f8cd20a620201f02ed9611d76a829adff56fbcd1/custom_components/hilo/__init__.py#L673-L709)
```python
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](https://github.com/dvd-dev/hilo/blob/f8cd20a620201f02ed9611d76a829adff56fbcd1/custom_components/hilo/__init__.py#L1056-L1075)
```python
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

```text
N/A
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.