Files
core/tests/components/nws/test_sensor.py
Abílio Costa b626204f63 Add default device class display precision for Sensor (#145013)
* Add default device class display precision for Sensor

* Renaming, docstrings, cleanup

* Simplify units list

* Fix tests

* Fix missing precision when suggested is specified

* Update snapshots

* Fix when unit of measurement is not valid

* Fix tests

* Fix deprecated unit usage

* Fix goalzero tests

The sensor native_value method was accessing the data dict and trowing,
since the mock did not have any data for the sensors.

Since now the precision is always specified (it was missing for those
sensors), the throw was hitting async_update_entity_options in _update_suggested_precision.
Previously, async_update_entity_options was not called since it had no
precision.

* Fix metoffice

* Fix smartthings

* Add default sensor data for Tesla Wall Connector tests

* Update snapshots

* Revert spaces

* Update smartthings snapshots

* Add missing sensor mock for tesla wall connector

* Address review comments

* Add doc comment

* Add cap to doc comment

* Update comment

* Update snapshots

* Update comment
2025-05-26 19:40:29 +02:00

109 lines
3.2 KiB
Python

"""Sensors for National Weather Service (NWS)."""
import pytest
from homeassistant.components.nws.const import ATTRIBUTION, DOMAIN
from homeassistant.components.nws.sensor import SENSOR_TYPES
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import ATTR_ATTRIBUTION, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util import slugify
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from .const import (
EXPECTED_FORECAST_IMPERIAL,
EXPECTED_FORECAST_METRIC,
NONE_OBSERVATION,
NWS_CONFIG,
SENSOR_EXPECTED_OBSERVATION_IMPERIAL,
SENSOR_EXPECTED_OBSERVATION_METRIC,
)
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
("units", "result_observation", "result_forecast"),
[
(
US_CUSTOMARY_SYSTEM,
SENSOR_EXPECTED_OBSERVATION_IMPERIAL,
EXPECTED_FORECAST_IMPERIAL,
),
(METRIC_SYSTEM, SENSOR_EXPECTED_OBSERVATION_METRIC, EXPECTED_FORECAST_METRIC),
],
)
async def test_imperial_metric(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
units,
result_observation,
result_forecast,
mock_simple_nws,
no_weather,
) -> None:
"""Test with imperial and metric units."""
for description in SENSOR_TYPES:
entity_registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
f"35_-75_{description.key}",
suggested_object_id=f"abc_{description.name}",
disabled_by=None,
)
hass.config.units = units
entry = MockConfigEntry(
domain=DOMAIN,
data=NWS_CONFIG,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
for description in SENSOR_TYPES:
assert description.name
state = hass.states.get(f"sensor.abc_{slugify(description.name)}")
assert state
assert state.state == result_observation[description.key], (
f"Failed for {description.key}"
)
assert state.attributes.get(ATTR_ATTRIBUTION) == ATTRIBUTION
@pytest.mark.parametrize("values", [NONE_OBSERVATION, None])
async def test_none_values(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_simple_nws,
no_weather,
values,
) -> None:
"""Test with no values."""
instance = mock_simple_nws.return_value
instance.observation = values
for description in SENSOR_TYPES:
entity_registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
f"35_-75_{description.key}",
suggested_object_id=f"abc_{description.name}",
disabled_by=None,
)
entry = MockConfigEntry(
domain=DOMAIN,
data=NWS_CONFIG,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
for description in SENSOR_TYPES:
assert description.name
state = hass.states.get(f"sensor.abc_{slugify(description.name)}")
assert state
assert state.state == STATE_UNKNOWN