1
0
mirror of https://github.com/home-assistant/core.git synced 2025-08-13 07:20:01 +00:00
Files
.devcontainer
.github
.vscode
docs
homeassistant
machine
pylint
rootfs
script
tests
auth
components
fixtures
hassfest
helpers
__init__.py
conftest.py
test_aiohttp_client.py
test_area_registry.py
test_check_config.py
test_collection.py
test_condition.py
test_config_entry_flow.py
test_config_entry_oauth2_flow.py
test_config_validation.py
test_debounce.py
test_deprecation.py
test_device_registry.py
test_discovery.py
test_discovery_flow.py
test_dispatcher.py
test_entity.py
test_entity_component.py
test_entity_platform.py
test_entity_registry.py
test_entity_values.py
test_entityfilter.py
test_event.py
test_frame.py
test_httpx_client.py
test_icon.py
test_init.py
test_instance_id.py
test_integration_platform.py
test_intent.py
test_json.py
test_location.py
test_network.py
test_ratelimit.py
test_recorder.py
test_reload.py
test_restore_state.py
test_script.py
test_script_variables.py
test_selector.py
test_service.py
test_significant_change.py
test_singleton.py
test_start.py
test_state.py
test_storage.py
test_storage_remove.py
test_sun.py
test_system_info.py
test_temperature.py
test_template.py
test_translation.py
test_trigger.py
test_update_coordinator.py
mock
resources
scripts
test_util
testing_config
util
__init__.py
bandit.yaml
common.py
conftest.py
ignore_uncaught_exceptions.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_exceptions.py
test_loader.py
test_main.py
test_requirements.py
test_runner.py
test_setup.py
test_test_fixtures.py
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hadolint.yaml
.ignore
.pre-commit-config.yaml
.prettierignore
.readthedocs.yml
.strict-typing
.yamllint
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
Dockerfile.dev
LICENSE.md
MANIFEST.in
README.rst
build.json
codecov.yml
mypy.ini
pyproject.toml
requirements.txt
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
requirements_test_pre_commit.txt
setup.cfg
setup.py
tox.ini
core/tests/helpers/test_significant_change.py

75 lines
2.8 KiB
Python

"""Test significant change helper."""
import pytest
from homeassistant.components.sensor import DEVICE_CLASS_BATTERY
from homeassistant.const import ATTR_DEVICE_CLASS, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import State
from homeassistant.helpers import significant_change
@pytest.fixture(name="checker")
async def checker_fixture(hass):
"""Checker fixture."""
checker = await significant_change.create_checker(hass, "test")
def async_check_significant_change(
_hass, old_state, _old_attrs, new_state, _new_attrs, **kwargs
):
return abs(float(old_state) - float(new_state)) > 4
hass.data[significant_change.DATA_FUNCTIONS][
"test_domain"
] = async_check_significant_change
return checker
async def test_signicant_change(hass, checker):
"""Test initialize helper works."""
ent_id = "test_domain.test_entity"
attrs = {ATTR_DEVICE_CLASS: DEVICE_CLASS_BATTERY}
assert checker.async_is_significant_change(State(ent_id, "100", attrs))
# Same state is not significant.
assert not checker.async_is_significant_change(State(ent_id, "100", attrs))
# State under 5 difference is not significant. (per test mock)
assert not checker.async_is_significant_change(State(ent_id, "96", attrs))
# Make sure we always compare against last significant change
assert checker.async_is_significant_change(State(ent_id, "95", attrs))
# State turned unknown
assert checker.async_is_significant_change(State(ent_id, STATE_UNKNOWN, attrs))
# State turned unavailable
assert checker.async_is_significant_change(State(ent_id, "100", attrs))
assert checker.async_is_significant_change(State(ent_id, STATE_UNAVAILABLE, attrs))
async def test_significant_change_extra(hass, checker):
"""Test extra significant checker works."""
ent_id = "test_domain.test_entity"
attrs = {ATTR_DEVICE_CLASS: DEVICE_CLASS_BATTERY}
assert checker.async_is_significant_change(State(ent_id, "100", attrs), extra_arg=1)
assert checker.async_is_significant_change(State(ent_id, "200", attrs), extra_arg=1)
# Reset the last significiant change to 100 to repeat test but with
# extra checker installed.
assert checker.async_is_significant_change(State(ent_id, "100", attrs), extra_arg=1)
def extra_significant_check(
hass, old_state, old_attrs, old_extra_arg, new_state, new_attrs, new_extra_arg
):
return old_extra_arg != new_extra_arg
checker.extra_significant_check = extra_significant_check
# This is normally a significant change (100 -> 200), but the extra arg check marks it
# as insignificant.
assert not checker.async_is_significant_change(
State(ent_id, "200", attrs), extra_arg=1
)
assert checker.async_is_significant_change(State(ent_id, "200", attrs), extra_arg=2)