Improve entity type hints [y] (#77888)

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

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
import requests import requests
import rxv import rxv
@ -215,7 +216,7 @@ class YamahaDevice(MediaPlayerEntity):
self._name = name self._name = name
self._zone = receiver.zone self._zone = receiver.zone
def update(self): def update(self) -> None:
"""Get the latest details from the device.""" """Get the latest details from the device."""
try: try:
self._play_status = self.receiver.play_status() self._play_status = self.receiver.play_status()
@ -335,42 +336,42 @@ class YamahaDevice(MediaPlayerEntity):
supported_features |= feature supported_features |= feature
return supported_features return supported_features
def turn_off(self): def turn_off(self) -> None:
"""Turn off media player.""" """Turn off media player."""
self.receiver.on = False self.receiver.on = False
def set_volume_level(self, volume): def set_volume_level(self, volume: float) -> None:
"""Set volume level, range 0..1.""" """Set volume level, range 0..1."""
receiver_vol = 100 - (volume * 100) receiver_vol = 100 - (volume * 100)
negative_receiver_vol = -receiver_vol negative_receiver_vol = -receiver_vol
self.receiver.volume = negative_receiver_vol self.receiver.volume = negative_receiver_vol
def mute_volume(self, mute): def mute_volume(self, mute: bool) -> None:
"""Mute (true) or unmute (false) media player.""" """Mute (true) or unmute (false) media player."""
self.receiver.mute = mute self.receiver.mute = mute
def turn_on(self): def turn_on(self) -> None:
"""Turn the media player on.""" """Turn the media player on."""
self.receiver.on = True self.receiver.on = True
self._volume = (self.receiver.volume / 100) + 1 self._volume = (self.receiver.volume / 100) + 1
def media_play(self): def media_play(self) -> None:
"""Send play command.""" """Send play command."""
self._call_playback_function(self.receiver.play, "play") self._call_playback_function(self.receiver.play, "play")
def media_pause(self): def media_pause(self) -> None:
"""Send pause command.""" """Send pause command."""
self._call_playback_function(self.receiver.pause, "pause") self._call_playback_function(self.receiver.pause, "pause")
def media_stop(self): def media_stop(self) -> None:
"""Send stop command.""" """Send stop command."""
self._call_playback_function(self.receiver.stop, "stop") self._call_playback_function(self.receiver.stop, "stop")
def media_previous_track(self): def media_previous_track(self) -> None:
"""Send previous track command.""" """Send previous track command."""
self._call_playback_function(self.receiver.previous, "previous track") self._call_playback_function(self.receiver.previous, "previous track")
def media_next_track(self): def media_next_track(self) -> None:
"""Send next track command.""" """Send next track command."""
self._call_playback_function(self.receiver.next, "next track") self._call_playback_function(self.receiver.next, "next track")
@ -380,11 +381,11 @@ class YamahaDevice(MediaPlayerEntity):
except rxv.exceptions.ResponseException: except rxv.exceptions.ResponseException:
_LOGGER.warning("Failed to execute %s on %s", function_text, self._name) _LOGGER.warning("Failed to execute %s on %s", function_text, self._name)
def select_source(self, source): def select_source(self, source: str) -> None:
"""Select input source.""" """Select input source."""
self.receiver.input = self._reverse_mapping.get(source, source) self.receiver.input = self._reverse_mapping.get(source, source)
def play_media(self, media_type, media_id, **kwargs): def play_media(self, media_type: str, media_id: str, **kwargs: Any) -> None:
"""Play media from an ID. """Play media from an ID.
This exposes a pass through for various input sources in the This exposes a pass through for various input sources in the
@ -421,7 +422,7 @@ class YamahaDevice(MediaPlayerEntity):
except AssertionError: except AssertionError:
_LOGGER.warning("Scene '%s' does not exist!", scene) _LOGGER.warning("Scene '%s' does not exist!", scene)
def select_sound_mode(self, sound_mode): def select_sound_mode(self, sound_mode: str) -> None:
"""Set Sound Mode for Receiver..""" """Set Sound Mode for Receiver.."""
self.receiver.surround_program = sound_mode self.receiver.surround_program = sound_mode

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import contextlib import contextlib
import logging import logging
from typing import Any
from aiomusiccast import MusicCastGroupException, MusicCastMediaContent from aiomusiccast import MusicCastGroupException, MusicCastMediaContent
from aiomusiccast.features import ZoneFeature from aiomusiccast.features import ZoneFeature
@ -100,7 +101,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
self._cur_track = 0 self._cur_track = 0
self._repeat = REPEAT_MODE_OFF self._repeat = REPEAT_MODE_OFF
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Run when this Entity has been added to HA.""" """Run when this Entity has been added to HA."""
await super().async_added_to_hass() await super().async_added_to_hass()
self.coordinator.entities.append(self) self.coordinator.entities.append(self)
@ -112,7 +113,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
self.coordinator.async_add_listener(self.async_schedule_check_client_list) self.coordinator.async_add_listener(self.async_schedule_check_client_list)
) )
async def async_will_remove_from_hass(self): async def async_will_remove_from_hass(self) -> None:
"""Entity being removed from hass.""" """Entity being removed from hass."""
await super().async_will_remove_from_hass() await super().async_will_remove_from_hass()
self.coordinator.entities.remove(self) self.coordinator.entities.remove(self)
@ -205,36 +206,36 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"""Return the unique ID for this media_player.""" """Return the unique ID for this media_player."""
return f"{self.coordinator.data.device_id}_{self._zone_id}" return f"{self.coordinator.data.device_id}_{self._zone_id}"
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.coordinator.musiccast.turn_on(self._zone_id) await self.coordinator.musiccast.turn_on(self._zone_id)
self.async_write_ha_state() self.async_write_ha_state()
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.coordinator.musiccast.turn_off(self._zone_id) await self.coordinator.musiccast.turn_off(self._zone_id)
self.async_write_ha_state() self.async_write_ha_state()
async def async_mute_volume(self, mute): async def async_mute_volume(self, mute: bool) -> None:
"""Mute the volume.""" """Mute the volume."""
await self.coordinator.musiccast.mute_volume(self._zone_id, mute) await self.coordinator.musiccast.mute_volume(self._zone_id, mute)
self.async_write_ha_state() self.async_write_ha_state()
async def async_set_volume_level(self, volume): async def async_set_volume_level(self, volume: float) -> None:
"""Set the volume level, range 0..1.""" """Set the volume level, range 0..1."""
await self.coordinator.musiccast.set_volume_level(self._zone_id, volume) await self.coordinator.musiccast.set_volume_level(self._zone_id, volume)
self.async_write_ha_state() self.async_write_ha_state()
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.coordinator.musiccast.volume_up(self._zone_id) await self.coordinator.musiccast.volume_up(self._zone_id)
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.coordinator.musiccast.volume_down(self._zone_id) await self.coordinator.musiccast.volume_down(self._zone_id)
async def async_media_play(self): async def async_media_play(self) -> None:
"""Send play command.""" """Send play command."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_play() await self.coordinator.musiccast.netusb_play()
@ -243,7 +244,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service play is not supported for non NetUSB sources." "Service play is not supported for non NetUSB sources."
) )
async def async_media_pause(self): async def async_media_pause(self) -> None:
"""Send pause command.""" """Send pause command."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_pause() await self.coordinator.musiccast.netusb_pause()
@ -252,7 +253,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service pause is not supported for non NetUSB sources." "Service pause is not supported for non NetUSB sources."
) )
async def async_media_stop(self): async def async_media_stop(self) -> None:
"""Send stop command.""" """Send stop command."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_stop() await self.coordinator.musiccast.netusb_stop()
@ -261,7 +262,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service stop is not supported for non NetUSB sources." "Service stop is not supported for non NetUSB sources."
) )
async def async_set_shuffle(self, shuffle): async def async_set_shuffle(self, shuffle: bool) -> None:
"""Enable/disable shuffle mode.""" """Enable/disable shuffle mode."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_shuffle(shuffle) await self.coordinator.musiccast.netusb_shuffle(shuffle)
@ -270,7 +271,9 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service shuffle is not supported for non NetUSB sources." "Service shuffle is not supported for non NetUSB sources."
) )
async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> None: async def async_play_media(
self, media_type: str, media_id: str, **kwargs: Any
) -> None:
"""Play media.""" """Play media."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):
play_item = await media_source.async_resolve_media( play_item = await media_source.async_resolve_media(
@ -384,7 +387,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
return overview return overview
async def async_select_sound_mode(self, sound_mode): async def async_select_sound_mode(self, sound_mode: str) -> None:
"""Select sound mode.""" """Select sound mode."""
await self.coordinator.musiccast.select_sound_mode(self._zone_id, sound_mode) await self.coordinator.musiccast.select_sound_mode(self._zone_id, sound_mode)
@ -461,7 +464,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
return supported_features return supported_features
async def async_media_previous_track(self): async def async_media_previous_track(self) -> None:
"""Send previous track command.""" """Send previous track command."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_previous_track() await self.coordinator.musiccast.netusb_previous_track()
@ -472,7 +475,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service previous track is not supported for non NetUSB or Tuner sources." "Service previous track is not supported for non NetUSB or Tuner sources."
) )
async def async_media_next_track(self): async def async_media_next_track(self) -> None:
"""Send next track command.""" """Send next track command."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_next_track() await self.coordinator.musiccast.netusb_next_track()
@ -483,7 +486,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service next track is not supported for non NetUSB or Tuner sources." "Service next track is not supported for non NetUSB or Tuner sources."
) )
async def async_set_repeat(self, repeat): async def async_set_repeat(self, repeat: str) -> None:
"""Enable/disable repeat mode.""" """Enable/disable repeat mode."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_repeat( await self.coordinator.musiccast.netusb_repeat(
@ -494,7 +497,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service set repeat is not supported for non NetUSB sources." "Service set repeat is not supported for non NetUSB sources."
) )
async def async_select_source(self, source): async def async_select_source(self, source: str) -> None:
"""Select input source.""" """Select input source."""
await self.coordinator.musiccast.select_source(self._zone_id, source) await self.coordinator.musiccast.select_source(self._zone_id, source)
@ -685,7 +688,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
# Services # Services
async def async_join_players(self, group_members): async def async_join_players(self, group_members: list[str]) -> None:
"""Add all clients given in entities to the group of the server. """Add all clients given in entities to the group of the server.
Creates a new group if necessary. Used for join service. Creates a new group if necessary. Used for join service.
@ -752,7 +755,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
await self.update_all_mc_entities(True) await self.update_all_mc_entities(True)
async def async_unjoin_player(self): async def async_unjoin_player(self) -> None:
"""Leave the group. """Leave the group.
Stops the distribution if device is server. Used for unjoin service. Stops the distribution if device is server. Used for unjoin service.

View File

@ -28,7 +28,7 @@ async def async_setup_entry(
class YeelightNightlightModeSensor(YeelightEntity, BinarySensorEntity): class YeelightNightlightModeSensor(YeelightEntity, BinarySensorEntity):
"""Representation of a Yeelight nightlight mode sensor.""" """Representation of a Yeelight nightlight mode sensor."""
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Handle entity which will be added.""" """Handle entity which will be added."""
self.async_on_remove( self.async_on_remove(
async_dispatcher_connect( async_dispatcher_connect(

View File

@ -115,7 +115,7 @@ class YoLinkClimateEntity(YoLinkEntity, ClimateEntity):
self._attr_fan_mode = fan_mode self._attr_fan_mode = fan_mode
self.async_write_ha_state() self.async_write_ha_state()
async def async_set_temperature(self, **kwargs) -> None: async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set temperature.""" """Set temperature."""
target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW) target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW)
target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH) target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH)