diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index b2dacc27327..e20f4703ab8 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -7,6 +7,7 @@ import contextlib from dataclasses import dataclass from datetime import datetime from functools import lru_cache +import ipaddress import logging import socket import sys @@ -136,6 +137,11 @@ def get_ip_address( if entity.argument in addresses: for addr in addresses[entity.argument]: if addr.family == IF_ADDRS_FAMILY[entity.entity_description.key]: + address = ipaddress.ip_address(addr.address) + if address.version == 6 and ( + address.is_link_local or address.is_loopback + ): + continue return addr.address return None diff --git a/tests/components/systemmonitor/conftest.py b/tests/components/systemmonitor/conftest.py index 3994b1cdfba..e6e1b35e66d 100644 --- a/tests/components/systemmonitor/conftest.py +++ b/tests/components/systemmonitor/conftest.py @@ -129,7 +129,21 @@ def mock_psutil(mock_process: list[MockProcess]) -> Generator: "255.255.255.0", "255.255.255.255", None, - ) + ), + snicaddr( + socket.AF_INET6, + "fe80::baf2:8a90:4f78:b1cb%end0", + "ffff:ffff:ffff:ffff::", + None, + None, + ), + snicaddr( + socket.AF_INET6, + "2a00:1f:2103:3a01:3333:2222:1111:0000", + "ffff:ffff:ffff:ffff::", + None, + None, + ), ], "eth1": [ snicaddr( diff --git a/tests/components/systemmonitor/snapshots/test_diagnostics.ambr b/tests/components/systemmonitor/snapshots/test_diagnostics.ambr index 883c90d9d5b..b50e051c816 100644 --- a/tests/components/systemmonitor/snapshots/test_diagnostics.ambr +++ b/tests/components/systemmonitor/snapshots/test_diagnostics.ambr @@ -4,7 +4,7 @@ 'coordinators': dict({ 'data': dict({ 'addresses': dict({ - 'eth0': "[snicaddr(family=, address='192.168.1.1', netmask='255.255.255.0', broadcast='255.255.255.255', ptp=None)]", + 'eth0': "[snicaddr(family=, address='192.168.1.1', netmask='255.255.255.0', broadcast='255.255.255.255', ptp=None), snicaddr(family=, address='fe80::baf2:8a90:4f78:b1cb%end0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snicaddr(family=, address='2a00:1f:2103:3a01:3333:2222:1111:0000', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None)]", 'eth1': "[snicaddr(family=, address='192.168.10.1', netmask='255.255.255.0', broadcast='255.255.255.255', ptp=None)]", 'vethxyzxyz': "[snicaddr(family=, address='172.16.10.1', netmask='255.255.255.0', broadcast='255.255.255.255', ptp=None)]", }), diff --git a/tests/components/systemmonitor/snapshots/test_sensor.ambr b/tests/components/systemmonitor/snapshots/test_sensor.ambr index 5374ea886d3..3fe9ae7e809 100644 --- a/tests/components/systemmonitor/snapshots/test_sensor.ambr +++ b/tests/components/systemmonitor/snapshots/test_sensor.ambr @@ -95,7 +95,7 @@ }) # --- # name: test_sensor[System Monitor IPv6 address eth0 - state] - 'unknown' + '2a00:1f:2103:3a01:3333:2222:1111:0000' # --- # name: test_sensor[System Monitor IPv6 address eth1 - attributes] ReadOnlyDict({