Skip to content

Commit

Permalink
bump pynintendoparental
Browse files Browse the repository at this point in the history
migrate from device tracker to sensor platform
  • Loading branch information
pantherale0 committed Mar 1, 2024
1 parent c7573db commit 44460a1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 62 deletions.
4 changes: 2 additions & 2 deletions custom_components/fuel_prices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion custom_components/fuel_prices/entity.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Fuel Price entity base type."""

from __future__ import annotations

from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .coordinator import FuelPricesCoordinator


class FeulStationEntity(CoordinatorEntity):
class FuelStationEntity(CoordinatorEntity):
"""Represents a fuel station."""

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/fuel_prices/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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."""
Expand All @@ -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
3 changes: 2 additions & 1 deletion custom_components/fuel_prices/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/fuel_prices/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
pyfuelprices==2.2.8

0 comments on commit 44460a1

Please sign in to comment.