diff --git a/homeassistant/components/fritz/coordinator.py b/homeassistant/components/fritz/coordinator.py index 4d5d743560e..46acad68545 100644 --- a/homeassistant/components/fritz/coordinator.py +++ b/homeassistant/components/fritz/coordinator.py @@ -467,8 +467,7 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]): self._devices[dev_mac].update(dev_info, consider_home) return False - device = FritzDevice(dev_mac, dev_info.name) - device.update(dev_info, consider_home) + device = FritzDevice(dev_mac, dev_info, consider_home) self._devices[dev_mac] = device # manually register device entry for new connected device diff --git a/homeassistant/components/fritz/entity.py b/homeassistant/components/fritz/entity.py index ad2a4d831d0..ef662737ad8 100644 --- a/homeassistant/components/fritz/entity.py +++ b/homeassistant/components/fritz/entity.py @@ -33,11 +33,9 @@ class FritzDeviceBase(CoordinatorEntity[AvmWrapper]): ) @property - def ip_address(self) -> str | None: + def ip_address(self) -> str: """Return the primary ip address of the device.""" - if self._mac: - return self._avm_wrapper.devices[self._mac].ip_address - return None + return self._avm_wrapper.devices[self._mac].ip_address @property def mac_address(self) -> str: @@ -45,11 +43,9 @@ class FritzDeviceBase(CoordinatorEntity[AvmWrapper]): return self._mac @property - def hostname(self) -> str | None: + def hostname(self) -> str: """Return hostname of the device.""" - if self._mac: - return self._avm_wrapper.devices[self._mac].hostname - return None + return self._avm_wrapper.devices[self._mac].hostname async def async_process_update(self) -> None: """Update device.""" diff --git a/homeassistant/components/fritz/models.py b/homeassistant/components/fritz/models.py index 9d917f4ac16..83bb790dc58 100644 --- a/homeassistant/components/fritz/models.py +++ b/homeassistant/components/fritz/models.py @@ -77,17 +77,21 @@ class HostInfo(TypedDict): class FritzDevice: """Representation of a device connected to the FRITZ!Box.""" - def __init__(self, mac: str, name: str) -> None: + _connected: bool + _connected_to: str + _connection_type: str + _ip_address: str + _last_activity: datetime | None + _mac: str + _name: str + _ssid: str | None + _wan_access: bool | None + + def __init__(self, mac: str, dev_info: Device, consider_home: float) -> None: """Initialize device info.""" - self._connected = False - self._connected_to: str | None = None - self._connection_type: str | None = None - self._ip_address: str | None = None - self._last_activity: datetime | None = None self._mac = mac - self._name = name - self._ssid: str | None = None - self._wan_access: bool | None = False + self._last_activity = None + self.update(dev_info, consider_home) def update(self, dev_info: Device, consider_home: float) -> None: """Update device info.""" @@ -100,8 +104,7 @@ class FritzDevice: else: consider_home_evaluated = dev_info.connected - if not self._name: - self._name = dev_info.name or self._mac.replace(":", "_") + self._name = dev_info.name or self._mac.replace(":", "_") self._connected = dev_info.connected or consider_home_evaluated @@ -115,12 +118,12 @@ class FritzDevice: self._wan_access = dev_info.wan_access @property - def connected_to(self) -> str | None: + def connected_to(self) -> str: """Return connected status.""" return self._connected_to @property - def connection_type(self) -> str | None: + def connection_type(self) -> str: """Return connected status.""" return self._connection_type @@ -140,7 +143,7 @@ class FritzDevice: return self._name @property - def ip_address(self) -> str | None: + def ip_address(self) -> str: """Get IP address.""" return self._ip_address diff --git a/homeassistant/components/fritz/switch.py b/homeassistant/components/fritz/switch.py index a452c4821bd..09e473d868a 100644 --- a/homeassistant/components/fritz/switch.py +++ b/homeassistant/components/fritz/switch.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging -from typing import TYPE_CHECKING, Any +from typing import Any from homeassistant.components.network import async_get_source_ip from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription @@ -524,8 +524,6 @@ class FritzBoxProfileSwitch(FritzDeviceBase, SwitchEntity): async def _async_handle_turn_on_off(self, turn_on: bool) -> bool: """Handle switch state change request.""" - if TYPE_CHECKING: - assert self.ip_address await self._avm_wrapper.async_set_allow_wan_access(self.ip_address, turn_on) self._avm_wrapper.devices[self._mac].wan_access = turn_on self.async_write_ha_state()