Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid polling in the sensor #1652

Merged
merged 1 commit into from
Nov 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions custom_components/hacs/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from homeassistant.helpers.entity import Entity
from custom_components.hacs.const import DOMAIN, NAME_SHORT, VERSION
from custom_components.hacs.share import get_hacs
from homeassistant.core import callback


async def async_setup_platform(
Expand Down Expand Up @@ -40,7 +41,23 @@ def __init__(self):
self._state = None
self.repositories = []

@property
def should_poll(self):
"""No polling needed."""
return False

async def async_update(self):
"""Manual updates of the sensor."""
self._update()

@callback
def _update_and_write_state(self, *_):
"""Update the sensor and write state."""
self._update()
self.async_write_ha_state()

@callback
def _update(self):
"""Update the sensor."""
hacs = get_hacs()
if hacs.status.background_task:
Expand Down Expand Up @@ -97,3 +114,9 @@ def device_state_attributes(self):
}
)
return {"repositories": repositories}

async def async_added_to_hass(self) -> None:
"""Register for status events."""
self.async_on_remove(
self.hass.bus.async_listen("hacs/status", self._update_and_write_state)
Copy link
Member

Choose a reason for hiding this comment

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

This is overkill? that even is used too much
Currently, I don't think there are any good events for this, but in general, I like the idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ideally there would be a specific dispatch/event when repository changes pending_update status, but this seemed better than polling every 15 seconds since this effectively disappears from the profile in most cases:

Screen Shot 2020-11-14 at 9 19 54 AM

Copy link
Member

Choose a reason for hiding this comment

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

I want to add a task/job manager, that could create a separate event when done with the different tasks, but until then I guess status is fine 👍

)