2021-03-24 06:17:51 +00:00
|
|
|
"""The tests for the Netatmo sensor platform."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.netatmo import sensor
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
from .common import TEST_TIME, selected_platforms
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
async def test_weather_sensor(hass, config_entry, netatmo_auth):
|
2021-03-24 06:17:51 +00:00
|
|
|
"""Test weather sensor setup."""
|
2021-05-20 12:59:19 +00:00
|
|
|
with patch("time.time", return_value=TEST_TIME), selected_platforms(["sensor"]):
|
2022-10-02 01:11:54 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
2021-05-20 12:59:19 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
prefix = "sensor.netatmoindoor_"
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
assert hass.states.get(f"{prefix}temperature").state == "24.6"
|
|
|
|
assert hass.states.get(f"{prefix}humidity").state == "36"
|
|
|
|
assert hass.states.get(f"{prefix}co2").state == "749"
|
|
|
|
assert hass.states.get(f"{prefix}pressure").state == "1017.3"
|
|
|
|
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
async def test_public_weather_sensor(hass, config_entry, netatmo_auth):
|
2021-03-24 06:17:51 +00:00
|
|
|
"""Test public weather sensor setup."""
|
2021-05-20 12:59:19 +00:00
|
|
|
with patch("time.time", return_value=TEST_TIME), selected_platforms(["sensor"]):
|
2022-10-02 01:11:54 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
2021-05-20 12:59:19 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.states.async_all()) > 0
|
|
|
|
|
2022-07-27 12:17:38 +00:00
|
|
|
prefix = "sensor.home_max_"
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
assert hass.states.get(f"{prefix}temperature").state == "27.4"
|
|
|
|
assert hass.states.get(f"{prefix}humidity").state == "76"
|
|
|
|
assert hass.states.get(f"{prefix}pressure").state == "1014.4"
|
|
|
|
|
2022-07-27 12:17:38 +00:00
|
|
|
prefix = "sensor.home_avg_"
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
assert hass.states.get(f"{prefix}temperature").state == "22.7"
|
|
|
|
assert hass.states.get(f"{prefix}humidity").state == "63.2"
|
|
|
|
assert hass.states.get(f"{prefix}pressure").state == "1010.3"
|
|
|
|
|
|
|
|
entities_before_change = len(hass.states.async_all())
|
|
|
|
|
|
|
|
valid_option = {
|
|
|
|
"lat_ne": 32.91336,
|
|
|
|
"lon_ne": -117.187429,
|
|
|
|
"lat_sw": 32.83336,
|
|
|
|
"lon_sw": -117.26743,
|
|
|
|
"show_on_map": True,
|
|
|
|
"area_name": "Home avg",
|
|
|
|
"mode": "max",
|
|
|
|
}
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
2021-03-24 06:17:51 +00:00
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={"new_area": "Home avg"}
|
|
|
|
)
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input=valid_option
|
|
|
|
)
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
await hass.async_block_till_done()
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
assert len(hass.states.async_all()) == entities_before_change
|
2021-05-20 12:59:19 +00:00
|
|
|
assert hass.states.get(f"{prefix}temperature").state == "27.4"
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"strength, expected",
|
|
|
|
[(50, "Full"), (60, "High"), (80, "Medium"), (90, "Low")],
|
|
|
|
)
|
|
|
|
async def test_process_wifi(strength, expected):
|
|
|
|
"""Test wifi strength translation."""
|
|
|
|
assert sensor.process_wifi(strength) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"strength, expected",
|
|
|
|
[(50, "Full"), (70, "High"), (80, "Medium"), (90, "Low")],
|
|
|
|
)
|
|
|
|
async def test_process_rf(strength, expected):
|
|
|
|
"""Test radio strength translation."""
|
|
|
|
assert sensor.process_rf(strength) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"health, expected",
|
|
|
|
[(4, "Unhealthy"), (3, "Poor"), (2, "Fair"), (1, "Fine"), (0, "Healthy")],
|
|
|
|
)
|
|
|
|
async def test_process_health(health, expected):
|
|
|
|
"""Test health index translation."""
|
|
|
|
assert sensor.process_health(health) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"uid, name, expected",
|
|
|
|
[
|
2022-09-26 01:55:58 +00:00
|
|
|
("12:34:56:37:11:ca-reachable", "mystation_reachable", "True"),
|
|
|
|
("12:34:56:03:1b:e4-rf_status", "mystation_yard_radio", "Full"),
|
2021-03-24 06:17:51 +00:00
|
|
|
(
|
|
|
|
"12:34:56:37:11:ca-wifi_status",
|
2022-09-26 01:55:58 +00:00
|
|
|
"mystation_wifi_strength",
|
2021-03-24 06:17:51 +00:00
|
|
|
"Full",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"12:34:56:37:11:ca-temp_trend",
|
2022-09-26 01:55:58 +00:00
|
|
|
"mystation_temperature_trend",
|
2021-03-24 06:17:51 +00:00
|
|
|
"stable",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"12:34:56:37:11:ca-pressure_trend",
|
|
|
|
"netatmo_mystation_pressure_trend",
|
|
|
|
"down",
|
|
|
|
),
|
|
|
|
("12:34:56:05:51:20-sum_rain_1", "netatmo_mystation_yard_rain_last_hour", "0"),
|
|
|
|
("12:34:56:05:51:20-sum_rain_24", "netatmo_mystation_yard_rain_today", "0"),
|
2022-09-26 01:55:58 +00:00
|
|
|
("12:34:56:03:1b:e4-windangle", "netatmoindoor_garden_direction", "SW"),
|
2021-03-24 06:17:51 +00:00
|
|
|
(
|
|
|
|
"12:34:56:03:1b:e4-windangle_value",
|
2022-09-26 01:55:58 +00:00
|
|
|
"netatmoindoor_garden_angle",
|
2021-03-24 06:17:51 +00:00
|
|
|
"217",
|
|
|
|
),
|
|
|
|
("12:34:56:03:1b:e4-gustangle", "mystation_garden_gust_direction", "S"),
|
|
|
|
(
|
|
|
|
"12:34:56:03:1b:e4-gustangle",
|
2022-09-26 01:55:58 +00:00
|
|
|
"netatmoindoor_garden_gust_direction",
|
2021-03-24 06:17:51 +00:00
|
|
|
"S",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"12:34:56:03:1b:e4-gustangle_value",
|
2022-09-26 01:55:58 +00:00
|
|
|
"netatmoindoor_garden_gust_angle",
|
2021-03-24 06:17:51 +00:00
|
|
|
"206",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"12:34:56:03:1b:e4-guststrength",
|
2022-09-26 01:55:58 +00:00
|
|
|
"netatmoindoor_garden_gust_strength",
|
2021-03-24 06:17:51 +00:00
|
|
|
"9",
|
|
|
|
),
|
2022-09-26 01:55:58 +00:00
|
|
|
(
|
|
|
|
"12:34:56:03:1b:e4-rf_status",
|
|
|
|
"netatmoindoor_garden_rf_strength",
|
|
|
|
"Full",
|
|
|
|
),
|
2021-03-24 06:17:51 +00:00
|
|
|
(
|
|
|
|
"12:34:56:26:68:92-health_idx",
|
2022-09-26 01:55:58 +00:00
|
|
|
"baby_bedroom_health",
|
2021-03-24 06:17:51 +00:00
|
|
|
"Fine",
|
|
|
|
),
|
2022-09-26 01:55:58 +00:00
|
|
|
(
|
|
|
|
"12:34:56:26:68:92-wifi_status",
|
|
|
|
"baby_bedroom_wifi",
|
|
|
|
"High",
|
|
|
|
),
|
|
|
|
("Home-max-windangle_value", "home_max_wind_angle", "17"),
|
|
|
|
("Home-max-gustangle_value", "home_max_gust_angle", "217"),
|
|
|
|
("Home-max-guststrength", "home_max_gust_strength", "31"),
|
|
|
|
("Home-max-sum_rain_1", "home_max_sum_rain_1", "0.2"),
|
2021-03-24 06:17:51 +00:00
|
|
|
],
|
|
|
|
)
|
2021-05-20 12:59:19 +00:00
|
|
|
async def test_weather_sensor_enabling(
|
|
|
|
hass, config_entry, uid, name, expected, netatmo_auth
|
|
|
|
):
|
2021-03-24 06:17:51 +00:00
|
|
|
"""Test enabling of by default disabled sensors."""
|
|
|
|
with patch("time.time", return_value=TEST_TIME), selected_platforms(["sensor"]):
|
|
|
|
states_before = len(hass.states.async_all())
|
|
|
|
assert hass.states.get(f"sensor.{name}") is None
|
|
|
|
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
registry.async_get_or_create(
|
|
|
|
"sensor",
|
|
|
|
"netatmo",
|
|
|
|
uid,
|
|
|
|
suggested_object_id=name,
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
2022-10-02 01:11:54 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
2021-03-24 06:17:51 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.states.async_all()) > states_before
|
|
|
|
assert hass.states.get(f"sensor.{name}").state == expected
|
2021-12-03 17:33:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_climate_battery_sensor(hass, config_entry, netatmo_auth):
|
|
|
|
"""Test climate device battery sensor."""
|
|
|
|
with patch("time.time", return_value=TEST_TIME), selected_platforms(
|
|
|
|
["sensor", "climate"]
|
|
|
|
):
|
2022-10-02 01:11:54 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
2021-12-03 17:33:24 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
prefix = "sensor.livingroom_"
|
|
|
|
|
|
|
|
assert hass.states.get(f"{prefix}battery_percent").state == "75"
|