diff --git a/.coveragerc b/.coveragerc index 1280df1e654..162e0c65f06 100644 --- a/.coveragerc +++ b/.coveragerc @@ -589,7 +589,6 @@ omit = homeassistant/components/nut/sensor.py homeassistant/components/nx584/alarm_control_panel.py homeassistant/components/nzbget/coordinator.py - homeassistant/components/nzbget/sensor.py homeassistant/components/obihai/* homeassistant/components/octoprint/* homeassistant/components/oem/climate.py diff --git a/homeassistant/components/nzbget/sensor.py b/homeassistant/components/nzbget/sensor.py index c9437195826..ddbc73ca10a 100644 --- a/homeassistant/components/nzbget/sensor.py +++ b/homeassistant/components/nzbget/sensor.py @@ -61,7 +61,7 @@ async def async_setup_entry( ) ) - async_add_entities(sensors, True) + async_add_entities(sensors) class NZBGetSensor(NZBGetEntity, Entity): @@ -108,7 +108,7 @@ class NZBGetSensor(NZBGetEntity, Entity): @property def state(self): """Return the state of the sensor.""" - value = self.coordinator.data.status.get(self._sensor_type) + value = self.coordinator.data["status"].get(self._sensor_type) if value is None: _LOGGER.warning("Unable to locate value for %s", self._sensor_type) diff --git a/tests/components/nzbget/__init__.py b/tests/components/nzbget/__init__.py index 8da67e2a0a2..8a36b299d87 100644 --- a/tests/components/nzbget/__init__.py +++ b/tests/components/nzbget/__init__.py @@ -48,16 +48,16 @@ YAML_CONFIG = { MOCK_VERSION = "21.0" MOCK_STATUS = { - "ArticleCacheMB": "64", - "AverageDownloadRate": "512", - "DownloadPaused": "4", - "DownloadRate": "1000", - "DownloadedSizeMB": "256", - "FreeDiskSpaceMB": "1024", - "PostJobCount": "2", - "PostPaused": "4", - "RemainingSizeMB": "512", - "UpTimeSec": "600", + "ArticleCacheMB": 64, + "AverageDownloadRate": 1250000, + "DownloadPaused": 4, + "DownloadRate": 2500000, + "DownloadedSizeMB": 256, + "FreeDiskSpaceMB": 1024, + "PostJobCount": 2, + "PostPaused": 4, + "RemainingSizeMB": 512, + "UpTimeSec": 600, } MOCK_HISTORY = [ diff --git a/tests/components/nzbget/test_sensor.py b/tests/components/nzbget/test_sensor.py new file mode 100644 index 00000000000..43803384740 --- /dev/null +++ b/tests/components/nzbget/test_sensor.py @@ -0,0 +1,54 @@ +"""Test the NZBGet sensors.""" +from datetime import timedelta + +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + DATA_MEGABYTES, + DATA_RATE_MEGABYTES_PER_SECOND, + DEVICE_CLASS_TIMESTAMP, +) +from homeassistant.util import dt as dt_util + +from . import init_integration + +from tests.async_mock import patch + + +async def test_sensors(hass) -> None: + """Test the creation and values of the sensors.""" + now = dt_util.utcnow().replace(microsecond=0) + with patch("homeassistant.util.dt.utcnow", return_value=now): + entry = await init_integration(hass) + + registry = await hass.helpers.entity_registry.async_get_registry() + + uptime = now - timedelta(seconds=600) + + sensors = { + "article_cache": ("ArticleCacheMB", "64", DATA_MEGABYTES, None), + "average_speed": ( + "AverageDownloadRate", + "1.19", + DATA_RATE_MEGABYTES_PER_SECOND, + None, + ), + "download_paused": ("DownloadPaused", "4", None, None), + "speed": ("DownloadRate", "2.38", DATA_RATE_MEGABYTES_PER_SECOND, None), + "size": ("DownloadedSizeMB", "256", DATA_MEGABYTES, None), + "disk_free": ("FreeDiskSpaceMB", "1024", DATA_MEGABYTES, None), + "post_processing_jobs": ("PostJobCount", "2", "Jobs", None), + "post_processing_paused": ("PostPaused", "4", None, None), + "queue_size": ("RemainingSizeMB", "512", DATA_MEGABYTES, None), + "uptime": ("UpTimeSec", uptime.isoformat(), None, DEVICE_CLASS_TIMESTAMP), + } + + for (sensor_id, data) in sensors.items(): + entity_entry = registry.async_get(f"sensor.nzbgettest_{sensor_id}") + assert entity_entry + assert entity_entry.device_class == data[3] + assert entity_entry.unique_id == f"{entry.entry_id}_{data[0]}" + + state = hass.states.get(f"sensor.nzbgettest_{sensor_id}") + assert state + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == data[2] + assert state.state == data[1]