From 8cccbf37b0b4b98b3d112bfa24965f41f818804f Mon Sep 17 00:00:00 2001 From: myTselection Date: Fri, 5 Apr 2024 23:18:19 +0200 Subject: [PATCH] cleanup --- custom_components/carbu_com/manifest.json | 2 +- custom_components/carbu_com/sensor.py | 25 ++++++++++++++++------- custom_components/carbu_com/utils.py | 4 +++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/custom_components/carbu_com/manifest.json b/custom_components/carbu_com/manifest.json index 3748227..4ba94dc 100644 --- a/custom_components/carbu_com/manifest.json +++ b/custom_components/carbu_com/manifest.json @@ -8,6 +8,6 @@ "integration_type": "hub", "iot_class": "cloud_polling", "issue_tracker": "https://github.com/myTselection/carbu_com/issues", - "requirements": ["bs4","requests","ratelimit","uuid","re","urllib3"], + "requirements": ["bs4","requests","ratelimit"], "version": "10.0.0" } diff --git a/custom_components/carbu_com/sensor.py b/custom_components/carbu_com/sensor.py index 7cf721b..30dfd8c 100644 --- a/custom_components/carbu_com/sensor.py +++ b/custom_components/carbu_com/sensor.py @@ -214,6 +214,8 @@ def __init__(self, config, hass): self._town = config.get("town") self._filter = config.get("filter","") self._logo_with_price = config.get("logo_with_price", True) + self._price_unit = "€" if self._country.lower() != "us" else "$" + self._price_unit_per = "€/l" if self._country.lower() != "us" else "$/g" self._friendly_name_price_template = config.get("friendly_name_price_template","") @@ -416,6 +418,8 @@ def __init__(self, data, fueltype: FuelType, postalcode, isOil, quantity, indivi self._date = None self._score = None self._country = data._country + self._price_unit = data._price_unit + self._price_unit_per = data._price_unit_per self._id = None @property @@ -469,6 +473,7 @@ async def async_update(self): self._date = stationInfo.get("date") self._country = stationInfo.get("country") self._id = stationInfo.get("id") + self._score = stationInfo.get("score") async def async_will_remove_from_hass(self): @@ -550,7 +555,7 @@ def unit(self) -> int: @property def unit_of_measurement(self) -> str: """Return the unit of measurement this sensor expresses itself in.""" - return "€/l" + return self._price_unit_per @property def device_class(self): @@ -586,6 +591,8 @@ def __init__(self, data, fueltype: FuelType, postalcode, max_distance): self._diffPct = None self._country = data._country self._id = None + self._price_unit = data._price_unit + self._price_unit_per = data._price_unit_per @property def state(self): @@ -668,9 +675,9 @@ def extra_state_attributes(self) -> dict: "longitude": self._lon, "region": f"{self._max_distance}km", "distance": f"{self._distance}km", - "price diff": f"{self._diff}€", + "price diff": f"{self._diff}{self._price_unit}", "price diff %": f"{self._diffPct}%", - "price diff 30l": f"{self._diff30}€", + "price diff 30l": f"{self._diff30}{self._price_unit}", "date": self._date, "score": self._score, "filter": self._data._filter, @@ -699,7 +706,7 @@ def unit(self) -> int: @property def unit_of_measurement(self) -> str: """Return the unit of measurement this sensor expresses itself in.""" - return "€/l" + return self._price_unit_per @property def device_class(self): @@ -817,6 +824,8 @@ def __init__(self, data, fueltype: FuelType, quantity): self._date = None self._officialPriceToday = None self._officialPriceTodayDate = None + self._price_unit = data._price_unit + self._price_unit_per = data._price_unit_per @property @@ -903,9 +912,9 @@ def extra_state_attributes(self) -> dict: "fueltype": self._fueltype.name_lowercase.split('_')[0].title(), "fuelname": self._fuelname, "trend": self._trend, - "price": f"{self._price}€", + "price": f"{self._price}{self._price_unit}", "date": self._date, - "current official max price": f"{self._officialPriceToday} €/l", + "current official max price": f"{self._officialPriceToday} {self._price_unit_per}", "current official max price date": {self._officialPriceTodayDate}, "quantity": self._quantity } @@ -950,6 +959,8 @@ def __init__(self, data, fueltype): self._date = None self._priceNext = None self._dateNext = None + self._price_unit = data._price_unit + self._price_unit_per = data._price_unit_per self._friendly_name_official_template = self._data._friendly_name_official_template self._friendly_name_official_template_choice = self._data._friendly_name_official_template_choice @@ -1035,7 +1046,7 @@ def unit(self) -> int: @property def unit_of_measurement(self) -> str: """Return the unit of measurement this sensor expresses itself in.""" - return "€/l" + return self._price_unit_per @property def device_class(self): diff --git a/custom_components/carbu_com/utils.py b/custom_components/carbu_com/utils.py index a1f8582..92cc8d7 100644 --- a/custom_components/carbu_com/utils.py +++ b/custom_components/carbu_com/utils.py @@ -693,6 +693,7 @@ def getFuelPricesUS(self, postalcode, country, town, locationinfo, fueltype: Fue date = matching_fuel_product.get("credit", {}).get("posted_time") lat = block.get('info').get('latitude') lon = block.get('info').get('longitude') + score = block.get('info').get('star_rating') block_data = { @@ -707,6 +708,7 @@ def getFuelPricesUS(self, postalcode, country, town, locationinfo, fueltype: Fue 'price': price_text, # 'price_changed': price_changed, 'lat': lat, + 'score': score, 'lon': lon, 'fuelname': fueltype.name, 'distance': distance, @@ -1320,7 +1322,7 @@ def haversine_distance(self, lat1, lon1, lat2, lon2): c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) # Calculate the distance - distance = earth_radius * c + distance = round(earth_radius * c, 2) return distance