core/tests/components/technove/test_sensor.py

126 lines
4.0 KiB
Python
Raw Normal View History

Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
"""Tests for the TechnoVE sensor platform."""
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
from datetime import timedelta
from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy import SnapshotAssertion
2024-02-09 07:41:48 +00:00
from technove import Station, Status, TechnoVEError
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
2024-02-09 07:41:48 +00:00
from homeassistant.components.technove.const import DOMAIN
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import setup_with_selected_platforms
2024-02-09 07:41:48 +00:00
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_object_fixture,
)
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "mock_technove")
async def test_sensors(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the creation and values of the TechnoVE sensors."""
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
entity_entries = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
assert entity_entries
for entity_entry in entity_entries:
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
assert hass.states.get(entity_entry.entity_id) == snapshot(
name=f"{entity_entry.entity_id}-state"
)
@pytest.mark.parametrize(
"entity_id",
[
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
"sensor.technove_station_signal_strength",
"sensor.technove_station_wi_fi_network_name",
],
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
)
@pytest.mark.usefixtures("init_integration")
async def test_disabled_by_default_sensors(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entity_id: str
) -> None:
"""Test the disabled by default TechnoVE sensors."""
assert hass.states.get(entity_id) is None
assert (entry := entity_registry.async_get(entity_id))
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_no_wifi_support(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_technove: MagicMock,
) -> None:
"""Test missing Wi-Fi information from TechnoVE device."""
# Remove Wi-Fi info
device = mock_technove.update.return_value
device.info.network_ssid = None
# Setup
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert (state := hass.states.get("sensor.technove_station_wi_fi_network_name"))
assert state.state == STATE_UNKNOWN
@pytest.mark.usefixtures("init_integration")
async def test_sensor_update_failure(
hass: HomeAssistant,
mock_technove: MagicMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test coordinator update failure."""
entity_id = "sensor.technove_station_status"
assert hass.states.get(entity_id).state == Status.PLUGGED_CHARGING.value
mock_technove.update.side_effect = TechnoVEError("Test error")
Implement TechnoVE integration (#106029) * Implement TechnoVE integration Only the basic sensors for now. * Add technoVE to strict typing * Implement TechnoVE PR suggestions * Remove Diagnostic from TechnoVE initial PR * Switch status sensor to Enum device class * Revert zeroconf for adding it back in subsequent PR * Implement changes from feedback in TechnoVE PR * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unnecessary translation keys * Fix existing technoVE tests * Use snapshot testing for TechnoVE sensors * Improve unit tests for TechnoVE * Add missing coverage for technoVE config flow * Add TechnoVE coordinator tests * Modify device_fixture for TechnoVE from PR Feedback * Change CONF_IP_ADDRESS to CONF_HOST for TechnoVE * Update homeassistant/components/technove/config_flow.py Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/technove/models.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Implement feedback from TechnoVE PR * Add test_sensor_update_failure to TechnoVE sensor tests * Add test for error recovery during config flow of TechnoVE * Remove test_coordinator.py from TechnoVE --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-17 10:04:35 +00:00
freezer.tick(timedelta(minutes=5, seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
2024-02-09 07:41:48 +00:00
@pytest.mark.usefixtures("init_integration")
async def test_sensor_unknown_status(
hass: HomeAssistant,
mock_technove: MagicMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test coordinator update failure."""
entity_id = "sensor.technove_station_status"
assert hass.states.get(entity_id).state == Status.PLUGGED_CHARGING.value
mock_technove.update.return_value = Station(
load_json_object_fixture("station_bad_status.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5, seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_UNKNOWN
# Other sensors should still be available
assert hass.states.get("sensor.technove_station_total_energy_usage").state == "1234"