mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
146 lines
4.7 KiB
Python
146 lines
4.7 KiB
Python
"""Support for UniFi AP Direct access as device tracker using Coordinator."""
|
|
|
|
from typing import override
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.device_tracker import (
|
|
PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
|
|
AsyncSeeCallback,
|
|
ScannerEntity,
|
|
)
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
from homeassistant.helpers import config_validation as cv, issue_registry as ir
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from .const import DEFAULT_SSH_PORT, DOMAIN
|
|
from .coordinator import UniFiDirectConfigEntry, UniFiDirectDataUpdateCoordinator
|
|
|
|
PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
|
|
{
|
|
vol.Required(CONF_HOST): cv.string,
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
vol.Optional(CONF_PORT, default=DEFAULT_SSH_PORT): cv.port,
|
|
}
|
|
)
|
|
|
|
|
|
async def async_setup_scanner(
|
|
hass: HomeAssistant,
|
|
config: ConfigType,
|
|
_async_see: AsyncSeeCallback,
|
|
_discovery_info: DiscoveryInfoType | None = None,
|
|
) -> bool:
|
|
"""Set up the legacy UniFi AP Direct device tracker."""
|
|
import_data = {
|
|
CONF_HOST: config[CONF_HOST],
|
|
CONF_USERNAME: config[CONF_USERNAME],
|
|
CONF_PASSWORD: config[CONF_PASSWORD],
|
|
CONF_PORT: config.get(CONF_PORT, DEFAULT_SSH_PORT),
|
|
}
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_IMPORT},
|
|
data=import_data,
|
|
)
|
|
|
|
if result["type"] is FlowResultType.ABORT and result["reason"] == "cannot_connect":
|
|
ir.async_create_issue(
|
|
hass,
|
|
DOMAIN,
|
|
"yaml_import_cannot_connect",
|
|
is_fixable=False,
|
|
issue_domain=DOMAIN,
|
|
severity=ir.IssueSeverity.ERROR,
|
|
translation_key="yaml_import_cannot_connect",
|
|
translation_placeholders={"host": config[CONF_HOST]},
|
|
)
|
|
return False
|
|
|
|
ir.async_create_issue(
|
|
hass,
|
|
HOMEASSISTANT_DOMAIN,
|
|
f"deprecated_yaml_{DOMAIN}",
|
|
is_fixable=False,
|
|
issue_domain=DOMAIN,
|
|
severity=ir.IssueSeverity.WARNING,
|
|
translation_key="deprecated_yaml",
|
|
translation_placeholders={
|
|
"domain": DOMAIN,
|
|
"integration_title": "UniFi AP",
|
|
},
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: UniFiDirectConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up device tracker for UniFi AP Direct."""
|
|
coordinator = config_entry.runtime_data
|
|
tracked: set[str] = set()
|
|
|
|
@callback
|
|
def _async_update_devices() -> None:
|
|
"""Add new devices from the coordinator."""
|
|
new_entities: list[UniFiScannerEntity] = []
|
|
for mac in coordinator.data:
|
|
if mac not in tracked:
|
|
tracked.add(mac)
|
|
new_entities.append(UniFiScannerEntity(coordinator, mac))
|
|
if new_entities:
|
|
async_add_entities(new_entities)
|
|
|
|
config_entry.async_on_unload(coordinator.async_add_listener(_async_update_devices))
|
|
_async_update_devices()
|
|
|
|
|
|
class UniFiScannerEntity(
|
|
CoordinatorEntity[UniFiDirectDataUpdateCoordinator], ScannerEntity
|
|
):
|
|
"""Representation of a device connected to a UniFi AP Direct."""
|
|
|
|
def __init__(self, coordinator: UniFiDirectDataUpdateCoordinator, mac: str) -> None:
|
|
"""Initialize the tracked device."""
|
|
super().__init__(coordinator)
|
|
self._mac = mac
|
|
device = coordinator.data.get(mac, {})
|
|
self._attr_name = device.get("hostname") or mac
|
|
|
|
@property
|
|
@override
|
|
def is_connected(self) -> bool:
|
|
"""Return true if the device is connected to the AP."""
|
|
return self._mac in self.coordinator.data
|
|
|
|
@property
|
|
@override
|
|
def mac_address(self) -> str:
|
|
"""Return the MAC address of the device."""
|
|
return self._mac
|
|
|
|
@property
|
|
@override
|
|
def ip_address(self) -> str | None:
|
|
"""Return the IP address of the device."""
|
|
if device := self.coordinator.data.get(self._mac):
|
|
return device.get("ip")
|
|
return None
|
|
|
|
@property
|
|
@override
|
|
def hostname(self) -> str | None:
|
|
"""Return the hostname of the device."""
|
|
if device := self.coordinator.data.get(self._mac):
|
|
return device.get("hostname")
|
|
return None
|