Skip to content

Commit

Permalink
Use strict typing for LiteJet integration (home-assistant#88629)
Browse files Browse the repository at this point in the history
* Strict typing for LiteJet.

* Add test for new check.

* PR feedback.

* PR feedback.
  • Loading branch information
joncar authored Feb 24, 2023
1 parent ee7dfda commit e69091c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ homeassistant.components.ld2410_ble.*
homeassistant.components.lidarr.*
homeassistant.components.lifx.*
homeassistant.components.light.*
homeassistant.components.litejet.*
homeassistant.components.litterrobot.*
homeassistant.components.local_ip.*
homeassistant.components.lock.*
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/litejet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
Expand Down Expand Up @@ -63,7 +63,7 @@ def handle_connected_changed(connected: bool, reason: str) -> None:

system.on_connected_changed(handle_connected_changed)

async def handle_stop(event) -> None:
async def handle_stop(event: Event) -> None:
await system.close()

entry.async_on_unload(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/litejet/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def async_step_user(
errors=errors,
)

async def async_step_import(self, import_data):
async def async_step_import(self, import_data: dict[str, Any]) -> FlowResult:
"""Import litejet config from configuration.yaml."""
return self.async_create_entry(title=import_data[CONF_PORT], data=import_data)

Expand Down
16 changes: 10 additions & 6 deletions homeassistant/components/litejet/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import annotations

from collections.abc import Callable
from datetime import datetime
from typing import cast

from pylitejet import LiteJet
import voluptuous as vol
Expand Down Expand Up @@ -42,15 +44,15 @@ async def async_attach_trigger(
) -> CALLBACK_TYPE:
"""Listen for events based on configuration."""
trigger_data = trigger_info["trigger_data"]
number = config.get(CONF_NUMBER)
number = cast(int, config[CONF_NUMBER])
held_more_than = config.get(CONF_HELD_MORE_THAN)
held_less_than = config.get(CONF_HELD_LESS_THAN)
pressed_time = None
cancel_pressed_more_than: Callable | None = None
job = HassJob(action)

@callback
def call_action():
def call_action() -> None:
"""Call action with right context."""
hass.async_run_hass_job(
job,
Expand All @@ -72,11 +74,11 @@ def call_action():
# neither: trigger on pressed

@callback
def pressed_more_than_satisfied(now):
def pressed_more_than_satisfied(now: datetime) -> None:
"""Handle the LiteJet's switch's button pressed >= held_more_than."""
call_action()

def pressed():
def pressed() -> None:
"""Handle the press of the LiteJet switch's button."""
nonlocal cancel_pressed_more_than, pressed_time
nonlocal held_less_than, held_more_than
Expand All @@ -88,10 +90,12 @@ def pressed():
hass, pressed_more_than_satisfied, dt_util.utcnow() + held_more_than
)

def released():
def released() -> None:
"""Handle the release of the LiteJet switch's button."""
nonlocal cancel_pressed_more_than, pressed_time
nonlocal held_less_than, held_more_than
if pressed_time is None:
return
if cancel_pressed_more_than is not None:
cancel_pressed_more_than()
cancel_pressed_more_than = None
Expand All @@ -110,7 +114,7 @@ def released():
system.on_switch_released(number, released)

@callback
def async_remove():
def async_remove() -> None:
"""Remove all subscriptions used for this trigger."""
system.unsubscribe(pressed)
system.unsubscribe(released)
Expand Down
10 changes: 10 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,16 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true

[mypy-homeassistant.components.litejet.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true

[mypy-homeassistant.components.litterrobot.*]
check_untyped_defs = true
disallow_incomplete_defs = true
Expand Down
11 changes: 11 additions & 0 deletions tests/components/litejet/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ async def test_simple(hass: HomeAssistant, calls, mock_litejet) -> None:
assert calls[0].data["id"] == 0


async def test_only_release(hass: HomeAssistant, calls, mock_litejet) -> None:
"""Test the simplest form of a LiteJet trigger."""
await setup_automation(
hass, {"platform": "litejet", "number": ENTITY_OTHER_SWITCH_NUMBER}
)

await simulate_release(hass, mock_litejet, ENTITY_OTHER_SWITCH_NUMBER)

assert len(calls) == 0


async def test_held_more_than_short(hass: HomeAssistant, calls, mock_litejet) -> None:
"""Test a too short hold."""
await setup_automation(
Expand Down

0 comments on commit e69091c

Please sign in to comment.