Explicitly pass in the config_entry in mikrotik coordinator (#138089)

explicitly pass in the config_entry in coordinator
This commit is contained in:
Michael 2025-02-09 17:33:26 +01:00 committed by GitHub
parent e1c222c54e
commit de19f8550f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 13 deletions

View File

@ -1,19 +1,16 @@
"""The Mikrotik component.""" """The Mikrotik component."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from .const import ATTR_MANUFACTURER, DOMAIN from .const import ATTR_MANUFACTURER, DOMAIN
from .coordinator import MikrotikDataUpdateCoordinator, get_api from .coordinator import MikrotikConfigEntry, MikrotikDataUpdateCoordinator, get_api
from .errors import CannotConnect, LoginError from .errors import CannotConnect, LoginError
PLATFORMS = [Platform.DEVICE_TRACKER] PLATFORMS = [Platform.DEVICE_TRACKER]
type MikrotikConfigEntry = ConfigEntry[MikrotikDataUpdateCoordinator]
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, config_entry: MikrotikConfigEntry hass: HomeAssistant, config_entry: MikrotikConfigEntry
@ -47,6 +44,8 @@ async def async_setup_entry(
return True return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_unload_entry(
hass: HomeAssistant, config_entry: MikrotikConfigEntry
) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)

View File

@ -45,6 +45,8 @@ from .errors import CannotConnect, LoginError
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type MikrotikConfigEntry = ConfigEntry[MikrotikDataUpdateCoordinator]
class MikrotikData: class MikrotikData:
"""Handle all communication with the Mikrotik API.""" """Handle all communication with the Mikrotik API."""
@ -246,17 +248,21 @@ class MikrotikData:
class MikrotikDataUpdateCoordinator(DataUpdateCoordinator[None]): class MikrotikDataUpdateCoordinator(DataUpdateCoordinator[None]):
"""Mikrotik Hub Object.""" """Mikrotik Hub Object."""
config_entry: MikrotikConfigEntry
def __init__( def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, api: librouteros.Api self,
hass: HomeAssistant,
config_entry: MikrotikConfigEntry,
api: librouteros.Api,
) -> None: ) -> None:
"""Initialize the Mikrotik Client.""" """Initialize the Mikrotik Client."""
self.hass = hass self._mk_data = MikrotikData(hass, config_entry, api)
self.config_entry: ConfigEntry = config_entry
self._mk_data = MikrotikData(self.hass, self.config_entry, api)
super().__init__( super().__init__(
self.hass, hass,
_LOGGER, _LOGGER,
name=f"{DOMAIN} - {self.host}", config_entry=config_entry,
name=f"{DOMAIN} - {config_entry.data[CONF_HOST]}",
update_interval=timedelta(seconds=10), update_interval=timedelta(seconds=10),
) )

View File

@ -14,8 +14,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import MikrotikConfigEntry from .coordinator import Device, MikrotikConfigEntry, MikrotikDataUpdateCoordinator
from .coordinator import Device, MikrotikDataUpdateCoordinator
async def async_setup_entry( async def async_setup_entry(