Skip to content

Commit

Permalink
More typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Apr 30, 2023
1 parent 008ba28 commit a89b716
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
19 changes: 10 additions & 9 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@
CONF_UPDATE_FREQUENCY, default=DEFAULT_DAILY_UPDATE_FREQUENCY
): selector.NumberSelector(
selector.NumberSelectorConfig(
min=10, unit_of_measurement="sec", mode=selector.NumberSelectorMode.BOX
)
min=10,
unit_of_measurement="sec",
mode=selector.NumberSelectorMode.BOX
) # type: ignore
),
}
)
Expand Down Expand Up @@ -219,7 +221,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 2

def __init__(self):
def __init__(self) -> None:
"""Initialize options flow."""
self.sensor_config: dict[str, Any] = {}
self.selected_sensor_type: str | None = None
Expand Down Expand Up @@ -518,7 +520,7 @@ async def async_step_model(self, user_input: dict[str, Any] | None = None) -> Fl
last_step=False,
)

async def async_step_post_library(self, user_input: dict[str, str] = None):
async def async_step_post_library(self, user_input: dict[str, str] = None) -> FlowResult:
"""Handles the logic after the user either selected manufacturer/model himself or confirmed autodiscovered"""
if (
self.power_profile.has_sub_profiles
Expand Down Expand Up @@ -751,8 +753,7 @@ def _get_strategy_schema(strategy: str, source_entity_id: str) -> vol.Schema:
return _create_linear_schema(source_entity_id)
if strategy == CalculationStrategy.WLED:
return SCHEMA_POWER_WLED
if strategy == CalculationStrategy.LUT:
return vol.Schema({})
return vol.Schema({})


def _create_virtual_power_schema(
Expand Down Expand Up @@ -869,7 +870,7 @@ def _create_linear_schema(source_entity_id: str) -> vol.Schema:
return SCHEMA_POWER_LINEAR.extend(
{
vol.Optional(CONF_ATTRIBUTE): selector.AttributeSelector(
selector.AttributeSelectorConfig(entity_id=source_entity_id)
selector.AttributeSelectorConfig(entity_id=source_entity_id, hide_attributes=[])
)
}
)
Expand Down Expand Up @@ -922,7 +923,7 @@ async def _create_schema_sub_profile(
profile = await library.get_profile(model_info)
sub_profiles = [
selector.SelectOptionDict(value=sub_profile, label=sub_profile)
for sub_profile in profile.get_sub_profiles()
for sub_profile in profile.get_sub_profiles() # type: ignore
]
return vol.Schema(
{
Expand Down Expand Up @@ -971,7 +972,7 @@ def _validate_daily_energy_input(user_input: dict[str, Any] = None) -> dict:
return errors


def _fill_schema_defaults(data_schema: vol.Schema, options: dict[str, str]):
def _fill_schema_defaults(data_schema: vol.Schema, options: dict[str, str]) -> vol.Schema:
"""Make a copy of the schema with suggested values set to saved options"""
schema = {}
for key, val in data_schema.schema.items():
Expand Down
18 changes: 10 additions & 8 deletions custom_components/powercalc/sensors/power.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import logging
from datetime import datetime
from decimal import Decimal, DecimalException
from typing import cast
from typing import cast, Any

import homeassistant.helpers.entity_registry as er
from homeassistant.components.sensor import (
Expand All @@ -23,6 +24,7 @@
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.event import (
Event,
TrackTemplate,
async_call_later,
async_track_state_change_event,
Expand Down Expand Up @@ -318,14 +320,14 @@ def __init__(
unique_id: str | None,
standby_power: Decimal,
standby_power_on: Decimal,
update_frequency,
update_frequency: int,
multiply_factor: float | None,
multiply_factor_standby: bool,
ignore_unavailable_state: bool,
rounding_digits: int,
sensor_config: dict,
power_profile: PowerProfile | None,
):
) -> None:
"""Initialize the sensor."""
self._power_calculator = power_calculator
self._calculation_mode = calculation_strategy
Expand Down Expand Up @@ -363,22 +365,22 @@ def __init__(
self._ignore_unavailable_state = True
self._standby_sensors: dict = hass.data[DOMAIN][DATA_STANDBY_POWER_SENSORS]

async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
await super().async_added_to_hass()

async def appliance_state_listener(event):
async def appliance_state_listener(event: Event) -> None:
"""Handle for state changes for dependent sensors."""
new_state = event.data.get("new_state")
await self._update_power_sensor(self._source_entity.entity_id, new_state)
async_dispatcher_send(self.hass, SIGNAL_POWER_SENSOR_STATE_CHANGE)

async def template_change_listener(*args):
async def template_change_listener(*args: Any) -> None:
state = self.hass.states.get(self._source_entity.entity_id)
await self._update_power_sensor(self._source_entity.entity_id, state)
async_dispatcher_send(self.hass, SIGNAL_POWER_SENSOR_STATE_CHANGE)

async def initial_update(event):
async def initial_update(event: Event) -> None:
for entity_id in self._track_entities:
new_state = self.hass.states.get(entity_id)
await self._update_power_sensor(entity_id, new_state)
Expand Down Expand Up @@ -420,7 +422,7 @@ async def initial_update(event):
self.async_on_remove(start.async_at_start(self.hass, initial_update))

@callback
def async_update(event_time=None):
def async_update(event_time: datetime | None = None):
"""Update the entity."""
self.async_schedule_update_ha_state(True)

Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ python_version = 3.10
show_error_codes = true
follow_imports = silent
local_partial_types = true
strict_equality = true
no_implicit_optional = true
warn_incomplete_stub = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unused_ignores = true
#enable_error_code = ignore-without-code, redundant-self, truthy-iterable
disable_error_code = typeddict-item
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
Expand Down

0 comments on commit a89b716

Please sign in to comment.