Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ def get_presence(self):
Returns:
bool: True if device is present, False if not
"""
presence_sysfs = f'/sys/module/sx_core/asic0/module{self.sdk_index}/hw_present' if self.is_sw_control() else f'/sys/module/sx_core/asic0/module{self.sdk_index}/present'
if utils.read_int_from_file(presence_sysfs) != 1:
return False
eeprom_raw = self._read_eeprom(0, 1, log_on_error=False)
return eeprom_raw is not None

Expand Down
6 changes: 6 additions & 0 deletions platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ def get_temperature(self):
A float number of current temperature in Celsius up to nearest thousandth
of one degree Celsius, e.g. 30.125
"""
if not self.sfp.get_presence():
return None
value = self.sfp.get_temperature()
return value if (value != 0.0 and value is not None) else None

Expand All @@ -446,6 +448,8 @@ def get_high_threshold(self):
A float number, the high threshold temperature of thermal in Celsius
up to nearest thousandth of one degree Celsius, e.g. 30.125
"""
if not self.sfp.get_presence():
return None
value = self.sfp.get_temperature_warning_threshold()
return value if (value != 0.0 and value is not None) else None

Expand All @@ -457,6 +461,8 @@ def get_high_critical_threshold(self):
A float number, the high critical threshold temperature of thermal in Celsius
up to nearest thousandth of one degree Celsius, e.g. 30.125
"""
if not self.sfp.get_presence():
return None
value = self.sfp.get_temperature_critical_threshold()
return value if (value != 0.0 and value is not None) else None

Expand Down
12 changes: 10 additions & 2 deletions platform/mellanox/mlnx-platform-api/tests/test_sfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,23 @@ def test_get_page_and_page_offset(self, mock_get_type_str, mock_eeprom_path, moc
assert page == '/tmp/1/data'
assert page_offset is 0

@mock.patch('sonic_platform.utils.read_int_from_file')
@mock.patch('sonic_platform.sfp.SFP._read_eeprom')
def test_sfp_get_presence(self, mock_read):
def test_sfp_get_presence(self, mock_read, mock_read_int):
sfp = SFP(0)

mock_read_int.return_value = 1
mock_read.return_value = None
assert not sfp.get_presence()

mock_read.return_value = 0
assert sfp.get_presence()

mock_read_int.return_value = 0
mock_read.return_value = None
assert not sfp.get_presence()
mock_read.return_value = 0
assert not sfp.get_presence()

@mock.patch('sonic_platform.utils.read_int_from_file')
def test_rj45_get_presence(self, mock_read_int):
sfp = RJ45Port(0)
Expand Down
9 changes: 9 additions & 0 deletions platform/mellanox/mlnx-platform-api/tests/test_thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,21 @@ def test_sfp_thermal(self):
assert thermal.get_name() == rule['name'].format(start_index)
assert thermal.get_position_in_parent() == 1
assert thermal.is_replaceable() == False
sfp.get_presence = mock.MagicMock(return_value=True)
sfp.get_temperature = mock.MagicMock(return_value=35.4)
sfp.get_temperature_warning_threshold = mock.MagicMock(return_value=70)
sfp.get_temperature_critical_threshold = mock.MagicMock(return_value=80)
assert thermal.get_temperature() == 35.4
assert thermal.get_high_threshold() == 70
assert thermal.get_high_critical_threshold() == 80
sfp.get_presence = mock.MagicMock(return_value=False)
sfp.get_temperature = mock.MagicMock(return_value=35.4)
sfp.get_temperature_warning_threshold = mock.MagicMock(return_value=70)
sfp.get_temperature_critical_threshold = mock.MagicMock(return_value=80)
assert thermal.get_temperature() is None
assert thermal.get_high_threshold() is None
assert thermal.get_high_critical_threshold() is None
sfp.get_presence = mock.MagicMock(return_value=True)
sfp.get_temperature = mock.MagicMock(return_value=0)
sfp.get_temperature_warning_threshold = mock.MagicMock(return_value=0)
sfp.get_temperature_critical_threshold = mock.MagicMock(return_value=None)
Expand Down