mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 18:08:37 -04:00
Add temperature sensors for unifi device (#122518)
* Add temperature sensors for device * Move to single line * Use right reference * Always return a value * Update tests * Use slugify for id name * Return default value if not present * Make _device_temperature return value * Add default value if temperatures is None * Set value to go over all code paths * Add test for no matching temperatures * make first part deterministic
This commit is contained in:
@@ -1519,3 +1519,165 @@ async def test_wan_monitor_latency_with_no_uptime(
|
||||
|
||||
latency_entry = entity_registry.async_get("sensor.mock_name_google_wan_latency")
|
||||
assert latency_entry is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_payload",
|
||||
[
|
||||
[
|
||||
{
|
||||
"board_rev": 3,
|
||||
"device_id": "mock-id",
|
||||
"has_fan": True,
|
||||
"fan_level": 0,
|
||||
"ip": "10.0.1.1",
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"model": "US16P150",
|
||||
"name": "Device",
|
||||
"next_interval": 20,
|
||||
"overheating": True,
|
||||
"state": 1,
|
||||
"type": "usw",
|
||||
"upgradable": True,
|
||||
"uptime": 60,
|
||||
"version": "4.0.42.10433",
|
||||
"temperatures": [
|
||||
{"name": "CPU", "type": "cpu", "value": 66.0},
|
||||
{"name": "Local", "type": "board", "value": 48.75},
|
||||
{"name": "PHY", "type": "board", "value": 50.25},
|
||||
],
|
||||
}
|
||||
]
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("temperature_id", "state", "updated_state", "index_to_update"),
|
||||
[
|
||||
("device_cpu", "66.0", "20", 0),
|
||||
("device_local", "48.75", "90.64", 1),
|
||||
("device_phy", "50.25", "80", 2),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_device_temperatures(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_websocket_message,
|
||||
device_payload: list[dict[str, Any]],
|
||||
temperature_id: str,
|
||||
state: str,
|
||||
updated_state: str,
|
||||
index_to_update: int,
|
||||
) -> None:
|
||||
"""Verify that device temperatures sensors are working as expected."""
|
||||
|
||||
entity_id = f"sensor.device_{temperature_id}_temperature"
|
||||
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 2
|
||||
|
||||
temperature_entity = entity_registry.async_get(entity_id)
|
||||
assert temperature_entity.disabled_by == RegistryEntryDisabler.INTEGRATION
|
||||
|
||||
# Enable entity
|
||||
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(
|
||||
hass,
|
||||
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 7
|
||||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 3
|
||||
|
||||
# Verify sensor state
|
||||
assert hass.states.get(entity_id).state == state
|
||||
|
||||
# # Verify state update
|
||||
device = device_payload[0]
|
||||
device["temperatures"][index_to_update]["value"] = updated_state
|
||||
|
||||
mock_websocket_message(message=MessageKey.DEVICE, data=device)
|
||||
|
||||
assert hass.states.get(entity_id).state == updated_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_payload",
|
||||
[
|
||||
[
|
||||
{
|
||||
"board_rev": 2,
|
||||
"device_id": "mock-id",
|
||||
"ip": "10.0.1.1",
|
||||
"mac": "10:00:00:00:01:01",
|
||||
"last_seen": 1562600145,
|
||||
"model": "US16P150",
|
||||
"name": "mock-name",
|
||||
"port_overrides": [],
|
||||
"state": 1,
|
||||
"type": "usw",
|
||||
"version": "4.0.42.10433",
|
||||
}
|
||||
]
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_device_with_no_temperature(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Verify that device temperature sensors is not created if there is no data."""
|
||||
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 2
|
||||
|
||||
temperature_entity = entity_registry.async_get(
|
||||
"sensor.device_device_cpu_temperature"
|
||||
)
|
||||
|
||||
assert temperature_entity is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_payload",
|
||||
[
|
||||
[
|
||||
{
|
||||
"board_rev": 2,
|
||||
"device_id": "mock-id",
|
||||
"ip": "10.0.1.1",
|
||||
"mac": "10:00:00:00:01:01",
|
||||
"last_seen": 1562600145,
|
||||
"model": "US16P150",
|
||||
"name": "mock-name",
|
||||
"port_overrides": [],
|
||||
"state": 1,
|
||||
"type": "usw",
|
||||
"version": "4.0.42.10433",
|
||||
"temperatures": [
|
||||
{"name": "MEM", "type": "mem", "value": 66.0},
|
||||
],
|
||||
}
|
||||
]
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_device_with_no_matching_temperatures(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Verify that device temperature sensors is not created if there is no matching data."""
|
||||
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 2
|
||||
|
||||
temperature_entity = entity_registry.async_get(
|
||||
"sensor.device_device_cpu_temperature"
|
||||
)
|
||||
|
||||
assert temperature_entity is None
|
||||
|
||||
Reference in New Issue
Block a user