Skip to content

Commit

Permalink
Optimize sensors update (#31)
Browse files Browse the repository at this point in the history
* Optimize sensors update

* fix sensors update after first update
  • Loading branch information
BlueBlueBlob authored and SebRut committed Dec 8, 2019
1 parent 9a49f79 commit d8231d4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions custom_components/grocy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
REQUIRED_FILES, SHOPPING_LIST_NAME, STARTUP, STOCK_NAME,
VERSION)

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -172,12 +172,23 @@ def __init__(self, hass, client):
EXPIRED_PRODUCTS_NAME : self.async_update_expired_products,
MISSING_PRODUCTS_NAME : self.async_update_missing_products,
}
self.sensor_update_dict = { STOCK_NAME : None,
CHORES_NAME : None,
SHOPPING_LIST_NAME : None,
EXPIRING_PRODUCTS_NAME : None,
EXPIRED_PRODUCTS_NAME : None,
MISSING_PRODUCTS_NAME : None,
}

async def async_update_data(self, sensor_type):
"""Update data."""
if sensor_type in self.sensor_types_dict:
# This is where the main logic to update platform data goes.
self.hass.async_create_task(self.sensor_types_dict[sensor_type]())
sensor_update = self.sensor_update_dict[sensor_type]
db_changed = await self.hass.async_add_executor_job(self.client.get_last_db_changed)
if db_changed != sensor_update:
self.sensor_update_dict[sensor_type] = db_changed
if sensor_type in self.sensor_types_dict:
# This is where the main logic to update platform data goes.
self.hass.async_create_task(self.sensor_types_dict[sensor_type]())

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update_stock(self):
Expand Down

0 comments on commit d8231d4

Please sign in to comment.