From 8660098e07427f23b9a32439c912ad87998a8baf Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 8 Aug 2023 09:43:56 +0200 Subject: [PATCH] Full coverage power.py --- tests/sensors/test_power.py | 73 +++++++++++++++++++++++++++++++++ tests/strategy/test_playbook.py | 15 ++++++- tests/strategy/test_wled.py | 63 +++++++++++++++++++++++++++- 3 files changed, 148 insertions(+), 3 deletions(-) diff --git a/tests/sensors/test_power.py b/tests/sensors/test_power.py index 6f5942a62..ffa35a231 100644 --- a/tests/sensors/test_power.py +++ b/tests/sensors/test_power.py @@ -19,8 +19,10 @@ STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN, + EntityCategory, ) from homeassistant.core import EVENT_HOMEASSISTANT_START, CoreState, HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.util import dt from pytest_homeassistant_custom_component.common import ( MockEntity, @@ -46,6 +48,7 @@ CONF_MULTIPLY_FACTOR, CONF_MULTIPLY_FACTOR_STANDBY, CONF_POWER, + CONF_POWER_SENSOR_CATEGORY, CONF_POWER_SENSOR_ID, CONF_POWER_SENSOR_PRECISION, CONF_SLEEP_POWER, @@ -527,3 +530,73 @@ async def test_multiply_factor_standby_power_on(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert hass.states.get("sensor.test_device_power").state == "1.64" + + +async def test_multiply_factor_sleep_power(hass: HomeAssistant) -> None: + await run_powercalc_setup( + hass, + { + CONF_ENTITY_ID: "switch.test", + CONF_MODE: CalculationStrategy.FIXED, + CONF_FIXED: {CONF_POWER: 10}, + CONF_SLEEP_POWER: { + CONF_POWER: 2, + CONF_DELAY: 20, + }, + CONF_MULTIPLY_FACTOR: 2, + CONF_MULTIPLY_FACTOR_STANDBY: True, + }, + ) + + hass.states.async_set("switch.test", STATE_OFF) + await hass.async_block_till_done() + + async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=25)) + await hass.async_block_till_done() + + assert hass.states.get("sensor.test_power").state == "4.00" + + +async def test_standby_power_invalid_template(hass: HomeAssistant) -> None: + """Test when the template does not return a decimal it does not break the powercalc sensor""" + + await run_powercalc_setup( + hass, + { + CONF_ENTITY_ID: "switch.test", + CONF_MODE: CalculationStrategy.FIXED, + CONF_FIXED: {CONF_POWER: 10}, + CONF_STANDBY_POWER: "{{ states('sensor.foo') }}", + }, + ) + + hass.states.async_set("switch.test", STATE_OFF) + hass.states.async_set("sensor.foo", "bla") + await hass.async_block_till_done() + + assert hass.states.get("sensor.test_power").state == "0.00" + + hass.states.async_set("sensor.foo", "20") + await hass.async_block_till_done() + + assert hass.states.get("sensor.test_power").state == "20.00" + + +async def test_entity_category(hass: HomeAssistant) -> None: + """Test setting an entity_category on the power sensor""" + + await run_powercalc_setup( + hass, + { + CONF_ENTITY_ID: "switch.test", + CONF_MODE: CalculationStrategy.FIXED, + CONF_FIXED: {CONF_POWER: 10}, + CONF_UNIQUE_ID: "123", + CONF_POWER_SENSOR_CATEGORY: EntityCategory.DIAGNOSTIC, + }, + ) + + entity_registry = er.async_get(hass) + power_entry = entity_registry.async_get("sensor.test_power") + assert power_entry + assert power_entry.entity_category == EntityCategory.DIAGNOSTIC diff --git a/tests/strategy/test_playbook.py b/tests/strategy/test_playbook.py index f9ae54942..beced512f 100644 --- a/tests/strategy/test_playbook.py +++ b/tests/strategy/test_playbook.py @@ -115,7 +115,7 @@ async def test_turn_off_stops_running_playbook(hass: HomeAssistant) -> None: await elapse_and_assert_power(hass, 3, "0.50") -async def test_services_raises_error_on_non_playbook_sensor( +async def test_activate_service_raises_error_on_non_playbook_sensor( hass: HomeAssistant, ) -> None: await run_powercalc_setup( @@ -127,6 +127,19 @@ async def test_services_raises_error_on_non_playbook_sensor( with pytest.raises(HomeAssistantError): await _activate_playbook(hass, "playbook1") + + +async def test_stop_service_raises_error_on_non_playbook_sensor( + hass: HomeAssistant, +) -> None: + await run_powercalc_setup( + hass, + get_simple_fixed_config("switch.test"), + ) + hass.states.async_set("switch.test", STATE_ON) + await hass.async_block_till_done() + + with pytest.raises(HomeAssistantError): await _stop_playbook(hass) diff --git a/tests/strategy/test_wled.py b/tests/strategy/test_wled.py index 9792226d7..742596408 100644 --- a/tests/strategy/test_wled.py +++ b/tests/strategy/test_wled.py @@ -2,7 +2,7 @@ from homeassistant.components import sensor from homeassistant.components.sensor import SensorDeviceClass from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY -from homeassistant.const import CONF_PLATFORM, STATE_OFF, STATE_ON +from homeassistant.const import CONF_ENTITY_ID, CONF_PLATFORM, STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant, State from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers.device_registry import DeviceEntry @@ -15,7 +15,7 @@ import custom_components.test.sensor as test_sensor_platform from custom_components.powercalc.common import create_source_entity -from custom_components.powercalc.const import CONF_POWER_FACTOR, CONF_VOLTAGE, DOMAIN +from custom_components.powercalc.const import CONF_POWER_FACTOR, CONF_VOLTAGE, CONF_WLED, DOMAIN from custom_components.powercalc.errors import StrategyConfigurationError from custom_components.powercalc.strategy.wled import WledStrategy from tests.common import run_powercalc_setup @@ -190,3 +190,62 @@ async def test_wled_autodiscovery_flow(hass: HomeAssistant) -> None: {CONF_VOLTAGE: 5}, ) assert result["type"] == FlowResultType.CREATE_ENTRY + + +async def test_yaml_configuration(hass: HomeAssistant) -> None: + """ + Full functional test for YAML configuration setup. + Also check standby power can be calculated by the WLED strategy + """ + mock_device_registry( + hass, + { + "wled-device": DeviceEntry( + id="wled-device", + manufacturer="WLED", + model="FOSS", + ), + }, + ) + mock_registry( + hass, + { + "light.test": RegistryEntry( + entity_id="light.test", + unique_id="1234", + platform="light", + device_id="wled-device", + ), + "sensor.test_current": RegistryEntry( + entity_id="sensor.test_current", + unique_id="1234", + platform="sensor", + device_id="wled-device", + unit_of_measurement="mA", + original_device_class=SensorDeviceClass.CURRENT, + ), + }, + ) + + await run_powercalc_setup( + hass, + { + CONF_ENTITY_ID: "light.test", + CONF_WLED: { + CONF_VOLTAGE: 5, + CONF_POWER_FACTOR: 1, + }, + }, + ) + + hass.states.async_set("light.test", STATE_ON) + hass.states.async_set("sensor.test_current", 500) + await hass.async_block_till_done() + + assert hass.states.get("sensor.test_power").state == "2.50" + + hass.states.async_set("light.test", STATE_OFF) + hass.states.async_set("sensor.test_current", 50) + await hass.async_block_till_done() + + assert hass.states.get("sensor.test_power").state == "0.25"