Skip to content

Commit

Permalink
fix: make everything work (#3)
Browse files Browse the repository at this point in the history
* fix: make everything work

* update README
  • Loading branch information
firstof9 authored Dec 8, 2023
1 parent 00bc302 commit 4db934d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 13 deletions.
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
# ha-gasbuddy
# GasBuddy

[![GitHub Release][releases-shield]][releases]
[![GitHub Activity][commits-shield]][commits]
[![License][license-shield]](LICENSE)

[![hacs][hacsbadge]][hacs]
![Project Maintenance][maintenance-shield]
[![BuyMeCoffee][buymecoffeebadge]][buymecoffee]

[![Discord][discord-shield]][discord]
[![Community Forum][forum-shield]][forum]

_Component to integrate with [GasBuddy][GasBuddy] fuel price tracker._

**This component will set up the following platforms.**

Platform | Description
-- | --
`sensor` | Show info from an GasBuddy listed station.


## Installation via HACS (recommended)

[![Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=firstof9&repository=ha-gasbuddy)

1. Follow the link [here](https://hacs.xyz/docs/faq/custom_repositories/)
2. Use the custom repo link https://github.com/firstof9/ha-gasbuddy
3. Select the category type `integration`
4. Then once it's there (still in HACS) click the INSTALL button
5. Restart Home Assistant
6. Once restarted, in the HA UI go to `Configuration` (the ⚙️ in the lower left) -> `Devices and Services` click `+ Add Integration` and search for `GasBuddy`

## Manual (non-HACS)
<details>
<summary>Instructions</summary>

<br>
You probably do not want to do this! Use the HACS method above unless you know what you are doing and have a good reason as to why you are installing manually
<br>

1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
2. If you do not have a `custom_components` directory (folder) there, you need to create it.
3. In the `custom_components` directory (folder) create a new folder called `gasbuddy`.
4. Download _all_ the files from the `custom_components/gasbuddy/` directory (folder) in this repository.
5. Place the files you downloaded in the new directory (folder) you created.
6. Restart Home Assistant
7. Once restarted, in the HA UI go to `Configuration` (the ⚙️ in the lower left) -> `Devices and Services` click `+ Add Integration` and search for `GasBuddy`
</details>

## Configuration is done in the UI

<!---->

## Contributions are welcome!

If you want to contribute to this please read the [Contribution guidelines](CONTRIBUTING.md)

***

## TODO

- [ ] Add tests


[GasBuddy]: https://gasbuddy.com/
[integration_blueprint]: https://github.com/firstof9/ha-gasbuddy
[buymecoffee]: https://www.buymeacoffee.com/firstof9
[buymecoffeebadge]: https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg?style=for-the-badge
[commits-shield]: https://img.shields.io/github/commit-activity/y/firstof9/ha-gasbuddy.svg?style=for-the-badge
[commits]: https://github.com/firstof9/ha-gasbuddy/commits/main
[hacs]: https://github.com/custom-components/hacs
[hacsbadge]: https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge
[discord]: https://discord.gg/Qa5fW2R
[discord-shield]: https://img.shields.io/discord/330944238910963714.svg?style=for-the-badge
[exampleimg]: example.png
[forum-shield]: https://img.shields.io/badge/community-forum-brightgreen.svg?style=for-the-badge
[forum]: https://community.home-assistant.io/
[license-shield]: https://img.shields.io/github/license/firstof9/ha-gasbuddy.svg?style=for-the-badge
[maintenance-shield]: https://img.shields.io/badge/maintainer-Chris%20Nowak%20%40firstof9-blue.svg?style=for-the-badge
[releases-shield]: https://img.shields.io/github/release/firstof9/ha-gasbuddy.svg?style=for-the-badge
[releases]: https://github.com/firstof9/ha-gasbuddy/releases
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self, hass: HomeAssistant, interval: int, config: ConfigEntry) -> N

async def _async_update_data(self) -> dict:
"""Update data via library."""
station = self._config.get(CONF_STATION_ID)
station = self._config.data[CONF_STATION_ID]
try:
self._data = await GasBuddy(station_id=station).price_lookup()
except APIError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ async def _get_station_list(hass, user_input) -> list | None:
"""Return list of utilities by lat/lon."""
lat = None
lon = None
postal = user_input[CONF_POSTAL]
postal = ""

if user_input is not None and CONF_POSTAL in user_input.keys():
postal = user_input[CONF_POSTAL]


if not bool(postal):
lat = hass.config.latitude
Expand All @@ -42,8 +46,9 @@ async def _get_station_list(hass, user_input) -> list | None:
lat=lat, lon=lon, zipcode=postal
)
stations_list = {}
_LOGGER.debug("search reply: %s", stations)

for station in stations["results"]:
for station in stations["data"]["locationBySearchTerm"]["stations"]["results"]:
name = station["name"]
full_name = f'{station["name"]} @ {station["address"]["line1"]}'
stations_list[station["id"]] = full_name
Expand Down Expand Up @@ -129,7 +134,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any | None:

@config_entries.HANDLERS.register(DOMAIN)
class GasBuddyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for NWS Alerts."""
"""Config flow for GasBuddy."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
Expand Down Expand Up @@ -158,9 +163,9 @@ async def async_step_manual(self, user_input={}):
else:
self._data.update(user_input)
return self.async_create_entry(title=self._data[CONF_NAME], data=self._data)
return await self._show_config_zone(user_input)
return await self._show_config_manual(user_input)

async def _show_config_zone(self, user_input):
async def _show_config_manual(self, user_input):
"""Show the configuration form to edit location data."""

# Defaults
Expand Down Expand Up @@ -234,9 +239,9 @@ async def async_step_postal_list(self, user_input={}):
user_input[CONF_INTERVAL] = 3600
self._data.update(user_input)
return self.async_create_entry(title=self._data[CONF_NAME], data=self._data)
return await self._show_config_postal(user_input)
return await self._show_config_postal_list(user_input)

async def _show_config_postal(self, user_input):
async def _show_config_postal_list(self, user_input):
"""Show the configuration form to edit location data."""
defaults = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# sensor constants
UNIT_OF_MEASURE = {
"dollars_per_gallon": "gallon",
"dollars_per_litre": "liter",
"cents_per_liter": "liter",
}


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"documentation": "https://github.com/firstof9/ha-gasbuddy",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/firstof9/ha-gasbuddy/issues",
"requirements": ["py-gasbuddy==0.2.0"],
"requirements": ["py-gasbuddy==0.2.3"],
"version": "0.0.0-dev"
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def native_value(self) -> Any:
if data is None:
self._state = None
if self._type in data.keys():
self._state = data[self._type]
self._state = data[self._type]["price"]

_LOGGER.debug("Sensor [%s] updated value: %s", self._type, self._state)
return self._state
Expand All @@ -80,7 +80,7 @@ def native_unit_of_measurement(self) -> Any:
uom = self.coordinator.data["unit_of_measure"]
currency = self.coordinator.data["currency"]
if uom is not None and currency is not None:
return f'{currency}/{uom}'
return f'{currency}/{UNIT_OF_MEASURE[uom]}'
return None

@property
Expand Down
File renamed without changes.

0 comments on commit 4db934d

Please sign in to comment.