Skip to content

Commit

Permalink
Bugfixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Lindvig authored Dec 22, 2022
1 parent 742cb88 commit 15b1a26
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
6 changes: 5 additions & 1 deletion custom_components/fuelprices_dk/fuelprices_dk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
DEFAULT_PRICE_TYPE = "pump"
DIESEL = "diesel"
DIESEL_PLUS = "diesel+"
ELECTRIC = "electric"
OCTANE_95 = "oktan 95"
OCTANE_95_PLUS = "oktan 95+"
OCTANE_100 = "oktan 100"
FUEL_COMPANIES = {
"circlek": {
"name": "Circle K",
"url": "https://www.circlek.dk/",
"url": "https://www.circlek.dk/priser",
"products": {
OCTANE_95: {"name": "miles95"},
OCTANE_95_PLUS: {"name": "milesPLUS95"},
DIESEL: {"name": "miles Diesel B7"},
DIESEL_PLUS: {"name": "milesPLUS Diesel"},
ELECTRIC: {"name": "El Lynlader"},
},
},
"f24": {
Expand Down Expand Up @@ -198,6 +200,7 @@ def refreshPrices(self):
# Provide the URL and the dictionary with the products
# Update the dictionary with products with the returned data
self._products = getattr(self._parser, self._key)(self._url, self._products)
_LOGGER.debug("products: %s", self._products)
# If the Key 'priceType' is present, extract it from the dict, else use DEFAULT_PRICE_TYPE
self._priceType = self._products.pop("priceType", DEFAULT_PRICE_TYPE)

Expand All @@ -208,6 +211,7 @@ def getProductName(self, productKey):
return self._products[productKey]["name"]

def getProductPrice(self, productKey):
_LOGGER.debug("productDict: %s", self._products[productKey])
return self._products[productKey]["price"]

def getProductLastUpdate(self, productKey):
Expand Down
26 changes: 13 additions & 13 deletions custom_components/fuelprices_dk/fuelprices_dk_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _goon_ocr(self, url, products):

# CIRCLE K
def circlek(self, url, products):
return self._getDataFromTable(url, products, 1, -1)
return self._getDataFromTable(url, products, 1, 2)

# SHELL
def shell(self, url, products):
Expand Down Expand Up @@ -205,18 +205,18 @@ def _getDataFromTable(self, url, products, productCol, priceCol):
html = self._get_html_soup(r)

"""
Find all <tr> (rows) in the table
Loop through all the products with the Key and a dict as Value (Object)
Set found to False
Loop through all the Rows
If we previously have found a product, scontinue with the next product
Find all the <td> (cells)
If number of cells is larger than 1
Set found true/false whether we have found the product
If found
Extract, and clean, and add the price to the products dict
Return the dict og products
"""
Find all <tr> (rows) in the table
Loop through all the products with the Key and a dict as Value (Object)
Set found to False
Loop through all the Rows
If we previously have found a product, scontinue with the next product
Find all the <td> (cells)
If number of cells is larger than 1
Set found true/false whether we have found the product
If found
Extract, and clean, and add the price to the products dict
Return the dict og products
"""
rows = html.find_all("tr")
for productKey, productDict in products.items():
found = False
Expand Down
18 changes: 9 additions & 9 deletions custom_components/fuelprices_dk/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"domain": "fuelprices_dk",
"name": "Fuelprices DK",
"documentation": "https://github.com/J-Lindvig/Fuelprices_DK",
"issue_tracker": "https://github.com/J-Lindvig/Fuelprices_DK/issues",
"dependencies": [],
"codeowners": ["@J-Lindvig"],
"requirements": ["beautifulsoup4", "html.parser"],
"iot_class": "cloud_polling",
"version": "1.4"
"domain": "fuelprices_dk",
"name": "Fuelprices DK",
"documentation": "https://github.com/J-Lindvig/Fuelprices_DK",
"issue_tracker": "https://github.com/J-Lindvig/Fuelprices_DK/issues",
"dependencies": [],
"codeowners": ["@J-Lindvig"],
"requirements": ["beautifulsoup4", "html.parser"],
"iot_class": "cloud_polling",
"version": "1.5"
}
4 changes: 2 additions & 2 deletions custom_components/fuelprices_dk/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import asyncio
import logging
import time

from homeassistant.const import DEVICE_CLASS_MONETARY, ATTR_ATTRIBUTION
from .const import (
Expand Down Expand Up @@ -33,7 +33,7 @@ async def async_update_data():
# Sleep for 3 seconds
for company in fuelPrices.getCompanies():
await hass.async_add_executor_job(company.refreshPrices)
time.sleep(3)
await asyncio.sleep(3)

# Create a coordinator
coordinator = DataUpdateCoordinator(
Expand Down

0 comments on commit 15b1a26

Please sign in to comment.