From 44460a16a4f0c0f944993cd4fcbc8b8433c54aec Mon Sep 17 00:00:00 2001 From: Jordan H Date: Fri, 1 Mar 2024 14:19:13 +0000 Subject: [PATCH] bump pynintendoparental migrate from device tracker to sensor platform --- custom_components/fuel_prices/__init__.py | 4 +- custom_components/fuel_prices/entity.py | 3 +- custom_components/fuel_prices/manifest.json | 2 +- .../{device_tracker.py => sensor.py} | 75 +++++-------------- custom_components/fuel_prices/strings.json | 3 +- .../fuel_prices/translations/en.json | 3 +- requirements.txt | 2 +- 7 files changed, 30 insertions(+), 62 deletions(-) rename custom_components/fuel_prices/{device_tracker.py => sensor.py} (55%) diff --git a/custom_components/fuel_prices/__init__.py b/custom_components/fuel_prices/__init__.py index 432653d..cf27b84 100644 --- a/custom_components/fuel_prices/__init__.py +++ b/custom_components/fuel_prices/__init__.py @@ -29,7 +29,7 @@ from .coordinator import FuelPricesCoordinator _LOGGER = logging.getLogger(__name__) -PLATFORMS = [Platform.DEVICE_TRACKER] +PLATFORMS = [Platform.SENSOR] def _build_configured_areas(hass_areas: dict) -> list[dict]: @@ -136,8 +136,8 @@ async def handle_fuel_location_lookup(call: ServiceCall) -> ServiceResponse: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" _LOGGER.debug("Unloading config entry %s", entry.entry_id) + await hass.data[DOMAIN][entry.entry_id].api.client_session.close() if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - await hass.data[DOMAIN][entry.entry_id].api.client_session.close() hass.data[DOMAIN].pop(entry.entry_id) return unload_ok diff --git a/custom_components/fuel_prices/entity.py b/custom_components/fuel_prices/entity.py index df3fdcc..6ef0d32 100644 --- a/custom_components/fuel_prices/entity.py +++ b/custom_components/fuel_prices/entity.py @@ -1,4 +1,5 @@ """Fuel Price entity base type.""" + from __future__ import annotations from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -6,7 +7,7 @@ from .coordinator import FuelPricesCoordinator -class FeulStationEntity(CoordinatorEntity): +class FuelStationEntity(CoordinatorEntity): """Represents a fuel station.""" def __init__( diff --git a/custom_components/fuel_prices/manifest.json b/custom_components/fuel_prices/manifest.json index 82e6ee6..b387257 100644 --- a/custom_components/fuel_prices/manifest.json +++ b/custom_components/fuel_prices/manifest.json @@ -12,7 +12,7 @@ "requirements": [ "reverse-geocode==1.4.1", "these-united-states==1.1.0.21", - "pyfuelprices==2.2.5" + "pyfuelprices==2.2.8" ], "ssdp": [], "version": "0.0.0", diff --git a/custom_components/fuel_prices/device_tracker.py b/custom_components/fuel_prices/sensor.py similarity index 55% rename from custom_components/fuel_prices/device_tracker.py rename to custom_components/fuel_prices/sensor.py index 5f4f26f..a8725dd 100644 --- a/custom_components/fuel_prices/device_tracker.py +++ b/custom_components/fuel_prices/sensor.py @@ -1,23 +1,21 @@ -"""Device tracker for fuel prices.""" +"""Sensor for fuel prices.""" from __future__ import annotations + import logging + +from collections.abc import Mapping +from typing import Any + +from homeassistant.components.sensor import SensorEntity from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS, CONF_NAME -from homeassistant.components.device_tracker.config_entry import ( - BaseTrackerEntity, - SourceType, - ATTR_SOURCE_TYPE, - ATTR_LATITUDE, - ATTR_LONGITUDE, -) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import StateType from pyfuelprices.const import PROP_FUEL_LOCATION_SOURCE from .const import CONF_AREAS, DOMAIN -from .entity import FeulStationEntity +from .entity import FuelStationEntity from .coordinator import FuelPricesCoordinator _LOGGER = logging.getLogger(__name__) @@ -51,28 +49,14 @@ async def async_setup_entry( async_add_entities(entities, True) -class FeulStationTracker(FeulStationEntity, BaseTrackerEntity): - """A fuel station tracker entity.""" +class FeulStationTracker(FuelStationEntity, SensorEntity): + """A fuel station entity.""" @property - def name(self) -> str: - """Return the name of the entity.""" + def native_value(self) -> str: + """Return the native value of the entity.""" return self._fuel_station.name - @property - def location_accuracy(self) -> int: - """Return the location accuracy of the device. - - Value in meters. - """ - return 0 - - @property - def state(self) -> str | None: - """Return the state of the device.""" - if self.location_name is not None: - return self.location_name - @property def _get_fuels(self) -> dict: """Return list of fuels.""" @@ -82,35 +66,16 @@ def _get_fuels(self) -> dict: return output @property - def latitude(self) -> float: - """Return the latitude.""" - return self._fuel_station.lat + def extra_state_attributes(self) -> Mapping[str, Any] | None: + """Return extra state attributes.""" + return {**self._fuel_station.__dict__(), **self._get_fuels} @property - def longitude(self) -> float: - """Return the longitude.""" - return self._fuel_station.long + def icon(self) -> str: + """Return entity icon.""" + return "mdi:gas-station" @property - def location_name(self) -> str: - """Return the name of the location.""" + def name(self) -> str: + """Return the name of the entity.""" return self._fuel_station.name - - @property - def source_type(self) -> SourceType: - """Return the source type.""" - return SourceType.GPS - - @property - def state_attributes(self) -> dict[str, StateType]: - """Return the fuel location prices.""" - attr: dict[str, StateType] = { - ATTR_SOURCE_TYPE: self.source_type, - **self._get_fuels, - **self._fuel_station.__dict__(), - } - if self.latitude is not None and self.longitude is not None: - attr[ATTR_LATITUDE] = self.latitude - attr[ATTR_LONGITUDE] = self.longitude - - return attr diff --git a/custom_components/fuel_prices/strings.json b/custom_components/fuel_prices/strings.json index ae7a634..1f4dd84 100644 --- a/custom_components/fuel_prices/strings.json +++ b/custom_components/fuel_prices/strings.json @@ -16,7 +16,8 @@ } }, "area_menu": { - "title": "Configure areas to register devices and sensors, by default your home location has already been added automatically with a radius of 20 miles, this can be removed or changed if needed.", + "title": "Configure areas to register devices and sensors.", + "description": "By default your home location has already been added automatically with a radius of 20 miles, this can be removed or changed if needed.", "menu_options": { "area_create": "Create an area", "area_update_select": "Update an area", diff --git a/custom_components/fuel_prices/translations/en.json b/custom_components/fuel_prices/translations/en.json index ae7a634..1f4dd84 100644 --- a/custom_components/fuel_prices/translations/en.json +++ b/custom_components/fuel_prices/translations/en.json @@ -16,7 +16,8 @@ } }, "area_menu": { - "title": "Configure areas to register devices and sensors, by default your home location has already been added automatically with a radius of 20 miles, this can be removed or changed if needed.", + "title": "Configure areas to register devices and sensors.", + "description": "By default your home location has already been added automatically with a radius of 20 miles, this can be removed or changed if needed.", "menu_options": { "area_create": "Create an area", "area_update_select": "Update an area", diff --git a/requirements.txt b/requirements.txt index 60aca4a..a33645e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ colorlog==6.7.0 homeassistant==2023.8.0 pip>=21.0,<23.2 ruff==0.0.292 -pyfuelprices==2.2.3 \ No newline at end of file +pyfuelprices==2.2.8 \ No newline at end of file