From e170cde4832425a99d08985a8470c95bf05bee26 Mon Sep 17 00:00:00 2001 From: Alex Erohin Date: Thu, 22 Feb 2024 14:28:03 +0300 Subject: [PATCH] Added tracked_by_device to events and flight filter by altitude --- README.md | 23 ++++++++++++++---- custom_components/flightradar24/__init__.py | 10 +++++++- .../flightradar24/config_flow.py | 24 ++++++++++++------- custom_components/flightradar24/const.py | 6 +++++ .../flightradar24/coordinator.py | 11 +++++++-- custom_components/flightradar24/manifest.json | 2 +- custom_components/flightradar24/strings.json | 2 ++ .../flightradar24/translations/de.json | 2 ++ .../flightradar24/translations/en.json | 2 ++ 9 files changed, 66 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index daaac8b..9bba9ed 100644 --- a/README.md +++ b/README.md @@ -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 Settings->Devices & services. 2. Search for `Flightradar24`, and click on it. @@ -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 +#### `trigger.event.data.tracked_by_device` + +To change name in tracked_by_device +1. Go to the Settings->Devices & services. +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` + ### Lovelace Card You can add flight table to your [Home Assistant dashboard](https://www.home-assistant.io/dashboards/) @@ -178,6 +192,7 @@ recorder: ## Flight fields | 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 | diff --git a/custom_components/flightradar24/__init__.py b/custom_components/flightradar24/__init__.py index 3749f2e..25249a5 100644 --- a/custom_components/flightradar24/__init__.py +++ b/custom_components/flightradar24/__init__.py @@ -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 @@ -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() diff --git a/custom_components/flightradar24/config_flow.py b/custom_components/flightradar24/config_flow.py index 2d6343b..ca0ac5b 100644 --- a/custom_components/flightradar24/config_flow.py +++ b/custom_components/flightradar24/config_flow.py @@ -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 @@ -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 @@ -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) diff --git a/custom_components/flightradar24/const.py b/custom_components/flightradar24/const.py index 1bb23fb..5724c5b 100644 --- a/custom_components/flightradar24/const.py +++ b/custom_components/flightradar24/const.py @@ -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 diff --git a/custom_components/flightradar24/coordinator.py b/custom_components/flightradar24/coordinator.py index bb68061..86787eb 100644 --- a/custom_components/flightradar24/coordinator.py +++ b/custom_components/flightradar24/coordinator.py @@ -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 @@ -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)}, @@ -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: @@ -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 @@ -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']), diff --git a/custom_components/flightradar24/manifest.json b/custom_components/flightradar24/manifest.json index d13a973..aac9c4f 100644 --- a/custom_components/flightradar24/manifest.json +++ b/custom_components/flightradar24/manifest.json @@ -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" } diff --git a/custom_components/flightradar24/strings.json b/custom_components/flightradar24/strings.json index f9b8e31..5d97e31 100644 --- a/custom_components/flightradar24/strings.json +++ b/custom_components/flightradar24/strings.json @@ -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%]" } diff --git a/custom_components/flightradar24/translations/de.json b/custom_components/flightradar24/translations/de.json index 4bef558..42dc706 100644 --- a/custom_components/flightradar24/translations/de.json +++ b/custom_components/flightradar24/translations/de.json @@ -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" } diff --git a/custom_components/flightradar24/translations/en.json b/custom_components/flightradar24/translations/en.json index 6bb0ad1..cb4cad1 100644 --- a/custom_components/flightradar24/translations/en.json +++ b/custom_components/flightradar24/translations/en.json @@ -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" }