Skip to content

Commit

Permalink
Update to use CoordinatorEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed Sep 5, 2020
1 parent e2353ad commit dcf97d0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 127 deletions.
37 changes: 9 additions & 28 deletions homeassistant/components/github/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)

from .const import (
CONF_CLONES,
Expand Down Expand Up @@ -193,14 +196,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
return unload_ok


class GitHubEntity(Entity):
class GitHubEntity(CoordinatorEntity):
"""Defines a GitHub entity."""

def __init__(
self, coordinator: DataUpdateCoordinator, unique_id: str, name: str, icon: str
) -> None:
"""Set up GitHub Entity."""
self._coordinator = coordinator
super().__init__(coordinator)
self._unique_id = unique_id
self._name = name
self._icon = icon
Expand All @@ -224,29 +227,7 @@ def icon(self) -> str:
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._coordinator.last_update_success and self._available

@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False

async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)

async def async_update(self) -> None:
"""Update GitHub entity."""
if await self._github_update():
self._available = True
else:
self._available = False

async def _github_update(self) -> bool:
"""Update GitHub entity."""
raise NotImplementedError()
return self.coordinator.last_update_success and self._available


class GitHubDeviceEntity(GitHubEntity):
Expand All @@ -255,7 +236,7 @@ class GitHubDeviceEntity(GitHubEntity):
@property
def device_info(self) -> Dict[str, Any]:
"""Return device information about this GitHub instance."""
data: GitHubData = self._coordinator.data
data: GitHubData = self.coordinator.data

return {
"entry_type": "service",
Expand Down
Loading

0 comments on commit dcf97d0

Please sign in to comment.