Add strict typing to command_line (#106889)

* Add strict typing to command_line

* Code review
This commit is contained in:
Marc Mueller 2024-01-02 20:04:28 +01:00 committed by GitHub
parent 943fb2791e
commit dcee8e67c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 18 deletions

View file

@ -116,6 +116,7 @@ homeassistant.components.clickatell.*
homeassistant.components.clicksend.* homeassistant.components.clicksend.*
homeassistant.components.climate.* homeassistant.components.climate.*
homeassistant.components.cloud.* homeassistant.components.cloud.*
homeassistant.components.command_line.*
homeassistant.components.configurator.* homeassistant.components.configurator.*
homeassistant.components.cover.* homeassistant.components.cover.*
homeassistant.components.cpuspeed.* homeassistant.components.cpuspeed.*

View file

@ -200,7 +200,7 @@ async def async_load_platforms(
load_coroutines: list[Coroutine[Any, Any, None]] = [] load_coroutines: list[Coroutine[Any, Any, None]] = []
platforms: list[Platform] = [] platforms: list[Platform] = []
reload_configs: list[tuple] = [] reload_configs: list[tuple[Platform, dict[str, Any]]] = []
for platform_config in command_line_config: for platform_config in command_line_config:
for platform, _config in platform_config.items(): for platform, _config in platform_config.items():
if (mapped_platform := PLATFORM_MAPPING[platform]) not in platforms: if (mapped_platform := PLATFORM_MAPPING[platform]) not in platforms:

View file

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from datetime import timedelta from datetime import datetime, timedelta
from typing import cast from typing import cast
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -115,7 +115,7 @@ class CommandBinarySensor(ManualTriggerEntity, BinarySensorEntity):
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to hass.""" """Call when entity about to be added to hass."""
await super().async_added_to_hass() await super().async_added_to_hass()
await self._update_entity_state(None) await self._update_entity_state()
self.async_on_remove( self.async_on_remove(
async_track_time_interval( async_track_time_interval(
self.hass, self.hass,
@ -126,7 +126,7 @@ class CommandBinarySensor(ManualTriggerEntity, BinarySensorEntity):
), ),
) )
async def _update_entity_state(self, now) -> None: async def _update_entity_state(self, now: datetime | None = None) -> None:
"""Update the state of the entity.""" """Update the state of the entity."""
if self._process_updates is None: if self._process_updates is None:
self._process_updates = asyncio.Lock() self._process_updates = asyncio.Lock()

View file

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from datetime import timedelta from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, cast from typing import TYPE_CHECKING, Any, cast
from homeassistant.components.cover import CoverEntity from homeassistant.components.cover import CoverEntity
@ -147,7 +147,7 @@ class CommandCover(ManualTriggerEntity, CoverEntity):
if TYPE_CHECKING: if TYPE_CHECKING:
return None return None
async def _update_entity_state(self, now) -> None: async def _update_entity_state(self, now: datetime | None = None) -> None:
"""Update the state of the entity.""" """Update the state of the entity."""
if self._process_updates is None: if self._process_updates is None:
self._process_updates = asyncio.Lock() self._process_updates = asyncio.Lock()
@ -186,14 +186,14 @@ class CommandCover(ManualTriggerEntity, CoverEntity):
async def async_open_cover(self, **kwargs: Any) -> None: async def async_open_cover(self, **kwargs: Any) -> None:
"""Open the cover.""" """Open the cover."""
await self.hass.async_add_executor_job(self._move_cover, self._command_open) await self.hass.async_add_executor_job(self._move_cover, self._command_open)
await self._update_entity_state(None) await self._update_entity_state()
async def async_close_cover(self, **kwargs: Any) -> None: async def async_close_cover(self, **kwargs: Any) -> None:
"""Close the cover.""" """Close the cover."""
await self.hass.async_add_executor_job(self._move_cover, self._command_close) await self.hass.async_add_executor_job(self._move_cover, self._command_close)
await self._update_entity_state(None) await self._update_entity_state()
async def async_stop_cover(self, **kwargs: Any) -> None: async def async_stop_cover(self, **kwargs: Any) -> None:
"""Stop the cover.""" """Stop the cover."""
await self.hass.async_add_executor_job(self._move_cover, self._command_stop) await self.hass.async_add_executor_job(self._move_cover, self._command_stop)
await self._update_entity_state(None) await self._update_entity_state()

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import asyncio import asyncio
from collections.abc import Mapping from collections.abc import Mapping
from datetime import timedelta from datetime import datetime, timedelta
import json import json
from typing import Any, cast from typing import Any, cast
@ -108,7 +108,7 @@ class CommandSensor(ManualTriggerSensorEntity):
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(self.hass, config) super().__init__(self.hass, config)
self.data = data self.data = data
self._attr_extra_state_attributes = {} self._attr_extra_state_attributes: dict[str, Any] = {}
self._json_attributes = json_attributes self._json_attributes = json_attributes
self._attr_native_value = None self._attr_native_value = None
self._value_template = value_template self._value_template = value_template
@ -118,12 +118,12 @@ class CommandSensor(ManualTriggerSensorEntity):
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return extra state attributes.""" """Return extra state attributes."""
return cast(dict, self._attr_extra_state_attributes) return self._attr_extra_state_attributes
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to hass.""" """Call when entity about to be added to hass."""
await super().async_added_to_hass() await super().async_added_to_hass()
await self._update_entity_state(None) await self._update_entity_state()
self.async_on_remove( self.async_on_remove(
async_track_time_interval( async_track_time_interval(
self.hass, self.hass,
@ -134,7 +134,7 @@ class CommandSensor(ManualTriggerSensorEntity):
), ),
) )
async def _update_entity_state(self, now) -> None: async def _update_entity_state(self, now: datetime | None = None) -> None:
"""Update the state of the entity.""" """Update the state of the entity."""
if self._process_updates is None: if self._process_updates is None:
self._process_updates = asyncio.Lock() self._process_updates = asyncio.Lock()

View file

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from datetime import timedelta from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, cast from typing import TYPE_CHECKING, Any, cast
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
@ -155,7 +155,7 @@ class CommandSwitch(ManualTriggerEntity, SwitchEntity):
if TYPE_CHECKING: if TYPE_CHECKING:
return None return None
async def _update_entity_state(self, now) -> None: async def _update_entity_state(self, now: datetime | None = None) -> None:
"""Update the state of the entity.""" """Update the state of the entity."""
if self._process_updates is None: if self._process_updates is None:
self._process_updates = asyncio.Lock() self._process_updates = asyncio.Lock()
@ -197,11 +197,11 @@ class CommandSwitch(ManualTriggerEntity, SwitchEntity):
if await self._switch(self._command_on) and not self._command_state: if await self._switch(self._command_on) and not self._command_state:
self._attr_is_on = True self._attr_is_on = True
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
await self._update_entity_state(None) await self._update_entity_state()
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
if await self._switch(self._command_off) and not self._command_state: if await self._switch(self._command_off) and not self._command_state:
self._attr_is_on = False self._attr_is_on = False
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
await self._update_entity_state(None) await self._update_entity_state()

View file

@ -920,6 +920,16 @@ disallow_untyped_defs = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.command_line.*]
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.configurator.*] [mypy-homeassistant.components.configurator.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true