Skip to content

Commit

Permalink
Full coverage power.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 8, 2023
1 parent c2cdd26 commit 8660098
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 3 deletions.
73 changes: 73 additions & 0 deletions tests/sensors/test_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
15 changes: 14 additions & 1 deletion tests/strategy/test_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)


Expand Down
63 changes: 61 additions & 2 deletions tests/strategy/test_wled.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"

0 comments on commit 8660098

Please sign in to comment.