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 = [ PLATFORMS = [
Platform.SENSOR, Platform.SENSOR,
Platform.BINARY_SENSOR, Platform.BINARY_SENSOR,
# Platform.DEVICE_TRACKER, Platform.DEVICE_TRACKER,
Platform.SWITCH, Platform.SWITCH,
Platform.BUTTON, Platform.BUTTON,
Platform.UPDATE, Platform.UPDATE,

View file

@ -1,12 +1,22 @@
"""Support for the Mikrotik Router device tracker.""" """Support for the Mikrotik Router device tracker."""
import logging from __future__ import annotations
from typing import Any
from logging import getLogger
from collections.abc import Mapping from collections.abc import Mapping
from datetime import timedelta from datetime import timedelta
from typing import Any
from homeassistant.components.device_tracker.config_entry import ScannerEntity 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.const import STATE_NOT_HOME
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utcnow 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 .helper import format_attribute
from .const import ( from .const import (
CONF_TRACK_HOSTS, CONF_TRACK_HOSTS,
@ -14,29 +24,24 @@ from .const import (
CONF_TRACK_HOSTS_TIMEOUT, CONF_TRACK_HOSTS_TIMEOUT,
DEFAULT_TRACK_HOST_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_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""" """Set up entry for component"""
dispatcher = { dispatcher = {
"MikrotikDeviceTracker": MikrotikDeviceTracker, "MikrotikDeviceTracker": MikrotikDeviceTracker,
"MikrotikHostDeviceTracker": MikrotikHostDeviceTracker, "MikrotikHostDeviceTracker": MikrotikHostDeviceTracker,
} }
await model_async_setup_entry( await async_add_entities(hass, config_entry, dispatcher)
hass,
config_entry,
async_add_entities,
SENSOR_SERVICES,
SENSOR_TYPES,
dispatcher,
)
# --------------------------- # ---------------------------
@ -74,7 +79,7 @@ class MikrotikDeviceTracker(MikrotikEntity, ScannerEntity):
@property @property
def source_type(self) -> str: def source_type(self) -> str:
"""Return the source type of the port.""" """Return the source type of the port."""
return SOURCE_TYPE_ROUTER return SourceType.ROUTER
# --------------------------- # ---------------------------
@ -136,7 +141,7 @@ class MikrotikHostDeviceTracker(MikrotikDeviceTracker):
@property @property
def state(self) -> str: def state(self) -> str:
"""Return the state of the device.""" """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 @property
def extra_state_attributes(self) -> Mapping[str, Any]: def extra_state_attributes(self) -> Mapping[str, Any]:

View file

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