Skip to content

Commit

Permalink
Implement funny rating
Browse files Browse the repository at this point in the history
This will show the user a funny rating based on their
measured download speed and ping.

Co-authored-by: AntonRidderby <AntonRidderby@users.noreply.github.com>
  • Loading branch information
SimonWargh and AntonRidderby committed Oct 24, 2023
1 parent 554ab94 commit 1753462
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
34 changes: 33 additions & 1 deletion homeassistant/components/speedtestdotnet/coordinator.py
Expand Up @@ -69,7 +69,39 @@ def update_data(self) -> dict[str, Any]:
)
self.api.download()
self.api.upload()
return cast(dict[str, Any], self.api.results.dict())
result_dict = cast(dict[str, Any], self.api.results.dict())
result_dict["funny_rating"] = self.generate_funny_rating(
round(result_dict["download"] / 10**6, 2),
result_dict["ping"],
)
return result_dict

def generate_funny_rating(self, download: float, ping: float) -> str:
"""Generate a funny rating based on download speed."""

funny_rating = ""
if ping > 200:
funny_rating = "Every online shooter's worst nightmare."
elif download < 1:
funny_rating = "Literal potato speeds."
elif download < 10:
funny_rating = "Kinda trashy."
elif download < 50:
funny_rating = "Granny dial-up internet."
elif download < 100:
funny_rating = "Nothing special, really"
elif download < 250:
funny_rating = "Just above average. Bet you feel special."
elif download < 500:
funny_rating = "You're probably paying too much."
elif download < 750:
funny_rating = "Is it bring your HomeAssistant to work day?"
elif download <= 1000:
funny_rating = "Dude. Stop."
elif download > 1000:
funny_rating = "Absolutely God-tier."

return funny_rating

async def _async_update_data(self) -> dict[str, Any]:
"""Update Speedtest data."""
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/speedtestdotnet/sensor.py
Expand Up @@ -64,6 +64,11 @@ class SpeedtestSensorEntityDescription(SensorEntityDescription):
device_class=SensorDeviceClass.DATA_RATE,
value=lambda value: round(value / 10**6, 2),
),
SpeedtestSensorEntityDescription(
key="funny_rating",
translation_key="funny_rating",
value=lambda value: value,
),
)


Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/speedtestdotnet/strings.json
Expand Up @@ -28,6 +28,9 @@
},
"upload": {
"name": "Upload"
},
"funny_rating": {
"name": "Funny rating"
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion tests/components/speedtestdotnet/__init__.py
Expand Up @@ -35,6 +35,7 @@
"download": 1024000,
"upload": 1024000,
"ping": 18.465,
"funny_rating": "Kinda trashy.",
"server": {
"url": "http://test_server:8080/speedtest/upload.php",
"lat": "00.0000",
Expand All @@ -52,4 +53,9 @@
"share": None,
}

MOCK_STATES = {"ping": "18", "download": "1.02", "upload": "1.02"}
MOCK_STATES = {
"ping": "18",
"download": "1.02",
"upload": "1.02",
"funny_rating": "Kinda trashy.",
}
24 changes: 24 additions & 0 deletions tests/components/speedtestdotnet/test_coordinator.py
@@ -0,0 +1,24 @@
"""Tests for SpeedTest coordinator."""

import pytest

from homeassistant.components.speedtestdotnet import SpeedTestDataCoordinator


@pytest.mark.parametrize(
("download", "ping", "expected_result"),
[
(0.5, 10, "Literal potato speeds."),
(5, 10, "Kinda trashy."),
(25, 10, "Granny dial-up internet."),
(75, 10, "Nothing special, really"),
(150, 10, "Just above average. Bet you feel special."),
(100, 250, "Every online shooter's worst nightmare."),
],
)
def test_generate_funny_rating(download, ping, expected_result) -> None:
"""Test the generate_funny_rating function."""
assert (
SpeedTestDataCoordinator.generate_funny_rating(None, download, ping)
== expected_result
)
6 changes: 5 additions & 1 deletion tests/components/speedtestdotnet/test_sensor.py
Expand Up @@ -23,7 +23,7 @@ async def test_speedtestdotnet_sensors(
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 3
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 4

sensor = hass.states.get("sensor.speedtest_ping")
assert sensor
Expand All @@ -36,3 +36,7 @@ async def test_speedtestdotnet_sensors(
sensor = hass.states.get("sensor.speedtest_ping")
assert sensor
assert sensor.state == MOCK_STATES["ping"]

sensor = hass.states.get("sensor.speedtest_funny_rating")
assert sensor
assert sensor.state == MOCK_STATES["funny_rating"]

0 comments on commit 1753462

Please sign in to comment.