converted device tracker

This commit is contained in:
Tomaae 2023-08-09 10:19:04 +02:00
parent 514a930d04
commit b8f4856a5b
No known key found for this signature in database
GPG key ID: 2F97770867DAA4E6
3 changed files with 40 additions and 32 deletions

View file

@ -4,7 +4,7 @@ from homeassistant.const import Platform
PLATFORMS = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
# Platform.DEVICE_TRACKER,
Platform.DEVICE_TRACKER,
Platform.SWITCH,
Platform.BUTTON,
Platform.UPDATE,

View file

@ -1,12 +1,22 @@
"""Support for the Mikrotik Router device tracker."""
import logging
from typing import Any
from __future__ import annotations
from logging import getLogger
from collections.abc import Mapping
from datetime import timedelta
from typing import Any
from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.const import STATE_NOT_HOME
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utcnow
from homeassistant.components.device_tracker.const import SourceType
from .device_tracker_types import SENSOR_TYPES, SENSOR_SERVICES
from .entity import MikrotikEntity, async_add_entities
from .helper import format_attribute
from .const import (
CONF_TRACK_HOSTS,
@ -14,29 +24,24 @@ from .const import (
CONF_TRACK_HOSTS_TIMEOUT,
DEFAULT_TRACK_HOST_TIMEOUT,
)
from .entity import model_async_setup_entry, MikrotikEntity
from .device_tracker_types import SENSOR_TYPES, SENSOR_SERVICES
_LOGGER = logging.getLogger(__name__)
_LOGGER = getLogger(__name__)
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
_async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry for component"""
dispatcher = {
"MikrotikDeviceTracker": MikrotikDeviceTracker,
"MikrotikHostDeviceTracker": MikrotikHostDeviceTracker,
}
await model_async_setup_entry(
hass,
config_entry,
async_add_entities,
SENSOR_SERVICES,
SENSOR_TYPES,
dispatcher,
)
await async_add_entities(hass, config_entry, dispatcher)
# ---------------------------
@ -74,7 +79,7 @@ class MikrotikDeviceTracker(MikrotikEntity, ScannerEntity):
@property
def source_type(self) -> str:
"""Return the source type of the port."""
return SOURCE_TYPE_ROUTER
return SourceType.ROUTER
# ---------------------------
@ -136,7 +141,7 @@ class MikrotikHostDeviceTracker(MikrotikDeviceTracker):
@property
def state(self) -> str:
"""Return the state of the device."""
return self._ctrl.option_zone if self.is_connected else STATE_NOT_HOME
return self.coordinator.option_zone if self.is_connected else STATE_NOT_HOME
@property
def extra_state_attributes(self) -> Mapping[str, Any]:

View file

@ -1,6 +1,9 @@
"""Definitions for Mikrotik Router device tracker entities."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import List
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.components.switch import (
SwitchEntityDescription,
@ -19,25 +22,25 @@ DEVICE_ATTRIBUTES_HOST = [
class MikrotikDeviceTrackerEntityDescription(SwitchEntityDescription):
"""Class describing mikrotik entities."""
key: str = ""
name: str = ""
key: str | None = None
name: str | None = None
device_class = None
icon_enabled: str = ""
icon_disabled: str = ""
ha_group: str = ""
ha_connection: str = ""
ha_connection_value: str = ""
data_path: str = ""
icon_enabled: str | None = None
icon_disabled: str | None = None
ha_group: str | None = None
ha_connection: str | None = None
ha_connection_value: str | None = None
data_path: str | None = None
data_attribute: str = "available"
data_name: str = ""
data_uid: str = ""
data_reference: str = ""
data_name: str | None = None
data_uid: str | None = None
data_reference: str | None = None
data_attributes_list: List = field(default_factory=lambda: [])
func: str = "MikrotikDeviceTracker"
SENSOR_TYPES = {
"host": MikrotikDeviceTrackerEntityDescription(
SENSOR_TYPES: tuple[MikrotikDeviceTrackerEntityDescription, ...] = (
MikrotikDeviceTrackerEntityDescription(
key="host",
name="",
icon_enabled="mdi:lan-connect",
@ -52,6 +55,6 @@ SENSOR_TYPES = {
data_attributes_list=DEVICE_ATTRIBUTES_HOST,
func="MikrotikHostDeviceTracker",
),
}
)
SENSOR_SERVICES = {}