generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
228 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
|
||
DOMAIN = "fuel_prices" | ||
NAME = "Fuel Prices" | ||
|
||
CONF_AREAS = "areas" | ||
CONF_SOURCES = "sources" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""Device tracker for fuel prices.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
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 .const import CONF_AREAS, DOMAIN | ||
from .entity import FeulStationEntity | ||
from .coordinator import FuelPricesCoordinator | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Setup fuel prices device tracker component.""" | ||
cooridinator: FuelPricesCoordinator = hass.data[DOMAIN][entry.entry_id] | ||
areas = entry.data[CONF_AREAS] | ||
entities = [] | ||
for area in areas: | ||
_LOGGER.debug("Registering entities for area %s", area[CONF_NAME]) | ||
for station_id in cooridinator.api.find_fuel_locations_from_point( | ||
point=(area[CONF_LATITUDE], area[CONF_LONGITUDE]), radius=area[CONF_RADIUS] | ||
): | ||
entities.append( | ||
FeulStationTracker( | ||
coordinator=cooridinator, | ||
fuel_station_id=station_id, | ||
entity_id="devicetracker", | ||
) | ||
) | ||
|
||
async_add_entities(entities, True) | ||
|
||
|
||
class FeulStationTracker(FeulStationEntity, BaseTrackerEntity): | ||
"""A fuel station tracker entity.""" | ||
|
||
@property | ||
def name(self) -> str: | ||
"""Return the name 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.""" | ||
output = {} | ||
for fuel in self._fuel_station.available_fuels: | ||
output[fuel.fuel_type] = fuel.cost | ||
return output | ||
|
||
@property | ||
def latitude(self) -> float: | ||
"""Return the latitude.""" | ||
return self._fuel_station.lat | ||
|
||
@property | ||
def longitude(self) -> float: | ||
"""Return the longitude.""" | ||
return self._fuel_station.long | ||
|
||
@property | ||
def location_name(self) -> str: | ||
"""Return the name of the location.""" | ||
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, | ||
} | ||
if self.latitude is not None and self.longitude is not None: | ||
attr[ATTR_LATITUDE] = self.latitude | ||
attr[ATTR_LONGITUDE] = self.longitude | ||
|
||
return attr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Fuel Price entity base type.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
||
from .coordinator import FuelPricesCoordinator | ||
|
||
|
||
class FeulStationEntity(CoordinatorEntity): | ||
"""Represents a fuel station.""" | ||
|
||
def __init__( | ||
self, coordinator: FuelPricesCoordinator, fuel_station_id, entity_id | ||
) -> None: | ||
"""Initialize.""" | ||
super().__init__(coordinator) | ||
self.coordinator: FuelPricesCoordinator = coordinator | ||
self._fuel_station_id = fuel_station_id | ||
self._entity_id = entity_id | ||
|
||
@property | ||
def _fuel_station(self): | ||
"""Return the fuel station.""" | ||
return self.coordinator.api.get_fuel_location(self._fuel_station_id) | ||
|
||
@property | ||
def unique_id(self) -> str | None: | ||
"""Return unique ID.""" | ||
return f"fuelprices_{self._fuel_station_id}_{self._entity_id}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Find Fuel Locations service | ||
find_fuel_station: | ||
fields: | ||
location: | ||
required: true | ||
selector: | ||
location: | ||
radius: true | ||
find_fuels: | ||
fields: | ||
location: | ||
required: true | ||
selector: | ||
location: | ||
radius: true | ||
type: | ||
required: true | ||
selector: | ||
text: | ||
multiline: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ colorlog==6.7.0 | |
homeassistant==2023.8.0 | ||
pip>=21.0,<23.2 | ||
ruff==0.0.292 | ||
pyfuelprices==0.0.0 | ||
pyfuelprices==1.1.1 |