Skip to content

Commit

Permalink
feat: add results limit to gps price lookup (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Nov 22, 2024
1 parent 6574956 commit eb2d125
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions custom_components/gasbuddy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

# hass.data attribues
ATTR_IMAGEURL = "image_url"
ATTR_LIMIT = "limit"
COORDINATOR = "coordinator"
DOMAIN = "gasbuddy"
VERSION = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/gasbuddy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"import_executor": true,
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/firstof9/ha-gasbuddy/issues",
"requirements": ["py-gasbuddy==0.2.9"],
"requirements": ["py_gasbuddy==0.2.10"],
"version": "0.0.0-dev"
}

13 changes: 11 additions & 2 deletions custom_components/gasbuddy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from homeassistant.helpers import config_validation as cv

from .const import DOMAIN, SERVICE_LOOKUP_GPS
from .const import ATTR_LIMIT, DOMAIN, SERVICE_LOOKUP_GPS

_LOGGER = logging.getLogger(__name__)

Expand All @@ -42,6 +42,9 @@ def async_register(self) -> None:
schema=vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
vol.Optional(ATTR_LIMIT): vol.All(
vol.Coerce(int), vol.Range(min=1, max=99)
),
}
),
supports_response=SupportsResponse.ONLY,
Expand All @@ -51,14 +54,20 @@ def async_register(self) -> None:
async def _price_lookup_gps(self, service: ServiceCall) -> ServiceResponse:
"""Set the override."""
entity_ids = service.data[ATTR_ENTITY_ID]
limit = 5

if ATTR_LIMIT in service.data:
limit = service.data[ATTR_LIMIT]

results = {}
for entity_id in entity_ids:
try:
entity = self.hass.states.get(entity_id)
lat = entity.attributes[ATTR_LATITUDE]
lon = entity.attributes[ATTR_LONGITUDE]
results[entity_id] = await GasBuddy().price_lookup_gps(lat=lat, lon=lon)
results[entity_id] = await GasBuddy().price_lookup_gps(
lat=lat, lon=lon, limit=limit
)
except Exception as err:
_LOGGER.error("Error checking prices: %s", err)

Expand Down
4 changes: 3 additions & 1 deletion custom_components/gasbuddy/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ lookup_gps:
description: List gas prices based on GPS coordinates of device tracker.
target:
entity:
domain: device_tracker
domain:
- device_tracker
- person
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
py-gasbuddy==0.2.9
py_gasbuddy==0.2.10
18 changes: 17 additions & 1 deletion tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.const import ATTR_ENTITY_ID, ATTR_LATITUDE, ATTR_LONGITUDE
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.gasbuddy.const import DOMAIN
from custom_components.gasbuddy.const import ATTR_LIMIT, DOMAIN
from tests.common import load_fixture

from .const import CONFIG_DATA
Expand Down Expand Up @@ -68,6 +68,22 @@ async def test_lookup_gps(
== "2024-11-18T21:58:38.859Z"
)

mock_aioclient.post(
TEST_URL,
status=200,
body=load_fixture("results.json"),
)

response = await hass.services.async_call(
DOMAIN,
SERVICE_LOOKUP_GPS,
{ATTR_ENTITY_ID: entity_id, ATTR_LIMIT: 10},
blocking=True,
return_response=True,
)

assert len(response[entity_id]["results"]) == 10

mock_aioclient.post(
TEST_URL,
status=400,
Expand Down

0 comments on commit eb2d125

Please sign in to comment.