-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe69bbc
commit fe78a02
Showing
19 changed files
with
128 additions
and
3 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
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,88 @@ | ||
from __future__ import annotations | ||
from homeassistant.components.device_tracker.config_entry import TrackerEntity | ||
from homeassistant.components.device_tracker.const import SourceType | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
from .coordinator import FlightRadar24Coordinator | ||
from .const import DOMAIN | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
coordinator = hass.data[DOMAIN][entry.entry_id] | ||
tracked: dict[str, FlightRadar24Tracker] = {} | ||
|
||
@callback | ||
def coordinator_updated(): | ||
"""Update the status of the device.""" | ||
update_items(coordinator, async_add_entities, tracked) | ||
|
||
entry.async_on_unload(coordinator.async_add_listener(coordinator_updated)) | ||
coordinator_updated() | ||
|
||
|
||
@callback | ||
def update_items( | ||
coordinator: FlightRadar24Coordinator, | ||
async_add_entities: AddEntitiesCallback, | ||
tracked: dict[str, FlightRadar24Tracker], | ||
) -> None: | ||
if not coordinator.enable_tracker: | ||
return | ||
|
||
new_tracked: list[FlightRadar24Tracker] = [] | ||
active: list[str] = [] | ||
for flight in coordinator.tracked.values(): | ||
active.append(flight['flight_number']) | ||
if flight['flight_number'] not in tracked: | ||
tracked[flight['flight_number']] = FlightRadar24Tracker(coordinator, flight) | ||
new_tracked.append(tracked[flight['flight_number']]) | ||
else: | ||
tracked[flight['flight_number']].info = flight | ||
|
||
if new_tracked: | ||
async_add_entities(new_tracked) | ||
|
||
|
||
class FlightRadar24Tracker(CoordinatorEntity, TrackerEntity): | ||
def __init__( | ||
self, | ||
coordinator: FlightRadar24Coordinator, | ||
data: dict, | ||
) -> None: | ||
self.info = data | ||
|
||
super().__init__(coordinator) | ||
|
||
@property | ||
def source_type(self) -> SourceType: | ||
return SourceType.GPS | ||
|
||
@property | ||
def unique_id(self) -> str: | ||
return f"{self.coordinator.unique_id}_{DOMAIN}_{self.info['flight_number']}" | ||
|
||
@property | ||
def extra_state_attributes(self) -> dict[str, str]: | ||
return self.info | ||
|
||
@property | ||
def latitude(self) -> float | None: | ||
return self.info.get('latitude') | ||
|
||
@property | ||
def longitude(self) -> float | None: | ||
return self.info.get('longitude') | ||
|
||
@property | ||
def icon(self) -> str: | ||
return "mdi:airplane" | ||
|
||
@property | ||
def name(self) -> str: | ||
return self.info['flight_number'] |
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
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
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
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