Skip to content

Commit 9d6b829

Browse files
committed
Add mypy check, add missing types and fix type issues
1 parent f0fb574 commit 9d6b829

19 files changed

+542
-239
lines changed

.github/workflows/python_check.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
python -m pip install --upgrade pip
27-
python -m pip install flake8 pylint black
27+
python -m pip install -r requirements.txt
28+
python -m pip install -r requirements_dev.txt
2829
- name: Lint with flake8
2930
run: |
3031
# stop the build if there are Python syntax errors or undefined names
3132
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
3233
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics
34+
- name: Type check with mypy
35+
run: |
36+
touch "$(python -c 'import inspect, homeassistant, os; print(os.path.dirname(inspect.getfile(homeassistant)))')"/py.typed
37+
mypy -p custom_components.hon
3338
# - name: Analysing the code with pylint
3439
# run: |
3540
# pylint --max-line-length 88 $(git ls-files '*.py')

custom_components/hon/__init__.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from pathlib import Path
33

4-
import voluptuous as vol
4+
import voluptuous as vol # type: ignore[import]
55
from homeassistant.config_entries import ConfigEntry
66
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
77
from homeassistant.helpers import config_validation as cv, aiohttp_client
@@ -25,13 +25,15 @@
2525
)
2626

2727

28-
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
28+
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None:
2929
session = aiohttp_client.async_get_clientsession(hass)
30+
if (config_dir := hass.config.config_dir) is None:
31+
raise ValueError("Missing Config Dir")
3032
hon = await Hon(
3133
entry.data["email"],
3234
entry.data["password"],
3335
session=session,
34-
test_data_path=Path(hass.config.config_dir),
36+
test_data_path=Path(config_dir),
3537
).create()
3638
hass.data.setdefault(DOMAIN, {})
3739
hass.data[DOMAIN][entry.unique_id] = hon
@@ -41,10 +43,10 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
4143
hass.async_create_task(
4244
hass.config_entries.async_forward_entry_setup(entry, platform)
4345
)
44-
return True
46+
return
4547

4648

47-
async def async_unload_entry(hass, entry: ConfigEntry) -> bool:
49+
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
4850
unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
4951
if unload:
5052
if not hass.data[DOMAIN]:

custom_components/hon/binary_sensor.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
)
99
from homeassistant.config_entries import ConfigEntry
1010
from homeassistant.core import callback
11+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
12+
from homeassistant.helpers.typing import HomeAssistantType
1113

1214
from .const import DOMAIN
1315
from .hon import HonEntity, unique_entities
@@ -287,7 +289,9 @@ class HonBinarySensorEntityDescription(BinarySensorEntityDescription):
287289
BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD"])
288290

289291

290-
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
292+
async def async_setup_entry(
293+
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
294+
) -> None:
291295
entities = []
292296
for device in hass.data[DOMAIN][entry.unique_id].appliances:
293297
for description in BINARY_SENSORS.get(device.appliance_type, []):
@@ -304,13 +308,13 @@ class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
304308

305309
@property
306310
def is_on(self) -> bool:
307-
return (
311+
return bool(
308312
self._device.get(self.entity_description.key, "")
309313
== self.entity_description.on_value
310314
)
311315

312316
@callback
313-
def _handle_coordinator_update(self, update=True) -> None:
317+
def _handle_coordinator_update(self, update: bool = True) -> None:
314318
self._attr_native_value = (
315319
self._device.get(self.entity_description.key, "")
316320
== self.entity_description.on_value

custom_components/hon/button.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from homeassistant.components.button import ButtonEntityDescription, ButtonEntity
66
from homeassistant.config_entries import ConfigEntry
77
from homeassistant.helpers.entity import EntityCategory
8+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
9+
from homeassistant.helpers.typing import HomeAssistantType
810
from pyhon.appliance import HonAppliance
911

1012
from .const import DOMAIN
1113
from .hon import HonEntity
14+
from .typedefs import HonButtonType
1215

1316
_LOGGER = logging.getLogger(__name__)
1417

@@ -38,8 +41,10 @@
3841
}
3942

4043

41-
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
42-
entities = []
44+
async def async_setup_entry(
45+
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
46+
) -> None:
47+
entities: list[HonButtonType] = []
4348
for device in hass.data[DOMAIN][entry.unique_id].appliances:
4449
for description in BUTTONS.get(device.appliance_type, []):
4550
if not device.commands.get(description.key):
@@ -70,7 +75,9 @@ def available(self) -> bool:
7075

7176

7277
class HonDeviceInfo(HonEntity, ButtonEntity):
73-
def __init__(self, hass, entry, device: HonAppliance) -> None:
78+
def __init__(
79+
self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance
80+
) -> None:
7481
super().__init__(hass, entry, device)
7582

7683
self._attr_unique_id = f"{super().unique_id}_show_device_info"
@@ -93,7 +100,9 @@ async def async_press(self) -> None:
93100

94101

95102
class HonDataArchive(HonEntity, ButtonEntity):
96-
def __init__(self, hass, entry, device: HonAppliance) -> None:
103+
def __init__(
104+
self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance
105+
) -> None:
97106
super().__init__(hass, entry, device)
98107

99108
self._attr_unique_id = f"{super().unique_id}_create_data_archive"
@@ -104,7 +113,9 @@ def __init__(self, hass, entry, device: HonAppliance) -> None:
104113
self._attr_entity_registry_enabled_default = False
105114

106115
async def async_press(self) -> None:
107-
path = Path(self._hass.config.config_dir) / "www"
116+
if (config_dir := self._hass.config.config_dir) is None:
117+
raise ValueError("Missing Config Dir")
118+
path = Path(config_dir) / "www"
108119
data = await self._device.data_archive(path)
109120
title = f"{self._device.nick_name} Data Archive"
110121
text = (

0 commit comments

Comments
 (0)