Skip to content

Commit

Permalink
feat: add cash prices if available (#68)
Browse files Browse the repository at this point in the history
* feat: add cash prices if available

* linting

Signed-off-by: [email protected] <[email protected]>

---------

Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
firstof9 authored Sep 6, 2024
1 parent e5f8f0f commit 7c96a7c
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 15 deletions.
15 changes: 15 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# These are supported funding model platforms

github: firstof9 # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
polar: # Replace with a single Polar username
buy_me_a_coffee: firstof9 # Replace with a single Buy Me a Coffee username
thanks_dev: # Replace with a single thanks.dev username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
40 changes: 34 additions & 6 deletions custom_components/gasbuddy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Final

from homeassistant.components.sensor import SensorEntityDescription
from .entity import GasBuddySensorEntityDescription

# config flow
CONF_STATION_ID = "station_id"
Expand All @@ -29,29 +29,57 @@
}


SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
"regular_gas": SensorEntityDescription(
SENSOR_TYPES: Final[dict[str, GasBuddySensorEntityDescription]] = {
"regular_gas": GasBuddySensorEntityDescription(
key="regular_gas",
name="Regular Gas",
icon="mdi:gas-station",
suggested_display_precision=2,
),
"midgrade_gas": SensorEntityDescription(
"midgrade_gas": GasBuddySensorEntityDescription(
key="midgrade_gas",
name="MidGrade Gas",
icon="mdi:gas-station",
suggested_display_precision=2,
),
"premium_gas": SensorEntityDescription(
"premium_gas": GasBuddySensorEntityDescription(
key="premium_gas",
name="Premium Gas",
icon="mdi:gas-station",
suggested_display_precision=2,
),
"diesel": SensorEntityDescription(
"diesel": GasBuddySensorEntityDescription(
key="diesel",
name="Diesel",
icon="mdi:gas-station",
suggested_display_precision=2,
),
"regular_gas_cash": GasBuddySensorEntityDescription(
key="regular_gas",
name="Regular Gas (Cash)",
cash=True,
icon="mdi:gas-station",
suggested_display_precision=2,
),
"midgrade_gas_cash": GasBuddySensorEntityDescription(
key="midgrade_gas",
name="MidGrade Gas (Cash)",
cash=True,
icon="mdi:gas-station",
suggested_display_precision=2,
),
"premium_gas_cash": GasBuddySensorEntityDescription(
key="premium_gas",
name="Premium Gas (Cash)",
cash=True,
icon="mdi:gas-station",
suggested_display_precision=2,
),
"diesel_cash": GasBuddySensorEntityDescription(
key="diesel",
name="Diesel (Cash)",
cash=True,
icon="mdi:gas-station",
suggested_display_precision=2,
),
}
14 changes: 14 additions & 0 deletions custom_components/gasbuddy/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Support for GasBuddy entities."""

from __future__ import annotations

from dataclasses import dataclass

from homeassistant.components.sensor import SensorEntityDescription


@dataclass
class GasBuddySensorEntityDescription(SensorEntityDescription):
"""Class describing OpenEVSE select entities."""

cash: bool | None = None
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.4"],
"requirements": ["py-gasbuddy==0.2.5"],
"version": "0.0.0-dev"
}

6 changes: 5 additions & 1 deletion custom_components/gasbuddy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(
self.coordinator = coordinator
self._state = None
self._icon = sensor_description.icon
self._cash = sensor_description.cash

self._attr_name = f"{self._config.data[CONF_NAME]} {self._name}"
self._attr_unique_id = f"{self._name}_{self._unique_id}"
Expand All @@ -78,7 +79,10 @@ def native_value(self) -> Any:
if data is None:
self._state = None
if self._type in data.keys():
self._state = data[self._type]["price"]
if self._cash and "cash_price" in data[self._type]:
self._state = data[self._type]["cash_price"]
else:
self._state = data[self._type]["price"]

_LOGGER.debug("Sensor [%s] updated value: %s", self._type, self._state)
return self._state
Expand Down
50 changes: 50 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[MASTER]
ignore=tests
# Use a conservative default here; 2 should speed up most setups and not hurt
# any too bad. Override on command line as appropriate.
jobs=2
persistent=no

[BASIC]
good-names=id,i,j,k,ex,Run,_,fp
max-attributes=15
argument-naming-style=snake_case
attr-naming-style=snake_case

[MESSAGES CONTROL]
# Reasons disabled:
# locally-disabled - it spams too much
# too-many-* - are not enforced for the sake of readability
# too-few-* - same as too-many-*
# import-outside-toplevel - TODO
disable=
duplicate-code,
fixme,
import-outside-toplevel,
locally-disabled,
too-few-public-methods,
too-many-arguments,
too-many-public-methods,
too-many-instance-attributes,
too-many-branches,
too-many-statements,
broad-except,
too-many-lines,
too-many-locals,
unexpected-keyword-arg,
abstract-method,

[REFACTORING]

# Maximum number of nested blocks for function / method body
max-nested-blocks=8

[REPORTS]
score=no

[TYPECHECK]
# For attrs
ignored-classes=_CountingAttr

[FORMAT]
expected-line-ending-format=LF
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
py-gasbuddy==0.2.4
py-gasbuddy==0.2.5
1 change: 1 addition & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"premium_gas": {
"credit": "Owner",
"price": 3.45,
"cash_price": 3.35,
"last_updated": "2023-12-10T17:31:01.856Z",
},
}
8 changes: 4 additions & 4 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ async def test_setup_and_unload_entry(hass, mock_gasbuddy):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1

assert await hass.config_entries.async_unload(entries[0].entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8
assert len(hass.states.async_entity_ids(DOMAIN)) == 0

assert await hass.config_entries.async_remove(entries[0].entry_id)
Expand All @@ -44,13 +44,13 @@ async def test_setup_and_unload_entry_v1(hass, mock_gasbuddy):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1

assert await hass.config_entries.async_unload(entries[0].entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8
assert len(hass.states.async_entity_ids(DOMAIN)) == 0

assert await hass.config_entries.async_remove(entries[0].entry_id)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test_sensors(hass, mock_gasbuddy):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1

Expand All @@ -42,6 +42,9 @@ async def test_sensors(hass, mock_gasbuddy):
state = hass.states.get("sensor.gas_station_premium_gas")
assert state
assert state.state == "3.45"
state = hass.states.get("sensor.gas_station_premium_gas_cash")
assert state
assert state.state == "3.35"


async def test_sensors_no_uom(hass, mock_gasbuddy):
Expand All @@ -56,7 +59,7 @@ async def test_sensors_no_uom(hass, mock_gasbuddy):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1

Expand Down

0 comments on commit 7c96a7c

Please sign in to comment.