mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add and use time related constants (#32065)
* Add and use time related constants * Sort time constants and reuse them in data rate constants * Fix greeneyemonitor import * Fix greeneyemonitor import V2 * Fix failing here_travel_time test * Add TIME_MONTHS and TIME_YEARS * Use TIME_MINUTES in opentherm_gw and isy994 * Add and use TIME_MILLISECONDS * Change inconsistent ones * Add TIME_MICROSECONDS and TIME_WEEKS * Use constants in apcupsd and travisci * Fix import error in upnp sensor.py * Fix isy994 sensor.py using TIME_YEARS * Fix dyson tests * Use TIME_SECONDS in more places * Use TIME_DAYS in google wifi
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.const import TIME_HOURS, TIME_MINUTES, TIME_SECONDS
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
@@ -79,7 +80,7 @@ async def test_dataSet1(hass):
|
||||
"""Test derivative sensor state."""
|
||||
await setup_tests(
|
||||
hass,
|
||||
{"unit_time": "s"},
|
||||
{"unit_time": TIME_SECONDS},
|
||||
times=[20, 30, 40, 50],
|
||||
values=[10, 30, 5, 0],
|
||||
expected_state=-0.5,
|
||||
@@ -89,30 +90,46 @@ async def test_dataSet1(hass):
|
||||
async def test_dataSet2(hass):
|
||||
"""Test derivative sensor state."""
|
||||
await setup_tests(
|
||||
hass, {"unit_time": "s"}, times=[20, 30], values=[5, 0], expected_state=-0.5
|
||||
hass,
|
||||
{"unit_time": TIME_SECONDS},
|
||||
times=[20, 30],
|
||||
values=[5, 0],
|
||||
expected_state=-0.5,
|
||||
)
|
||||
|
||||
|
||||
async def test_dataSet3(hass):
|
||||
"""Test derivative sensor state."""
|
||||
state = await setup_tests(
|
||||
hass, {"unit_time": "s"}, times=[20, 30], values=[5, 10], expected_state=0.5
|
||||
hass,
|
||||
{"unit_time": TIME_SECONDS},
|
||||
times=[20, 30],
|
||||
values=[5, 10],
|
||||
expected_state=0.5,
|
||||
)
|
||||
|
||||
assert state.attributes.get("unit_of_measurement") == "/s"
|
||||
assert state.attributes.get("unit_of_measurement") == f"/{TIME_SECONDS}"
|
||||
|
||||
|
||||
async def test_dataSet4(hass):
|
||||
"""Test derivative sensor state."""
|
||||
await setup_tests(
|
||||
hass, {"unit_time": "s"}, times=[20, 30], values=[5, 5], expected_state=0
|
||||
hass,
|
||||
{"unit_time": TIME_SECONDS},
|
||||
times=[20, 30],
|
||||
values=[5, 5],
|
||||
expected_state=0,
|
||||
)
|
||||
|
||||
|
||||
async def test_dataSet5(hass):
|
||||
"""Test derivative sensor state."""
|
||||
await setup_tests(
|
||||
hass, {"unit_time": "s"}, times=[20, 30], values=[10, -10], expected_state=-2
|
||||
hass,
|
||||
{"unit_time": TIME_SECONDS},
|
||||
times=[20, 30],
|
||||
values=[10, -10],
|
||||
expected_state=-2,
|
||||
)
|
||||
|
||||
|
||||
@@ -137,7 +154,12 @@ async def test_data_moving_average_for_discrete_sensor(hass):
|
||||
times = list(range(0, 1800 + 30, 30))
|
||||
|
||||
config, entity_id = await _setup_sensor(
|
||||
hass, {"time_window": {"seconds": time_window}, "unit_time": "min", "round": 1}
|
||||
hass,
|
||||
{
|
||||
"time_window": {"seconds": time_window},
|
||||
"unit_time": TIME_MINUTES,
|
||||
"round": 1,
|
||||
},
|
||||
) # two minute window
|
||||
|
||||
for time, value in zip(times, temperature_values):
|
||||
@@ -186,7 +208,7 @@ async def test_prefix(hass):
|
||||
|
||||
# Testing a power sensor at 1000 Watts for 1hour = 0kW/h
|
||||
assert round(float(state.state), config["sensor"]["round"]) == 0.0
|
||||
assert state.attributes.get("unit_of_measurement") == "kW/h"
|
||||
assert state.attributes.get("unit_of_measurement") == f"kW/{TIME_HOURS}"
|
||||
|
||||
|
||||
async def test_suffix(hass):
|
||||
@@ -198,7 +220,7 @@ async def test_suffix(hass):
|
||||
"source": "sensor.bytes_per_second",
|
||||
"round": 2,
|
||||
"unit_prefix": "k",
|
||||
"unit_time": "s",
|
||||
"unit_time": TIME_SECONDS,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user