Skip to content

Commit

Permalink
Fixes #17
Browse files Browse the repository at this point in the history
Fixed a bug that caused very frequent data updates (every 30s). This was
mostly due to the entity classes not overriding the `should_poll` method
and returning `False`. These entities now sub-class the
CoordinatorEntity which resolves this.
  • Loading branch information
boralyl committed Apr 16, 2021
1 parent 04bc4d0 commit 6709a5c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
28 changes: 10 additions & 18 deletions custom_components/nintendo_wishlist/binary_sensor.py
Expand Up @@ -2,10 +2,14 @@
from typing import List

from homeassistant import core
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util import slugify

from .const import DOMAIN
from .types import SwitchGame
from .types import EShopResults, SwitchGame

try:
from homeassistant.components.binary_sensor import BinarySensorEntity
Expand All @@ -30,12 +34,14 @@ async def async_setup_platform(
async_add_entities(sensors, True)


class SwitchGameQueryEntity(BinarySensorEntity):
class SwitchGameQueryEntity(CoordinatorEntity, BinarySensorEntity):
"""Represents a Query for a switch game."""

def __init__(self, coordinator, game_title: str):
def __init__(
self, coordinator: DataUpdateCoordinator[EShopResults], game_title: str
):
super().__init__(coordinator)
self.attrs = {}
self.coordinator = coordinator
self.game_title = game_title
self.matches: List[SwitchGame] = []

Expand Down Expand Up @@ -73,17 +79,3 @@ def name(self) -> str:
@property
def device_state_attributes(self):
return {"matches": self.matches}

async def async_update(self):
"""Update the entity.
This is only used by the generic entity update service. Normal updates
happen via the coordinator.
"""
await self.coordinator.async_request_refresh()

async def async_added_to_hass(self):
"""Subscribe entity to updates when added to hass."""
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)
4 changes: 2 additions & 2 deletions custom_components/nintendo_wishlist/eshop.py
Expand Up @@ -7,7 +7,7 @@
import aiohttp
from algoliasearch.search_client import SearchClient

from .types import ResultsDict, SwitchGame
from .types import EShopResults, ResultsDict, SwitchGame

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -101,7 +101,7 @@ def __init__(
self.wishlist_terms = [term.lower() for term in wishlist_terms]
self.fetch_method = self.fetch_na if country in NA_COUNTRIES else self.fetch_eu

async def fetch_on_sale(self) -> Dict[int, SwitchGame]:
async def fetch_on_sale(self) -> EShopResults:
"""Fetch data about games that are on sale."""
return await self.fetch_method()

Expand Down
2 changes: 1 addition & 1 deletion custom_components/nintendo_wishlist/manifest.json
Expand Up @@ -5,5 +5,5 @@
"dependencies": [],
"name": "Nintendo Wishlist Component",
"requirements": ["algoliasearch==2.2.0", "typing-extensions==3.7.4.1"],
"version": "3.1.4"
"version": "3.1.5"
}
28 changes: 8 additions & 20 deletions custom_components/nintendo_wishlist/sensor.py
@@ -1,9 +1,13 @@
import logging

from homeassistant import core
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)

from .const import DOMAIN
from .types import EShopResults

_LOGGER = logging.getLogger(__name__)

Expand All @@ -16,12 +20,12 @@ async def async_setup_platform(
async_add_entities([NintendoWishlistEntity(coordinator)], True)


class NintendoWishlistEntity(Entity):
class NintendoWishlistEntity(CoordinatorEntity):
"""Representation of a sensor."""

def __init__(self, coordinator):
def __init__(self, coordinator: DataUpdateCoordinator[EShopResults]):
super().__init__(coordinator)
self.attrs = {}
self.coordinator = coordinator
self._state = 0

@property
Expand Down Expand Up @@ -54,19 +58,3 @@ def state(self):
@property
def device_state_attributes(self):
return self.attrs

async def async_update(self):
"""Update the entity.
This is only used by the generic entity update service. Normal updates
happen via the coordinator.
"""
await self.coordinator.async_request_refresh()

async def async_added_to_hass(self):
"""Subscribe entity to updates when added to hass."""
self.async_on_remove(
self.coordinator.async_add_listener(
self.async_write_ha_state
)
)
3 changes: 3 additions & 0 deletions custom_components/nintendo_wishlist/types.py
Expand Up @@ -15,3 +15,6 @@ class SwitchGame(TypedDict):
class ResultsDict(TypedDict):
games: Dict[int, SwitchGame]
num_pages: int


EShopResults = Dict[int, SwitchGame]
2 changes: 1 addition & 1 deletion hacs.json
Expand Up @@ -3,5 +3,5 @@
"iot_class": "Cloud Polling",
"name": "Nintendo Wishlist",
"render_readme": true,
"homeassistant": "0.106.0"
"homeassistant": "0.118.5"
}

0 comments on commit 6709a5c

Please sign in to comment.