Skip to content

Commit

Permalink
Typing for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed May 7, 2023
2 parents e9fb76e + eac69c4 commit a4049d9
Show file tree
Hide file tree
Showing 44 changed files with 596 additions and 227 deletions.
4 changes: 2 additions & 2 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from typing import Any

import homeassistant.util.dt as dt_util
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
RestoreSensor,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down
24 changes: 18 additions & 6 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ async def create_mock_light_entity(
platform.ENTITIES.extend(entities)

assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}},
hass,
light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: "test"}},
)
await hass.async_block_till_done()

Expand All @@ -62,15 +64,19 @@ async def create_mock_light_entity(
)

entity_entry = entity_registry.async_get_or_create(
"light", "test", entity.unique_id, device_id=device_entry.id,
"light",
"test",
entity.unique_id,
device_id=device_entry.id,
)
await hass.async_block_till_done()

return (entity_entry.entity_id, device_entry.id)


def create_discoverable_light(
name: str, unique_id: str = "99f899fefes",
name: str,
unique_id: str = "99f899fefes",
) -> test_light_platform.MockLight:
light = test_light_platform.MockLight(name, STATE_ON, unique_id)
light.manufacturer = "lidl"
Expand Down Expand Up @@ -106,7 +112,9 @@ async def run_powercalc_setup(

async def create_input_boolean(hass: HomeAssistant, name: str = "test"):
assert await async_setup_component(
hass, input_boolean.DOMAIN, {"input_boolean": {name: None}},
hass,
input_boolean.DOMAIN,
{"input_boolean": {name: None}},
)
await hass.async_block_till_done()

Expand Down Expand Up @@ -134,12 +142,16 @@ def get_simple_fixed_config(entity_id: str, power: float = 50) -> ConfigType:

def get_test_profile_dir(sub_dir: str) -> str:
return os.path.join(
os.path.dirname(__file__), "testing_config/powercalc_profiles", sub_dir,
os.path.dirname(__file__),
"testing_config/powercalc_profiles",
sub_dir,
)


async def create_mocked_virtual_power_sensor_entry(
hass: HomeAssistant, name: str, unique_id: str | None,
hass: HomeAssistant,
name: str,
unique_id: str | None,
) -> config_entries.ConfigEntry:
config_entry = MockConfigEntry(
domain=DOMAIN,
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def mock_config_entry() -> MockConfigEntry:
def mock_flow_init(hass):
"""Mock hass.config_entries.flow.async_init."""
with patch.object(
hass.config_entries.flow, "async_init", return_value=AsyncMock(),
hass.config_entries.flow,
"async_init",
return_value=AsyncMock(),
) as mock_init:
yield mock_init
4 changes: 3 additions & 1 deletion tests/group_include/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
],
)
async def test_composite_filter(
filter_return_values: list, operator: FilterOperator, expected_result: bool,
filter_return_values: list,
operator: FilterOperator,
expected_result: bool,
) -> None:
filter_mocks = []
for value in filter_return_values:
Expand Down
14 changes: 10 additions & 4 deletions tests/group_include/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@


async def test_include_area(
hass: HomeAssistant, entity_reg: EntityRegistry, area_reg: AreaRegistry,
hass: HomeAssistant,
entity_reg: EntityRegistry,
area_reg: AreaRegistry,
):
await create_mock_light_entity(hass, create_discoverable_light("bathroom_mirror"))

Expand Down Expand Up @@ -62,7 +64,8 @@ async def test_include_area(


async def test_include_area_not_found(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture,
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
):
caplog.set_level(logging.ERROR)
await run_powercalc_setup(
Expand Down Expand Up @@ -182,7 +185,8 @@ async def test_combine_include_with_entities(hass: HomeAssistant) -> None:
light_e = create_discoverable_light("light_e", "6765765756")
light_f = create_discoverable_light("light_f", "676576575sds6")
await create_mock_light_entity(
hass, [light_a, light_b, light_c, light_d, light_e, light_f],
hass,
[light_a, light_b, light_c, light_d, light_e, light_f],
)

# Ugly hack, maybe I can figure out something better in the future.
Expand Down Expand Up @@ -255,7 +259,9 @@ async def test_combine_include_with_entities(hass: HomeAssistant) -> None:


async def test_include_filter_domain(
hass: HomeAssistant, entity_reg: EntityRegistry, area_reg: AreaRegistry,
hass: HomeAssistant,
entity_reg: EntityRegistry,
area_reg: AreaRegistry,
):
area = area_reg.async_get_or_create("Bathroom 1")
await hass.async_block_till_done()
Expand Down
6 changes: 5 additions & 1 deletion tests/power_profile/device_types/test_infrared_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
CONF_MODEL,
)
from custom_components.test.light import MockLight
from tests.common import create_mock_light_entity, get_test_profile_dir, run_powercalc_setup
from tests.common import (
create_mock_light_entity,
get_test_profile_dir,
run_powercalc_setup,
)


async def test_infrared_light(hass: HomeAssistant):
Expand Down
16 changes: 12 additions & 4 deletions tests/power_profile/device_types/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ async def test_media_player(hass: HomeAssistant):
hass,
{
"nest-device": DeviceEntry(
id="nest-device-id", manufacturer=manufacturer, model=model,
id="nest-device-id",
manufacturer=manufacturer,
model=model,
),
},
)
Expand All @@ -60,21 +62,27 @@ async def test_media_player(hass: HomeAssistant):
assert power_state.state == "unavailable"

hass.states.async_set(
entity_id, STATE_PLAYING, {"volume_level": 0.20, "is_volume_muted": False},
entity_id,
STATE_PLAYING,
{"volume_level": 0.20, "is_volume_muted": False},
)
await hass.async_block_till_done()

assert hass.states.get(power_sensor_id).state == "2.04"

hass.states.async_set(
entity_id, STATE_PAUSED, {"volume_level": 0.20, "is_volume_muted": False},
entity_id,
STATE_PAUSED,
{"volume_level": 0.20, "is_volume_muted": False},
)
await hass.async_block_till_done()

assert hass.states.get(power_sensor_id).state == "1.65"

hass.states.async_set(
entity_id, STATE_PLAYING, {"volume_level": 0.20, "is_volume_muted": True},
entity_id,
STATE_PLAYING,
{"volume_level": 0.20, "is_volume_muted": True},
)
await hass.async_block_till_done()

Expand Down
18 changes: 13 additions & 5 deletions tests/power_profile/device_types/test_smart_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ async def test_smart_switch(hass: HomeAssistant):
hass,
{
"shelly-device": DeviceEntry(
id="shelly-device-id", manufacturer=manufacturer, model=model,
id="shelly-device-id",
manufacturer=manufacturer,
model=model,
),
},
)
Expand Down Expand Up @@ -100,7 +102,9 @@ async def test_smart_switch_power_input_yaml(hass: HomeAssistant):
hass,
{
"ikea-device-id": DeviceEntry(
id="ikea-device-id", manufacturer=manufacturer, model=model,
id="ikea-device-id",
manufacturer=manufacturer,
model=model,
),
},
)
Expand Down Expand Up @@ -158,7 +162,9 @@ async def test_smart_switch_power_input_gui_config_flow(hass: HomeAssistant):
hass,
{
"ikea-device-id": DeviceEntry(
id="ikea-device-id", manufacturer=manufacturer, model=model,
id="ikea-device-id",
manufacturer=manufacturer,
model=model,
),
},
)
Expand All @@ -173,13 +179,15 @@ async def test_smart_switch_power_input_gui_config_flow(hass: HomeAssistant):

assert flow["step_id"] == "library"
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], {CONF_CONFIRM_AUTODISCOVERED_MODEL: True},
flow["flow_id"],
{CONF_CONFIRM_AUTODISCOVERED_MODEL: True},
)

# After confirming the manufacturer/model we must be directed to the fixed config step
assert result["step_id"] == "fixed"
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], {CONF_POWER: 50},
flow["flow_id"],
{CONF_POWER: 50},
)
assert result["type"] == FlowResultType.CREATE_ENTRY

Expand Down
3 changes: 2 additions & 1 deletion tests/power_profile/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ async def test_get_non_existing_profile(hass: HomeAssistant):


async def test_hidden_directories_are_skipped_from_model_listing(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture,
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
):
caplog.set_level(logging.ERROR)
library = ProfileLibrary(hass)
Expand Down
21 changes: 14 additions & 7 deletions tests/power_profile/test_power_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

async def test_load_lut_profile_from_custom_directory(hass: HomeAssistant):
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("signify", "LCA001"), get_test_profile_dir("signify-LCA001"),
ModelInfo("signify", "LCA001"),
get_test_profile_dir("signify-LCA001"),
)
assert power_profile.calculation_strategy == CalculationStrategy.LUT
assert power_profile.manufacturer == "signify"
Expand All @@ -38,7 +39,8 @@ async def test_load_lut_profile_from_custom_directory(hass: HomeAssistant):

async def test_load_fixed_profile(hass: HomeAssistant):
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("dummy", "dummy"), get_test_profile_dir("fixed"),
ModelInfo("dummy", "dummy"),
get_test_profile_dir("fixed"),
)
assert power_profile.calculation_strategy == CalculationStrategy.FIXED
assert power_profile.standby_power == 0.5
Expand All @@ -50,7 +52,8 @@ async def test_load_fixed_profile(hass: HomeAssistant):

async def test_load_linear_profile(hass: HomeAssistant):
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("dummy", "dummy"), get_test_profile_dir("linear"),
ModelInfo("dummy", "dummy"),
get_test_profile_dir("linear"),
)
assert power_profile.calculation_strategy == CalculationStrategy.LINEAR
assert power_profile.standby_power == 0.5
Expand All @@ -62,7 +65,8 @@ async def test_load_linear_profile(hass: HomeAssistant):

async def test_load_linked_profile(hass: HomeAssistant):
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("signify", "LCA007"), get_test_profile_dir("linked_profile"),
ModelInfo("signify", "LCA007"),
get_test_profile_dir("linked_profile"),
)
assert power_profile.calculation_strategy == CalculationStrategy.LUT
assert power_profile.manufacturer == "signify"
Expand All @@ -85,7 +89,8 @@ async def test_load_sub_profile(hass: HomeAssistant):
async def test_load_sub_profile_without_model_json(hass: HomeAssistant):
"""Test if sub profile can be loaded correctly when the sub directories don't have an own model.json"""
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("test", "test/a"), get_test_profile_dir("sub_profile"),
ModelInfo("test", "test/a"),
get_test_profile_dir("sub_profile"),
)
assert power_profile.calculation_strategy == CalculationStrategy.LUT
assert power_profile.manufacturer == "test"
Expand Down Expand Up @@ -171,7 +176,8 @@ async def test_selecting_sub_profile_is_ignored(hass: HomeAssistant) -> None:
This should not happen anyway
"""
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("dummy", "dummy"), get_test_profile_dir("smart_switch"),
ModelInfo("dummy", "dummy"),
get_test_profile_dir("smart_switch"),
)

power_profile.select_sub_profile("foo")
Expand All @@ -180,7 +186,8 @@ async def test_selecting_sub_profile_is_ignored(hass: HomeAssistant) -> None:

async def test_device_type(hass: HomeAssistant) -> None:
power_profile = await ProfileLibrary.factory(hass).get_profile(
ModelInfo("dummy", "dummy"), get_test_profile_dir("media_player"),
ModelInfo("dummy", "dummy"),
get_test_profile_dir("media_player"),
)

assert power_profile.device_type == DeviceType.SMART_SPEAKER
18 changes: 14 additions & 4 deletions tests/sensors/test_daily_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
DEFAULT_DAILY_UPDATE_FREQUENCY,
create_daily_fixed_energy_sensor,
)
from tests.common import assert_entity_state, create_input_boolean, create_input_number, run_powercalc_setup
from tests.common import (
assert_entity_state,
create_input_boolean,
create_input_number,
run_powercalc_setup,
)


async def test_create_daily_energy_sensor_default_options(hass: HomeAssistant):
Expand All @@ -70,7 +75,9 @@ async def test_create_daily_energy_sensor_default_options(hass: HomeAssistant):
],
)
async def test_create_daily_energy_sensor_unit_prefix_watt(
hass: HomeAssistant, unit_prefix: str, unit_of_measurement: str,
hass: HomeAssistant,
unit_prefix: str,
unit_of_measurement: str,
):
"""Test that setting the unit_prefix results in the correct unit_of_measurement"""
sensor_config = {
Expand Down Expand Up @@ -318,7 +325,9 @@ async def test_reset_service(hass: HomeAssistant):

# Set the individual entities to some initial values
hass.states.async_set(
entity_id, "0.8", {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR},
entity_id,
"0.8",
{ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR},
)
await hass.async_block_till_done()

Expand Down Expand Up @@ -459,6 +468,7 @@ async def test_name_and_entity_id_can_be_inherited_from_source_entity(
async def _trigger_periodic_update(hass: HomeAssistant, number_of_updates: int = 1):
for _i in range(0, number_of_updates):
async_fire_time_changed(
hass, dt.utcnow() + timedelta(seconds=DEFAULT_DAILY_UPDATE_FREQUENCY),
hass,
dt.utcnow() + timedelta(seconds=DEFAULT_DAILY_UPDATE_FREQUENCY),
)
await hass.async_block_till_done()
Loading

0 comments on commit a4049d9

Please sign in to comment.