From 3193ea3359573f35e6929015f39c15b9f7170e59 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 20 Jul 2022 04:00:14 +0200 Subject: [PATCH] Fix type narrowing in energy integration (#75462) --- homeassistant/components/energy/data.py | 2 +- homeassistant/components/energy/validate.py | 23 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/energy/data.py b/homeassistant/components/energy/data.py index e8c62da0c3c..3b3952136be 100644 --- a/homeassistant/components/energy/data.py +++ b/homeassistant/components/energy/data.py @@ -54,7 +54,7 @@ class FlowToGridSourceType(TypedDict): stat_compensation: str | None # Used to generate costs if stat_compensation is set to None - entity_energy_from: str | None # entity_id of an energy meter (kWh), entity_id of the energy meter for stat_energy_from + entity_energy_to: str | None # entity_id of an energy meter (kWh), entity_id of the energy meter for stat_energy_to entity_energy_price: str | None # entity_id of an entity providing price ($/kWh) number_energy_price: float | None # Price for energy ($/kWh) diff --git a/homeassistant/components/energy/validate.py b/homeassistant/components/energy/validate.py index 3baae348770..02549ddfe96 100644 --- a/homeassistant/components/energy/validate.py +++ b/homeassistant/components/energy/validate.py @@ -263,10 +263,10 @@ def _async_validate_auto_generated_cost_entity( async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation: """Validate the energy configuration.""" - manager = await data.async_get_manager(hass) + manager: data.EnergyManager = await data.async_get_manager(hass) statistics_metadata: dict[str, tuple[int, recorder.models.StatisticMetaData]] = {} validate_calls = [] - wanted_statistics_metadata = set() + wanted_statistics_metadata: set[str] = set() result = EnergyPreferencesValidation() @@ -279,6 +279,7 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation: result.energy_sources.append(source_result) if source["type"] == "grid": + flow: data.FlowFromGridSourceType | data.FlowToGridSourceType for flow in source["flow_from"]: wanted_statistics_metadata.add(flow["stat_energy_from"]) validate_calls.append( @@ -294,14 +295,14 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation: ) ) - if flow.get("stat_cost") is not None: - wanted_statistics_metadata.add(flow["stat_cost"]) + if (stat_cost := flow.get("stat_cost")) is not None: + wanted_statistics_metadata.add(stat_cost) validate_calls.append( functools.partial( _async_validate_cost_stat, hass, statistics_metadata, - flow["stat_cost"], + stat_cost, source_result, ) ) @@ -345,14 +346,14 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation: ) ) - if flow.get("stat_compensation") is not None: - wanted_statistics_metadata.add(flow["stat_compensation"]) + if (stat_compensation := flow.get("stat_compensation")) is not None: + wanted_statistics_metadata.add(stat_compensation) validate_calls.append( functools.partial( _async_validate_cost_stat, hass, statistics_metadata, - flow["stat_compensation"], + stat_compensation, source_result, ) ) @@ -396,14 +397,14 @@ async def async_validate(hass: HomeAssistant) -> EnergyPreferencesValidation: ) ) - if source.get("stat_cost") is not None: - wanted_statistics_metadata.add(source["stat_cost"]) + if (stat_cost := source.get("stat_cost")) is not None: + wanted_statistics_metadata.add(stat_cost) validate_calls.append( functools.partial( _async_validate_cost_stat, hass, statistics_metadata, - source["stat_cost"], + stat_cost, source_result, ) )