Skip to content

Commit

Permalink
Added tracked_by_device to events and flight filter by altitude
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrErohin committed Feb 22, 2024
1 parent 8fd3179 commit e170cde
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 16 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ The default data is preset already
4. Click `SUBMIT`

### Edit Configuration
You may edit configuration data like radius or coordinates.
You may edit configuration data like:
1. Latitude and longitude of your point
2. Radius of your zone
3. Scan interval for updates in seconds
4. The minimum and maximum altitudes in foots between which the aircraft will be tracked
4. Username and password if you have FlightRadar24 subscription

If you have FlightRadar24 subscription, you may authenticate also

To do that
To do that:

1. Go to the <b>Settings</b>-><b>Devices & services</b>.
2. Search for `Flightradar24`, and click on it.
Expand All @@ -86,6 +89,17 @@ automation:
All available fields in `trigger.event.data` you can check [here](#flight)

If you have defined more than one device of FlightRadar24 for more places to observe - you may be interested to know what device has fired the event
It is stored in
#### <a id="tracked_by_device">`trigger.event.data.tracked_by_device`</a>

To change name in tracked_by_device
1. Go to the <b>Settings</b>-><b>Devices & services</b>.
2. Search for `Flightradar24`, and click on it.
3. Click on three-dot near of device you wanted
4. Click on `Rename` in the opened sub-menu
5. Enter new name and click `OK`

### <a id="lovelace">Lovelace Card</a>
You can add flight table to your [Home Assistant dashboard](https://www.home-assistant.io/dashboards/)

Expand Down Expand Up @@ -178,6 +192,7 @@ recorder:
## <a id="flight">Flight fields</a>
| Field | Description |
| --- |---|
| tracked_by_device | If you have defined more than one device of FlightRadar24 for more places to observe - you may be interested to know what device has fired the event. To renema the device check [this](#tracked_by_device) |
| flight_number | Flight Number |
| latitude | Current latitude of the aircraft |
| longitude | Current longitude of the aircraft |
Expand Down
10 changes: 9 additions & 1 deletion custom_components/flightradar24/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
CONF_PASSWORD,
CONF_USERNAME,
)
from .const import (
CONF_MIN_ALTITUDE,
CONF_MAX_ALTITUDE,
MIN_ALTITUDE,
MAX_ALTITUDE,
)
from FlightRadar24 import FlightRadar24API
from .sensor import SENSOR_TYPES

Expand Down Expand Up @@ -50,7 +56,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
client,
entry.data[CONF_SCAN_INTERVAL],
_LOGGER,
entry.entry_id
entry.entry_id,
entry.data.get(CONF_MIN_ALTITUDE, MIN_ALTITUDE),
entry.data.get(CONF_MAX_ALTITUDE, MAX_ALTITUDE),
)

await coordinator.async_config_entry_first_refresh()
Expand Down
24 changes: 16 additions & 8 deletions custom_components/flightradar24/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from .const import (
DOMAIN,
DEFAULT_NAME,
CONF_MIN_ALTITUDE,
CONF_MAX_ALTITUDE,
MIN_ALTITUDE,
MAX_ALTITUDE,
)
from FlightRadar24 import FlightRadar24API
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -47,7 +51,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo
CONF_LONGITUDE: self.hass.config.longitude,
},
)
)
)

@staticmethod
@callback
Expand Down Expand Up @@ -80,12 +84,16 @@ async def async_step_init(self, user_input: dict[str, Any] | None = None) -> Flo
return self.async_create_entry(title=DEFAULT_NAME, data=user_input)

data_schema = vol.Schema({
vol.Required(CONF_RADIUS, default=data.get(CONF_RADIUS)): vol.Coerce(float),
vol.Required(CONF_LATITUDE, default=data.get(CONF_LATITUDE)): cv.latitude,
vol.Required(CONF_LONGITUDE, default=data.get(CONF_LONGITUDE)): cv.longitude,
vol.Required(CONF_SCAN_INTERVAL, default=data.get(CONF_SCAN_INTERVAL)): int,
vol.Optional(CONF_USERNAME, description={"suggested_value": data.get(CONF_USERNAME, '')}): cv.string,
vol.Optional(CONF_PASSWORD, description={"suggested_value": data.get(CONF_PASSWORD, '')}): cv.string,
})
vol.Required(CONF_RADIUS, default=data.get(CONF_RADIUS)): vol.Coerce(float),
vol.Required(CONF_LATITUDE, default=data.get(CONF_LATITUDE)): cv.latitude,
vol.Required(CONF_LONGITUDE, default=data.get(CONF_LONGITUDE)): cv.longitude,
vol.Required(CONF_SCAN_INTERVAL, default=data.get(CONF_SCAN_INTERVAL)): int,
vol.Optional(CONF_MIN_ALTITUDE,
description={"suggested_value": data.get(CONF_MIN_ALTITUDE, MIN_ALTITUDE)}): int,
vol.Optional(CONF_MAX_ALTITUDE,
description={"suggested_value": data.get(CONF_MAX_ALTITUDE, MAX_ALTITUDE)}): int,
vol.Optional(CONF_USERNAME, description={"suggested_value": data.get(CONF_USERNAME, '')}): cv.string,
vol.Optional(CONF_PASSWORD, description={"suggested_value": data.get(CONF_PASSWORD, '')}): cv.string,
})

return self.async_show_form(step_id="init", data_schema=data_schema, errors=errors)
6 changes: 6 additions & 0 deletions custom_components/flightradar24/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
DOMAIN = "flightradar24"
URL = 'https://www.flightradar24.com/'

CONF_MIN_ALTITUDE = "min_altitude"
CONF_MAX_ALTITUDE = "max_altitude"

EVENT_FLIGHTRADAR24_ENTRY = f"{DOMAIN}_entry"
EVENT_FLIGHTRADAR24_EXIT = f"{DOMAIN}_exit"

MIN_ALTITUDE = -1
MAX_ALTITUDE = 100000
11 changes: 9 additions & 2 deletions custom_components/flightradar24/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __init__(
client: FlightRadar24API,
update_interval: int,
logger: Logger,
unique_id: str
unique_id: str,
min_altitude: int,
max_altitude: int,
) -> None:

self._bounds = bounds
Expand All @@ -34,6 +36,8 @@ def __init__(
self.tracked: dict[int, dict[str, Any]] | None = None
self.entered = {}
self.exited = {}
self.min_altitude = min_altitude
self.max_altitude = max_altitude
self.device_info = DeviceInfo(
configuration_url=URL,
identifiers={(DOMAIN, DEFAULT_NAME)},
Expand All @@ -57,6 +61,8 @@ async def _async_update_data(self):
)
current: dict[int, dict[str, Any]] = {}
for obj in flights:
if not self.min_altitude <= obj.altitude <= self.max_altitude:
continue
if self.tracked is not None and obj.id in self.tracked and self._is_valid(self.tracked[obj.id]):
flight = self.tracked[obj.id]
else:
Expand Down Expand Up @@ -88,6 +94,7 @@ async def _async_update_data(self):

def _handle_boundary(self, event: str, flights: list[dict[str, Any]]) -> None:
for flight in flights:
flight['tracked_by_device'] = self.config_entry.title
self.hass.bus.fire(event, flight)

@staticmethod
Expand Down Expand Up @@ -145,7 +152,7 @@ def _get_flight_data(flight: dict) -> dict[str, Any] | None:
['airport', 'origin', 'position',
'country', 'name']),
'airport_origin_country_code': FlightRadar24Coordinator._get_country_code(
FlightRadar24Coordinator._get_value(flight,['airport', 'origin', 'position', 'country', 'code'])),
FlightRadar24Coordinator._get_value(flight, ['airport', 'origin', 'position', 'country', 'code'])),
'airport_origin_city': FlightRadar24Coordinator._get_value(flight,
['airport', 'origin', 'position', 'region',
'city']),
Expand Down
2 changes: 1 addition & 1 deletion custom_components/flightradar24/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/AlexandrErohin/home-assistant-flightradar24/issues",
"requirements": ["FlightRadarAPI==1.3.12", "pycountry==23.12.11"],
"version": "1.4.0"
"version": "1.5.0"
}
2 changes: 2 additions & 0 deletions custom_components/flightradar24/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"latitude": "[%key:common::config_flow::data::latitude%]",
"longitude": "[%key:common::config_flow::data::longitude%]",
"scan_interval": "[%key:common::config_flow::data::scan_interval%]",
"min_altitude": "[%key:common::config_flow::data::min_altitude%]",
"max_altitude": "[%key:common::config_flow::data::max_altitude%]",
"username": "[%key:common::config_flow::data::username%]",
"password": "[%key:common::config_flow::data::password%]"
}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/flightradar24/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"latitude": "Breitengrad",
"longitude": "Längengrad",
"scan_interval": "Aktualisierungsintervall in Sekunden",
"min_altitude": "Die Mindesthöhe in Fuß, unter der das Flugzeug verfolgt wird.",
"max_altitude": "Die maximale Höhe in Fuß, über der das Flugzeug verfolgt wird.",
"username": "OPTIONAL! Flightradar24 username",
"password": "OPTIONAL! Flightradar24 password"
}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/flightradar24/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"latitude": "Latitude",
"longitude": "Longitude",
"scan_interval": "Scan interval for updates in seconds",
"min_altitude": "The minimum altitude in foots under which the aircraft will be tracked.",
"max_altitude": "The maximum altitude in foots above which the aircraft will be tracked.",
"username": "OPTIONAL! Flightradar24 username",
"password": "OPTIONAL! Flightradar24 password"
}
Expand Down

0 comments on commit e170cde

Please sign in to comment.