Fix CPUSpeed with missing info (#66339)

pull/66349/head
Franck Nijhof 2022-02-11 19:38:55 +01:00 committed by GitHub
parent 8ff987d90c
commit 2f220b27d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -81,8 +81,8 @@ class CPUSpeedSensor(SensorEntity):
if info:
self._attr_extra_state_attributes = {
ATTR_ARCH: info["arch_string_raw"],
ATTR_BRAND: info["brand_raw"],
ATTR_ARCH: info.get("arch_string_raw"),
ATTR_BRAND: info.get("brand_raw"),
}
if HZ_ADVERTISED in info:
self._attr_extra_state_attributes[ATTR_HZ] = round(

View File

@ -61,3 +61,25 @@ async def test_sensor(
assert state.attributes.get(ATTR_ARCH) == "aargh"
assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7"
assert state.attributes.get(ATTR_HZ) == 3.6
async def test_sensor_partial_info(
hass: HomeAssistant,
mock_cpuinfo: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the CPU Speed sensor missing info."""
mock_config_entry.add_to_hass(hass)
# Pop some info from the mocked CPUSpeed
mock_cpuinfo.return_value.pop("brand_raw")
mock_cpuinfo.return_value.pop("arch_string_raw")
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.cpu_speed")
assert state
assert state.state == "3.2"
assert state.attributes.get(ATTR_ARCH) is None
assert state.attributes.get(ATTR_BRAND) is None