2019-04-03 15:40:03 +00:00
|
|
|
"""Sensor for the Open Sky Network."""
|
2022-01-03 18:10:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-04-20 05:56:20 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2023-08-10 13:42:46 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2023-07-25 18:46:04 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2023-08-10 14:04:00 +00:00
|
|
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, CONF_RADIUS
|
2023-07-25 18:46:04 +00:00
|
|
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
2019-12-09 13:26:53 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2023-08-11 08:11:13 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-07-25 18:46:04 +00:00
|
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2023-08-10 13:42:46 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2017-04-20 05:56:20 +00:00
|
|
|
|
2023-08-10 14:04:00 +00:00
|
|
|
from .const import CONF_ALTITUDE, DEFAULT_ALTITUDE, DOMAIN, MANUFACTURER
|
2023-08-10 13:42:46 +00:00
|
|
|
from .coordinator import OpenSkyDataUpdateCoordinator
|
2023-07-25 18:46:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_RADIUS): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Inclusive(CONF_LATITUDE, "coordinates"): cv.latitude,
|
|
|
|
vol.Inclusive(CONF_LONGITUDE, "coordinates"): cv.longitude,
|
|
|
|
vol.Optional(CONF_ALTITUDE, default=DEFAULT_ALTITUDE): vol.Coerce(float),
|
|
|
|
}
|
|
|
|
)
|
2017-04-20 05:56:20 +00:00
|
|
|
|
|
|
|
|
2023-07-25 18:46:04 +00:00
|
|
|
async def async_setup_platform(
|
2022-01-03 18:10:57 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2023-07-25 18:46:04 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 18:10:57 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2023-07-25 18:46:04 +00:00
|
|
|
"""Set up the OpenSky sensor platform from yaml."""
|
|
|
|
|
|
|
|
async_create_issue(
|
|
|
|
hass,
|
|
|
|
HOMEASSISTANT_DOMAIN,
|
|
|
|
f"deprecated_yaml_{DOMAIN}",
|
|
|
|
breaks_in_ha_version="2024.1.0",
|
|
|
|
is_fixable=False,
|
|
|
|
issue_domain=DOMAIN,
|
|
|
|
severity=IssueSeverity.WARNING,
|
|
|
|
translation_key="deprecated_yaml",
|
|
|
|
translation_placeholders={
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"integration_title": "OpenSky",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the entries."""
|
|
|
|
|
2023-08-10 13:42:46 +00:00
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
2023-07-25 18:46:04 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
OpenSkySensor(
|
2023-08-10 13:42:46 +00:00
|
|
|
coordinator,
|
|
|
|
entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2017-04-20 05:56:20 +00:00
|
|
|
|
|
|
|
|
2023-08-10 13:42:46 +00:00
|
|
|
class OpenSkySensor(CoordinatorEntity[OpenSkyDataUpdateCoordinator], SensorEntity):
|
2017-04-20 05:56:20 +00:00
|
|
|
"""Open Sky Network Sensor."""
|
|
|
|
|
2022-10-18 10:49:59 +00:00
|
|
|
_attr_attribution = (
|
|
|
|
"Information provided by the OpenSky Network (https://opensky-network.org)"
|
|
|
|
)
|
2023-08-10 13:42:46 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
_attr_name = None
|
|
|
|
_attr_icon = "mdi:airplane"
|
|
|
|
_attr_native_unit_of_measurement = "flights"
|
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2022-10-18 10:49:59 +00:00
|
|
|
|
2023-05-24 10:48:55 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-08-10 13:42:46 +00:00
|
|
|
coordinator: OpenSkyDataUpdateCoordinator,
|
|
|
|
config_entry: ConfigEntry,
|
2023-05-24 10:48:55 +00:00
|
|
|
) -> None:
|
2017-04-20 05:56:20 +00:00
|
|
|
"""Initialize the sensor."""
|
2023-08-10 13:42:46 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_unique_id = f"{config_entry.entry_id}_opensky"
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, f"{coordinator.config_entry.entry_id}")},
|
|
|
|
manufacturer=MANUFACTURER,
|
|
|
|
name=config_entry.title,
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
)
|
2017-04-20 05:56:20 +00:00
|
|
|
|
|
|
|
@property
|
2023-05-28 00:58:04 +00:00
|
|
|
def native_value(self) -> int:
|
2017-04-20 05:56:20 +00:00
|
|
|
"""Return the state of the sensor."""
|
2023-08-10 13:42:46 +00:00
|
|
|
return self.coordinator.data
|