-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service call to return prices around device_track location (#…
…114) * feat: add service call to return prices around device_track location * formatting * remove unused import * update linter config * update test
- Loading branch information
Showing
12 changed files
with
1,242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""GasBuddy services.""" | ||
|
||
import logging | ||
|
||
import voluptuous as vol | ||
from gasbuddy import GasBuddy # pylint: disable=import-self | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_LATITUDE, ATTR_LONGITUDE | ||
from homeassistant.core import ( | ||
HomeAssistant, | ||
ServiceCall, | ||
ServiceResponse, | ||
SupportsResponse, | ||
callback, | ||
) | ||
from homeassistant.helpers import config_validation as cv | ||
|
||
from .const import DOMAIN, SERVICE_LOOKUP_GPS | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class GasBuddyServices: | ||
"""Class that holds our services.""" | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
config: ConfigEntry, | ||
) -> None: | ||
"""Initialize with hass object.""" | ||
self.hass = hass | ||
self._config = config | ||
|
||
@callback | ||
def async_register(self) -> None: | ||
"""Register all our services.""" | ||
self.hass.services.async_register( | ||
DOMAIN, | ||
SERVICE_LOOKUP_GPS, | ||
self._price_lookup_gps, | ||
schema=vol.Schema( | ||
{ | ||
vol.Required(ATTR_ENTITY_ID): cv.entity_ids, | ||
} | ||
), | ||
supports_response=SupportsResponse.ONLY, | ||
) | ||
|
||
# Setup services | ||
async def _price_lookup_gps(self, service: ServiceCall) -> ServiceResponse: | ||
"""Set the override.""" | ||
entity_ids = service.data[ATTR_ENTITY_ID] | ||
|
||
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) | ||
except Exception as err: | ||
_LOGGER.error("Error checking prices: %s", err) | ||
|
||
_LOGGER.debug("GPS price lookup: %s", results) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
lookup_gps: | ||
name: Lookup GPS | ||
description: List gas prices based on GPS coordinates of device tracker. | ||
target: | ||
entity: | ||
domain: device_tracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ disable= | |
too-many-locals, | ||
unexpected-keyword-arg, | ||
abstract-method, | ||
cyclic-import, | ||
|
||
[REFACTORING] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
py-gasbuddy==0.2.8 | ||
py-gasbuddy==0.2.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ isort | |
pylint==3.3.1 | ||
tox==4.23.2 | ||
pytest | ||
aioresponses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Provide common pytest fixtures.""" | ||
|
||
import os | ||
|
||
|
||
def load_fixture(filename): | ||
"""Load a fixture.""" | ||
path = os.path.join(os.path.dirname(__file__), "fixtures", filename) | ||
with open(path, encoding="utf-8") as fptr: | ||
return fptr.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.