Skip to content

Commit

Permalink
Improve on surrounding ac
Browse files Browse the repository at this point in the history
  • Loading branch information
vingerha committed Mar 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 250046c commit 689d367
Showing 2 changed files with 42 additions and 20 deletions.
14 changes: 7 additions & 7 deletions custom_components/adsb_lol/adsb_helper.py
Original file line number Diff line number Diff line change
@@ -23,18 +23,18 @@ def get_point_of_interest(self):
for ac in response.json()["ac"]:
# _LOGGER.debug ("Get ac in: %s", ac)
if self._altitude_limit == 0 or (self._altitude_limit > 0 and ac["alt_geom"] * 1000 / 0.3048 < self._altitude_limit):
aircraft["callsign"] = ac["flight"]
aircraft["registration"] = ac["r"]
self._reg = ac["r"]
aircraft["icao24"] = ac["hex"]
aircraft["type"] = ac["t"]
aircraft["callsign"] = ac.get("flight", None)
aircraft["registration"] = ac.get("r", None)
self._reg = ac.get("r", "NoReg")
aircraft["icao24"] = ac.get("hex", None)
aircraft["type"] = ac.get("t", None)
aircraft["groundspeed_nmph"] = ac.get("gs",None)
aircraft["true_airspeed_nmph"] = ac.get("tas",None)
aircraft["mach"] = ac.get("mach", None)
aircraft["altitude_baro_ft"] = ac.get("alt_baro", None)
aircraft["altitude_geom_ft"] = ac.get("alt_geom", None)
aircraft["latitude"] = ac["lat"]
aircraft["longitude"] = ac["lon"]
aircraft["latitude"] = ac.get("lat", None)
aircraft["longitude"] = ac.get("lon", None)
aircraft_h[str(self._reg)] = aircraft.copy()
# _LOGGER.debug ("Get ac append _h: %s", aircraft_h)
# _LOGGER.debug ("Get ac append: %s", aircraft)
48 changes: 35 additions & 13 deletions custom_components/adsb_lol/sensor.py
Original file line number Diff line number Diff line change
@@ -21,6 +21,12 @@

_LOGGER = logging.getLogger(__name__)

def get_current_registrations(flights) -> set[str]:
"""Return a list of present goals."""
result = set()
for key, value in flights.items():
result.add(key)
return result

async def async_setup_entry(
hass: HomeAssistant,
@@ -34,28 +40,48 @@ async def async_setup_entry(
"coordinator"
]
await coordinator.async_config_entry_first_refresh()
_LOGGER.debug("Incoming data: %s:", coordinator.data)
for key, value in coordinator.data.items():
_LOGGER.debug("Incoming data ac key: %s - value: %s", key, value)
sensors.append(
ADSBPointACSensor(value, coordinator, config_entry.data.get('device_tracker_id') )
)
sensors.append(
ADSBPointSensor(coordinator, config_entry.data.get('device_tracker_id') )
)

)
else:
coordinator: ADSBUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
coordinator_t: ADSBUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
"coordinator"
]
await coordinator.async_config_entry_first_refresh()
await coordinator_t.async_config_entry_first_refresh()

sensors.append(
ADSBFlightTrackerSensor(coordinator)
ADSBFlightTrackerSensor(coordinator_t)
)

async_add_entities(sensors, False)

# setup listening for new reg
current_registrations = get_current_registrations(coordinator.data)

def _async_registrations_listener() -> None:
"""Listen for new registrations and add sensors if they did not exist."""
received_registrations = get_current_registrations(coordinator.data)
new_registrations = received_registrations - current_registrations
_LOGGER.debug("New registrations: %s", new_registrations)
if new_registrations:
sensors = []
current_registrations.update(new_registrations)
for registration in new_registrations:
for key, value in coordinator.data.items():
if key == registration:
_LOGGER.debug("Listener: adding registration: %s", key)
sensors.append(
ADSBPointACSensor(value, coordinator, config_entry.data.get('device_tracker_id'))
)
async_add_entities(sensors, False)

coordinator.async_add_listener(_async_registrations_listener)

class ADSBFlightTrackerSensor(CoordinatorEntity, SensorEntity):
"""Implementation of a ADSB Flight tracker via registration departures sensor."""

@@ -92,8 +118,6 @@ def _update_attrs(self): # noqa: C901 PLR0911
self._state: str | None = None
self._state = self.coordinator.data["callsign"]
self._attr_native_value = self._state


self._attr_extra_state_attributes = self.coordinator.data
return self._attr_extra_state_attributes

@@ -147,8 +171,7 @@ class ADSBPointACSensor(CoordinatorEntity, SensorEntity):

def __init__(self, aircraft, coordinator, device_tracker_id) -> None:
"""Initialize the ADSB sensor."""
super().__init__(coordinator)
_LOGGER.debug("SENSOR incoming data AC Point: %s:", aircraft)
super().__init__(coordinator)
self._name = device_tracker_id + "_" + aircraft["registration"]
self._device_tracker_id = device_tracker_id
self._attributes: dict[str, Any] = {}
@@ -168,7 +191,7 @@ def __init__(self, aircraft, coordinator, device_tracker_id) -> None:
@property
def name(self) -> str:
"""Return the name of the sensor."""
return "flight_in_radius_" + str(self._name) + str(self._aircraft["registration"])
return "aircraft_radius_" + str(self._name)

@callback
def _handle_coordinator_update(self) -> None:
@@ -181,7 +204,6 @@ def _update_attrs(self): # noqa: C901 PLR0911
self._state: str | None = None
#dealing with updates form coordinator
for k,v in self.coordinator.data.items():
_LOGGER.debug("SENSOR Point AC Reg: %s - Key: %s - Value:%s", self._aircraft["registration"], k, v)
if self._aircraft["registration"] == k:
self._aircraft = v
else:

0 comments on commit 689d367

Please sign in to comment.