Improve entity type hints [x] (#77887)

This commit is contained in:
epenet 2022-09-06 14:00:09 +02:00 committed by GitHub
parent a6b6949793
commit 856318b137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 47 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import re import re
from typing import Any
from xbox.webapi.api.client import XboxLiveClient from xbox.webapi.api.client import XboxLiveClient
from xbox.webapi.api.provider.catalog.models import Image from xbox.webapi.api.provider.catalog.models import Image
@ -154,42 +155,42 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
"""If the image url is remotely accessible.""" """If the image url is remotely accessible."""
return True return True
async def async_turn_on(self): async def async_turn_on(self) -> None:
"""Turn the media player on.""" """Turn the media player on."""
await self.client.smartglass.wake_up(self._console.id) await self.client.smartglass.wake_up(self._console.id)
async def async_turn_off(self): async def async_turn_off(self) -> None:
"""Turn the media player off.""" """Turn the media player off."""
await self.client.smartglass.turn_off(self._console.id) await self.client.smartglass.turn_off(self._console.id)
async def async_mute_volume(self, mute): async def async_mute_volume(self, mute: bool) -> None:
"""Mute the volume.""" """Mute the volume."""
if mute: if mute:
await self.client.smartglass.mute(self._console.id) await self.client.smartglass.mute(self._console.id)
else: else:
await self.client.smartglass.unmute(self._console.id) await self.client.smartglass.unmute(self._console.id)
async def async_volume_up(self): async def async_volume_up(self) -> None:
"""Turn volume up for media player.""" """Turn volume up for media player."""
await self.client.smartglass.volume(self._console.id, VolumeDirection.Up) await self.client.smartglass.volume(self._console.id, VolumeDirection.Up)
async def async_volume_down(self): async def async_volume_down(self) -> None:
"""Turn volume down for media player.""" """Turn volume down for media player."""
await self.client.smartglass.volume(self._console.id, VolumeDirection.Down) await self.client.smartglass.volume(self._console.id, VolumeDirection.Down)
async def async_media_play(self): async def async_media_play(self) -> None:
"""Send play command.""" """Send play command."""
await self.client.smartglass.play(self._console.id) await self.client.smartglass.play(self._console.id)
async def async_media_pause(self): async def async_media_pause(self) -> None:
"""Send pause command.""" """Send pause command."""
await self.client.smartglass.pause(self._console.id) await self.client.smartglass.pause(self._console.id)
async def async_media_previous_track(self): async def async_media_previous_track(self) -> None:
"""Send previous track command.""" """Send previous track command."""
await self.client.smartglass.previous(self._console.id) await self.client.smartglass.previous(self._console.id)
async def async_media_next_track(self): async def async_media_next_track(self) -> None:
"""Send next track command.""" """Send next track command."""
await self.client.smartglass.next(self._console.id) await self.client.smartglass.next(self._console.id)
@ -203,7 +204,9 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
media_content_id, media_content_id,
) )
async def async_play_media(self, media_type, media_id, **kwargs): async def async_play_media(
self, media_type: str, media_id: str, **kwargs: Any
) -> None:
"""Launch an app on the Xbox.""" """Launch an app on the Xbox."""
if media_id == "Home": if media_id == "Home":
await self.client.smartglass.go_home(self._console.id) await self.client.smartglass.go_home(self._console.id)

View File

@ -128,7 +128,7 @@ class XboxSensor(SensorEntity):
"""Return the icon to use in the frontend.""" """Return the icon to use in the frontend."""
return ICON return ICON
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Start custom polling.""" """Start custom polling."""
@callback @callback
@ -138,7 +138,7 @@ class XboxSensor(SensorEntity):
async_track_time_interval(self.hass, async_update, self._interval) async_track_time_interval(self.hass, async_update, self._interval)
def update(self): def update(self) -> None:
"""Update state data from Xbox API.""" """Update state data from Xbox API."""
presence = self._api.gamer(gamertag="", xuid=self._xuid).get("presence") presence = self._api.gamer(gamertag="", xuid=self._xuid).get("presence")
_LOGGER.debug("User presence: %s", presence) _LOGGER.debug("User presence: %s", presence)

View File

@ -153,7 +153,7 @@ class XiaomiBinarySensor(XiaomiDevice, BinarySensorEntity):
"""Return the class of binary sensor.""" """Return the class of binary sensor."""
return self._device_class return self._device_class
def update(self): def update(self) -> None:
"""Update the sensor state.""" """Update the sensor state."""
_LOGGER.debug("Updating xiaomi sensor (%s) by polling", self._sid) _LOGGER.debug("Updating xiaomi sensor (%s) by polling", self._sid)
self._get_from_hub(self._sid) self._get_from_hub(self._sid)

View File

@ -1,5 +1,6 @@
"""Support for Xiaomi Aqara binary sensors.""" """Support for Xiaomi Aqara binary sensors."""
import logging import logging
from typing import Any
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -179,13 +180,13 @@ class XiaomiGenericSwitch(XiaomiDevice, SwitchEntity):
attrs.update(super().extra_state_attributes) attrs.update(super().extra_state_attributes)
return attrs return attrs
def turn_on(self, **kwargs): def turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on.""" """Turn the switch on."""
if self._write_to_hub(self._sid, **{self._data_key: "on"}): if self._write_to_hub(self._sid, **{self._data_key: "on"}):
self._state = True self._state = True
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self, **kwargs): def turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off.""" """Turn the switch off."""
if self._write_to_hub(self._sid, **{self._data_key: "off"}): if self._write_to_hub(self._sid, **{self._data_key: "off"}):
self._state = False self._state = False
@ -216,7 +217,7 @@ class XiaomiGenericSwitch(XiaomiDevice, SwitchEntity):
self._state = state self._state = state
return True return True
def update(self): def update(self) -> None:
"""Get data from hub.""" """Get data from hub."""
_LOGGER.debug("Update data from hub: %s", self._name) _LOGGER.debug("Update data from hub: %s", self._name)
self._get_from_hub(self._sid) self._get_from_hub(self._sid)

View File

@ -1018,7 +1018,7 @@ class XiaomiGatewayLight(LightEntity):
self._gateway.light.set_rgb(0, self._rgb) self._gateway.light.set_rgb(0, self._rgb)
self.schedule_update_ha_state() self.schedule_update_ha_state()
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
try: try:
state_dict = await self.hass.async_add_executor_job( state_dict = await self.hass.async_add_executor_job(

View File

@ -5,6 +5,7 @@ import asyncio
from datetime import timedelta from datetime import timedelta
import logging import logging
import time import time
from typing import Any
from miio import ChuangmiIr, DeviceException from miio import ChuangmiIr, DeviceException
import voluptuous as vol import voluptuous as vol
@ -227,14 +228,14 @@ class XiaomiMiioRemote(RemoteEntity):
except DeviceException: except DeviceException:
return False return False
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on.""" """Turn the device on."""
_LOGGER.error( _LOGGER.error(
"Device does not support turn_on, " "Device does not support turn_on, "
"please use 'remote.send_command' to send commands" "please use 'remote.send_command' to send commands"
) )
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
_LOGGER.error( _LOGGER.error(
"Device does not support turn_off, " "Device does not support turn_off, "

View File

@ -892,7 +892,7 @@ class XiaomiAirQualityMonitor(XiaomiMiioEntity, SensorEntity):
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
return self._state_attrs return self._state_attrs
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the miio device.""" """Fetch state from the miio device."""
try: try:
state = await self.hass.async_add_executor_job(self._device.status) state = await self.hass.async_add_executor_job(self._device.status)
@ -958,7 +958,7 @@ class XiaomiGatewayIlluminanceSensor(SensorEntity):
"""Return the state of the device.""" """Return the state of the device."""
return self._state return self._state
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
try: try:
self._state = await self.hass.async_add_executor_job( self._state = await self.hass.async_add_executor_job(

View File

@ -5,6 +5,7 @@ import asyncio
from dataclasses import dataclass from dataclasses import dataclass
from functools import partial from functools import partial
import logging import logging
from typing import Any
from miio import AirConditioningCompanionV3, ChuangmiPlug, DeviceException, PowerStrip from miio import AirConditioningCompanionV3, ChuangmiPlug, DeviceException, PowerStrip
from miio.powerstrip import PowerMode from miio.powerstrip import PowerMode
@ -527,7 +528,7 @@ class XiaomiGenericCoordinatedSwitch(XiaomiCoordinatedMiioEntity, SwitchEntity):
self.async_write_ha_state() self.async_write_ha_state()
@property @property
def available(self): def available(self) -> bool:
"""Return true when state is known.""" """Return true when state is known."""
if ( if (
super().available super().available
@ -537,7 +538,7 @@ class XiaomiGenericCoordinatedSwitch(XiaomiCoordinatedMiioEntity, SwitchEntity):
return False return False
return super().available return super().available
async def async_turn_on(self, **kwargs) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on an option of the miio device.""" """Turn on an option of the miio device."""
method = getattr(self, self.entity_description.method_on) method = getattr(self, self.entity_description.method_on)
if await method(): if await method():
@ -545,7 +546,7 @@ class XiaomiGenericCoordinatedSwitch(XiaomiCoordinatedMiioEntity, SwitchEntity):
self._attr_is_on = True self._attr_is_on = True
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off an option of the miio device.""" """Turn off an option of the miio device."""
method = getattr(self, self.entity_description.method_off) method = getattr(self, self.entity_description.method_off)
if await method(): if await method():
@ -748,15 +749,15 @@ class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
"""Return true if switch is on.""" """Return true if switch is on."""
return self._sub_device.status[self._data_key] == "on" return self._sub_device.status[self._data_key] == "on"
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on.""" """Turn the switch on."""
await self.hass.async_add_executor_job(self._sub_device.on, self._channel) await self.hass.async_add_executor_job(self._sub_device.on, self._channel)
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off.""" """Turn the switch off."""
await self.hass.async_add_executor_job(self._sub_device.off, self._channel) await self.hass.async_add_executor_job(self._sub_device.off, self._channel)
async def async_toggle(self, **kwargs): async def async_toggle(self, **kwargs: Any) -> None:
"""Toggle the switch.""" """Toggle the switch."""
await self.hass.async_add_executor_job(self._sub_device.toggle, self._channel) await self.hass.async_add_executor_job(self._sub_device.toggle, self._channel)
@ -816,7 +817,7 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
return False return False
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the plug on.""" """Turn the plug on."""
result = await self._try_command("Turning the plug on failed", self._device.on) result = await self._try_command("Turning the plug on failed", self._device.on)
@ -824,7 +825,7 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
self._state = True self._state = True
self._skip_update = True self._skip_update = True
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the plug off.""" """Turn the plug off."""
result = await self._try_command( result = await self._try_command(
"Turning the plug off failed", self._device.off "Turning the plug off failed", self._device.off
@ -834,7 +835,7 @@ class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
self._state = False self._state = False
self._skip_update = True self._skip_update = True
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
# On state change the device doesn't provide the new state immediately. # On state change the device doesn't provide the new state immediately.
if self._skip_update: if self._skip_update:
@ -907,7 +908,7 @@ class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch):
if self._device_features & FEATURE_SET_POWER_PRICE == 1: if self._device_features & FEATURE_SET_POWER_PRICE == 1:
self._state_attrs[ATTR_POWER_PRICE] = None self._state_attrs[ATTR_POWER_PRICE] = None
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
# On state change the device doesn't provide the new state immediately. # On state change the device doesn't provide the new state immediately.
if self._skip_update: if self._skip_update:
@ -972,7 +973,7 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
if self._channel_usb is False: if self._channel_usb is False:
self._state_attrs[ATTR_LOAD_POWER] = None self._state_attrs[ATTR_LOAD_POWER] = None
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn a channel on.""" """Turn a channel on."""
if self._channel_usb: if self._channel_usb:
result = await self._try_command( result = await self._try_command(
@ -987,7 +988,7 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
self._state = True self._state = True
self._skip_update = True self._skip_update = True
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn a channel off.""" """Turn a channel off."""
if self._channel_usb: if self._channel_usb:
result = await self._try_command( result = await self._try_command(
@ -1002,7 +1003,7 @@ class ChuangMiPlugSwitch(XiaomiPlugGenericSwitch):
self._state = False self._state = False
self._skip_update = True self._skip_update = True
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
# On state change the device doesn't provide the new state immediately. # On state change the device doesn't provide the new state immediately.
if self._skip_update: if self._skip_update:
@ -1042,7 +1043,7 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
self._state_attrs.update({ATTR_TEMPERATURE: None, ATTR_LOAD_POWER: None}) self._state_attrs.update({ATTR_TEMPERATURE: None, ATTR_LOAD_POWER: None})
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the socket on.""" """Turn the socket on."""
result = await self._try_command( result = await self._try_command(
"Turning the socket on failed", self._device.socket_on "Turning the socket on failed", self._device.socket_on
@ -1052,7 +1053,7 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
self._state = True self._state = True
self._skip_update = True self._skip_update = True
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the socket off.""" """Turn the socket off."""
result = await self._try_command( result = await self._try_command(
"Turning the socket off failed", self._device.socket_off "Turning the socket off failed", self._device.socket_off
@ -1062,7 +1063,7 @@ class XiaomiAirConditioningCompanionSwitch(XiaomiPlugGenericSwitch):
self._state = False self._state = False
self._skip_update = True self._skip_update = True
async def async_update(self): async def async_update(self) -> None:
"""Fetch state from the device.""" """Fetch state from the device."""
# On state change the device doesn't provide the new state immediately. # On state change the device doesn't provide the new state immediately.
if self._skip_update: if self._skip_update:

View File

@ -329,7 +329,10 @@ class MiroboVacuum(
await self._try_command("Unable to locate the botvac: %s", self._device.find) await self._try_command("Unable to locate the botvac: %s", self._device.find)
async def async_send_command( async def async_send_command(
self, command: str, params: dict | list | None = None, **kwargs: Any self,
command: str,
params: dict[str, Any] | list[Any] | None = None,
**kwargs: Any,
) -> None: ) -> None:
"""Send raw command.""" """Send raw command."""
await self._try_command( await self._try_command(

View File

@ -87,7 +87,7 @@ class XiaomiTV(MediaPlayerEntity):
"""Indicate that state is assumed.""" """Indicate that state is assumed."""
return True return True
def turn_off(self): def turn_off(self) -> None:
""" """
Instruct the TV to turn sleep. Instruct the TV to turn sleep.
@ -100,17 +100,17 @@ class XiaomiTV(MediaPlayerEntity):
self._state = STATE_OFF self._state = STATE_OFF
def turn_on(self): def turn_on(self) -> None:
"""Wake the TV back up from sleep.""" """Wake the TV back up from sleep."""
if self._state != STATE_ON: if self._state != STATE_ON:
self._tv.wake() self._tv.wake()
self._state = STATE_ON self._state = STATE_ON
def volume_up(self): def volume_up(self) -> None:
"""Increase volume by one.""" """Increase volume by one."""
self._tv.volume_up() self._tv.volume_up()
def volume_down(self): def volume_down(self) -> None:
"""Decrease volume by one.""" """Decrease volume by one."""
self._tv.volume_down() self._tv.volume_down()

View File

@ -1,6 +1,8 @@
"""Support for XS1 climate devices.""" """Support for XS1 climate devices."""
from __future__ import annotations from __future__ import annotations
from typing import Any
from xs1_api_client.api_constants import ActuatorType from xs1_api_client.api_constants import ActuatorType
from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate import ClimateEntity
@ -69,7 +71,7 @@ class XS1ThermostatEntity(XS1DeviceEntity, ClimateEntity):
return self.sensor.value() return self.sensor.value()
@property @property
def temperature_unit(self): def temperature_unit(self) -> str:
"""Return the unit of measurement used by the platform.""" """Return the unit of measurement used by the platform."""
return self.device.unit() return self.device.unit()
@ -88,7 +90,7 @@ class XS1ThermostatEntity(XS1DeviceEntity, ClimateEntity):
"""Return the maximum temperature.""" """Return the maximum temperature."""
return MAX_TEMP return MAX_TEMP
def set_temperature(self, **kwargs): def set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature.""" """Set new target temperature."""
temp = kwargs.get(ATTR_TEMPERATURE) temp = kwargs.get(ATTR_TEMPERATURE)
@ -100,7 +102,7 @@ class XS1ThermostatEntity(XS1DeviceEntity, ClimateEntity):
def set_hvac_mode(self, hvac_mode: HVACMode) -> None: def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode.""" """Set new target hvac mode."""
async def async_update(self): async def async_update(self) -> None:
"""Also update the sensor when available.""" """Also update the sensor when available."""
await super().async_update() await super().async_update()
if self.sensor is not None: if self.sensor is not None:

View File

@ -1,6 +1,8 @@
"""Support for XS1 switches.""" """Support for XS1 switches."""
from __future__ import annotations from __future__ import annotations
from typing import Any
from xs1_api_client.api_constants import ActuatorType from xs1_api_client.api_constants import ActuatorType
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
@ -43,10 +45,10 @@ class XS1SwitchEntity(XS1DeviceEntity, SwitchEntity):
"""Return true if switch is on.""" """Return true if switch is on."""
return self.device.value() == 100 return self.device.value() == 100
def turn_on(self, **kwargs): def turn_on(self, **kwargs: Any) -> None:
"""Turn the device on.""" """Turn the device on."""
self.device.turn_on() self.device.turn_on()
def turn_off(self, **kwargs): def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
self.device.turn_off() self.device.turn_off()