feat: Use Home Assistant's shared aiohttp client session#210
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughConfig flow, coordinator, and service modules now explicitly obtain and pass Home Assistant aiohttp sessions via async_get_clientsession; related function signatures were updated to accept Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #210 +/- ##
=======================================
Coverage 99.06% 99.07%
=======================================
Files 8 8
Lines 537 541 +4
=======================================
+ Hits 532 536 +4
Misses 5 5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
custom_components/gasbuddy/services.py (1)
109-112: Consider moving GasBuddy instantiation outside the loop.A new
GasBuddyinstance is created for each entity in the loop. While the shared aiohttp session is correctly reused, you could instantiateGasBuddyonce before the loop sincesolver_urldoesn't change between iterations.♻️ Optional refactor to instantiate GasBuddy once
results = {} + api = GasBuddy( + solver_url=solver, + session=async_get_clientsession(self.hass), + ) for entity_id in entity_ids: try: entity = self.hass.states.get(entity_id) if entity: lat = entity.attributes[ATTR_LATITUDE] lon = entity.attributes[ATTR_LONGITUDE] - results[entity_id] = await GasBuddy( - solver_url=solver, - session=async_get_clientsession(self.hass), - ).price_lookup_service(lat=lat, lon=lon, limit=limit) + results[entity_id] = await api.price_lookup_service(lat=lat, lon=lon, limit=limit) except (APIError, LibraryError, CSRFTokenMissing) as ex: _LOGGER.error("Error checking prices: %s", ex)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@custom_components/gasbuddy/services.py` around lines 109 - 112, The code creates a new GasBuddy instance inside the per-entity loop every iteration; move the instantiation of GasBuddy(solver_url=solver, session=async_get_clientsession(self.hass)) to a single variable created before the loop and then call its price_lookup_service(lat=..., lon=..., limit=...) for each entity (replace the inline GasBuddy(...) in the results[entity_id] assignment with the precreated instance) to avoid redundant object creation while still reusing the shared aiohttp session.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@custom_components/gasbuddy/manifest.json`:
- Line 11: Update the pinned dependency in the manifest requirements so
installation uses the published py_gasbuddy release: replace the current
"py_gasbuddy==0.4.3" entry in the requirements array with "py_gasbuddy==0.4.2"
(or alternatively remove the version pin and wait until py_gasbuddy 0.4.3 is
available on PyPI); ensure the change is made in the manifest's "requirements"
array so installs won't fail due to an unpublished 0.4.3 release.
---
Nitpick comments:
In `@custom_components/gasbuddy/services.py`:
- Around line 109-112: The code creates a new GasBuddy instance inside the
per-entity loop every iteration; move the instantiation of
GasBuddy(solver_url=solver, session=async_get_clientsession(self.hass)) to a
single variable created before the loop and then call its
price_lookup_service(lat=..., lon=..., limit=...) for each entity (replace the
inline GasBuddy(...) in the results[entity_id] assignment with the precreated
instance) to avoid redundant object creation while still reusing the shared
aiohttp session.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 561e8357-65fd-42f7-94c8-14aa6d2acebe
📒 Files selected for processing (4)
custom_components/gasbuddy/config_flow.pycustom_components/gasbuddy/coordinator.pycustom_components/gasbuddy/manifest.jsoncustom_components/gasbuddy/services.py
Summary by CodeRabbit
Chores
Refactor