From b043053aadab57435c768d1e0fe3892367686999 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 26 Aug 2022 11:37:12 +0200 Subject: [PATCH] Improve entity type hints [g] (#77145) --- .../components/gc100/binary_sensor.py | 2 +- homeassistant/components/gc100/switch.py | 8 +++++--- homeassistant/components/gdacs/sensor.py | 4 ++-- .../components/generic_thermostat/climate.py | 7 ++++--- homeassistant/components/geniushub/switch.py | 5 +++-- .../components/geniushub/water_heater.py | 2 +- .../components/geo_rss_events/sensor.py | 2 +- .../components/geofency/device_tracker.py | 4 ++-- .../components/geonetnz_quakes/sensor.py | 4 ++-- .../components/geonetnz_volcano/sensor.py | 4 ++-- homeassistant/components/gitlab_ci/sensor.py | 2 +- homeassistant/components/gitter/sensor.py | 2 +- homeassistant/components/glances/sensor.py | 8 ++++---- .../components/google_travel_time/sensor.py | 2 +- homeassistant/components/google_wifi/sensor.py | 11 ++++++++--- .../components/gpslogger/device_tracker.py | 4 ++-- homeassistant/components/gree/climate.py | 9 +++++---- homeassistant/components/gree/switch.py | 18 ++++++++++-------- .../components/growatt_server/sensor.py | 2 +- .../components/gstreamer/media_player.py | 17 ++++++++++------- 20 files changed, 66 insertions(+), 51 deletions(-) diff --git a/homeassistant/components/gc100/binary_sensor.py b/homeassistant/components/gc100/binary_sensor.py index f93076196a34..e750f928cf76 100644 --- a/homeassistant/components/gc100/binary_sensor.py +++ b/homeassistant/components/gc100/binary_sensor.py @@ -59,7 +59,7 @@ class GC100BinarySensor(BinarySensorEntity): """Return the state of the entity.""" return self._state - def update(self): + def update(self) -> None: """Update the sensor state.""" self._gc100.read_sensor(self._port_addr, self.set_state) diff --git a/homeassistant/components/gc100/switch.py b/homeassistant/components/gc100/switch.py index 3372203231fb..d88b4c9fa79a 100644 --- a/homeassistant/components/gc100/switch.py +++ b/homeassistant/components/gc100/switch.py @@ -1,6 +1,8 @@ """Support for switches using GC100.""" from __future__ import annotations +from typing import Any + import voluptuous as vol from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity @@ -54,15 +56,15 @@ class GC100Switch(SwitchEntity): """Return the state of the entity.""" return self._state - def turn_on(self, **kwargs): + def turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" self._gc100.write_switch(self._port_addr, 1, self.set_state) - def turn_off(self, **kwargs): + def turn_off(self, **kwargs: Any) -> None: """Turn the device off.""" self._gc100.write_switch(self._port_addr, 0, self.set_state) - def update(self): + def update(self) -> None: """Update the sensor state.""" self._gc100.read_sensor(self._port_addr, self.set_state) diff --git a/homeassistant/components/gdacs/sensor.py b/homeassistant/components/gdacs/sensor.py index c5d8e76b2897..531eb05dcf9e 100644 --- a/homeassistant/components/gdacs/sensor.py +++ b/homeassistant/components/gdacs/sensor.py @@ -59,7 +59,7 @@ class GdacsSensor(SensorEntity): self._removed = None self._remove_signal_status = None - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity is added to hass.""" self._remove_signal_status = async_dispatcher_connect( self.hass, @@ -81,7 +81,7 @@ class GdacsSensor(SensorEntity): _LOGGER.debug("Received status update for %s", self._config_entry_id) self.async_schedule_update_ha_state(True) - async def async_update(self): + async def async_update(self) -> None: """Update this entity from the data held in the feed manager.""" _LOGGER.debug("Updating %s", self._config_entry_id) if self._manager: diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index c2eecf413f76..9dd49dd851d4 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio import logging import math +from typing import Any import voluptuous as vol @@ -366,7 +367,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): """List of available operation modes.""" return self._hvac_list - async def async_set_hvac_mode(self, hvac_mode): + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: """Set hvac mode.""" if hvac_mode == HVACMode.HEAT: self._hvac_mode = HVACMode.HEAT @@ -384,7 +385,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): # Ensure we update the current operation after changing the mode self.async_write_ha_state() - async def async_set_temperature(self, **kwargs): + async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: return @@ -537,7 +538,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): HA_DOMAIN, SERVICE_TURN_OFF, data, context=self._context ) - async def async_set_preset_mode(self, preset_mode: str): + async def async_set_preset_mode(self, preset_mode: str) -> None: """Set new preset mode.""" if preset_mode not in (self._attr_preset_modes or []): raise ValueError( diff --git a/homeassistant/components/geniushub/switch.py b/homeassistant/components/geniushub/switch.py index acf747f3e511..cf29d0ea8028 100644 --- a/homeassistant/components/geniushub/switch.py +++ b/homeassistant/components/geniushub/switch.py @@ -2,6 +2,7 @@ from __future__ import annotations from datetime import timedelta +from typing import Any import voluptuous as vol @@ -71,13 +72,13 @@ class GeniusSwitch(GeniusZone, SwitchEntity): """ return self._zone.data["mode"] == "override" and self._zone.data["setpoint"] - async def async_turn_off(self, **kwargs) -> None: + async def async_turn_off(self, **kwargs: Any) -> None: """Send the zone to Timer mode. The zone is deemed 'off' in this mode, although the plugs may actually be on. """ await self._zone.set_mode("timer") - async def async_turn_on(self, **kwargs) -> None: + async def async_turn_on(self, **kwargs: Any) -> None: """Set the zone to override/on ({'setpoint': true}) for x seconds.""" await self._zone.set_override(1, kwargs.get(ATTR_DURATION, 3600)) diff --git a/homeassistant/components/geniushub/water_heater.py b/homeassistant/components/geniushub/water_heater.py index 3a6b446b8db4..ea8b1a439616 100644 --- a/homeassistant/components/geniushub/water_heater.py +++ b/homeassistant/components/geniushub/water_heater.py @@ -78,6 +78,6 @@ class GeniusWaterHeater(GeniusHeatingZone, WaterHeaterEntity): """Return the current operation mode.""" return GH_STATE_TO_HA[self._zone.data["mode"]] # type: ignore[return-value] - async def async_set_operation_mode(self, operation_mode) -> None: + async def async_set_operation_mode(self, operation_mode: str) -> None: """Set a new operation mode for this boiler.""" await self._zone.set_mode(HA_OPMODE_TO_GH[operation_mode]) diff --git a/homeassistant/components/geo_rss_events/sensor.py b/homeassistant/components/geo_rss_events/sensor.py index 5605ffc803b6..eba903a4cdf8 100644 --- a/homeassistant/components/geo_rss_events/sensor.py +++ b/homeassistant/components/geo_rss_events/sensor.py @@ -150,7 +150,7 @@ class GeoRssServiceSensor(SensorEntity): """Return the state attributes.""" return self._state_attributes - def update(self): + def update(self) -> None: """Update this sensor from the GeoRSS service.""" status, feed_entries = self._feed.update() diff --git a/homeassistant/components/geofency/device_tracker.py b/homeassistant/components/geofency/device_tracker.py index 61deb9ede7d3..85197239ccd3 100644 --- a/homeassistant/components/geofency/device_tracker.py +++ b/homeassistant/components/geofency/device_tracker.py @@ -100,7 +100,7 @@ class GeofencyEntity(TrackerEntity, RestoreEntity): """Return the source type, eg gps or router, of the device.""" return SourceType.GPS - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register state update callback.""" await super().async_added_to_hass() self._unsub_dispatcher = async_dispatcher_connect( @@ -117,7 +117,7 @@ class GeofencyEntity(TrackerEntity, RestoreEntity): attr = state.attributes self._gps = (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE)) - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Clean up after entity before removal.""" await super().async_will_remove_from_hass() self._unsub_dispatcher() diff --git a/homeassistant/components/geonetnz_quakes/sensor.py b/homeassistant/components/geonetnz_quakes/sensor.py index 357c86a3b8f6..9183aead1690 100644 --- a/homeassistant/components/geonetnz_quakes/sensor.py +++ b/homeassistant/components/geonetnz_quakes/sensor.py @@ -60,7 +60,7 @@ class GeonetnzQuakesSensor(SensorEntity): self._removed = None self._remove_signal_status = None - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity is added to hass.""" self._remove_signal_status = async_dispatcher_connect( self.hass, @@ -82,7 +82,7 @@ class GeonetnzQuakesSensor(SensorEntity): _LOGGER.debug("Received status update for %s", self._config_entry_id) self.async_schedule_update_ha_state(True) - async def async_update(self): + async def async_update(self) -> None: """Update this entity from the data held in the feed manager.""" _LOGGER.debug("Updating %s", self._config_entry_id) if self._manager: diff --git a/homeassistant/components/geonetnz_volcano/sensor.py b/homeassistant/components/geonetnz_volcano/sensor.py index e11a9394579a..add35bfbcd7b 100644 --- a/homeassistant/components/geonetnz_volcano/sensor.py +++ b/homeassistant/components/geonetnz_volcano/sensor.py @@ -81,7 +81,7 @@ class GeonetnzVolcanoSensor(SensorEntity): self._feed_last_update_successful = None self._remove_signal_update = None - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity is added to hass.""" self._remove_signal_update = async_dispatcher_connect( self.hass, @@ -99,7 +99,7 @@ class GeonetnzVolcanoSensor(SensorEntity): """Call update method.""" self.async_schedule_update_ha_state(True) - async def async_update(self): + async def async_update(self) -> None: """Update this entity from the data held in the feed manager.""" _LOGGER.debug("Updating %s", self._external_id) feed_entry = self._feed_manager.get_entry(self._external_id) diff --git a/homeassistant/components/gitlab_ci/sensor.py b/homeassistant/components/gitlab_ci/sensor.py index 4206a1184adf..21e1221e6b88 100644 --- a/homeassistant/components/gitlab_ci/sensor.py +++ b/homeassistant/components/gitlab_ci/sensor.py @@ -126,7 +126,7 @@ class GitLabSensor(SensorEntity): return ICON_SAD return ICON_OTHER - def update(self): + def update(self) -> None: """Collect updated data from GitLab API.""" self._gitlab_data.update() diff --git a/homeassistant/components/gitter/sensor.py b/homeassistant/components/gitter/sensor.py index 3069ea04347c..514cb9e0ad53 100644 --- a/homeassistant/components/gitter/sensor.py +++ b/homeassistant/components/gitter/sensor.py @@ -98,7 +98,7 @@ class GitterSensor(SensorEntity): """Return the icon to use in the frontend, if any.""" return ICON - def update(self): + def update(self) -> None: """Get the latest data and updates the state.""" try: diff --git a/homeassistant/components/glances/sensor.py b/homeassistant/components/glances/sensor.py index af6f307ef3ae..0b2ce1801e16 100644 --- a/homeassistant/components/glances/sensor.py +++ b/homeassistant/components/glances/sensor.py @@ -20,7 +20,7 @@ from homeassistant.const import ( TEMP_CELSIUS, Platform, ) -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.helpers import entity_registry from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import DeviceInfo @@ -330,7 +330,7 @@ class GlancesSensor(SensorEntity): self.glances_data = glances_data self._sensor_name_prefix = sensor_name_prefix self._state = None - self.unsub_update = None + self.unsub_update: CALLBACK_TYPE | None = None self.entity_description = description self._attr_name = f"{sensor_name_prefix} {description.name_suffix}" @@ -342,7 +342,7 @@ class GlancesSensor(SensorEntity): self._attr_unique_id = f"{self.glances_data.config_entry.entry_id}-{sensor_name_prefix}-{description.key}" @property - def available(self): + def available(self) -> bool: """Could the device be accessed during the last update call.""" return self.glances_data.available @@ -351,7 +351,7 @@ class GlancesSensor(SensorEntity): """Return the state of the resources.""" return self._state - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Handle entity which will be added.""" self.unsub_update = async_dispatcher_connect( self.hass, DATA_UPDATED, self._schedule_immediate_update diff --git a/homeassistant/components/google_travel_time/sensor.py b/homeassistant/components/google_travel_time/sensor.py index 3ee2a18455c1..df5221dea2f1 100644 --- a/homeassistant/components/google_travel_time/sensor.py +++ b/homeassistant/components/google_travel_time/sensor.py @@ -186,7 +186,7 @@ class GoogleTravelTimeSensor(SensorEntity): await self.hass.async_add_executor_job(self.update) self.async_write_ha_state() - def update(self): + def update(self) -> None: """Get the latest data from Google.""" options_copy = self._config_entry.options.copy() dtime = options_copy.get(CONF_DEPARTURE_TIME) diff --git a/homeassistant/components/google_wifi/sensor.py b/homeassistant/components/google_wifi/sensor.py index 8c4cae9bd79a..2ebbd44b81b2 100644 --- a/homeassistant/components/google_wifi/sensor.py +++ b/homeassistant/components/google_wifi/sensor.py @@ -136,18 +136,23 @@ class GoogleWifiSensor(SensorEntity): entity_description: GoogleWifiSensorEntityDescription - def __init__(self, api, name, description: GoogleWifiSensorEntityDescription): + def __init__( + self, + api: GoogleWifiAPI, + name: str, + description: GoogleWifiSensorEntityDescription, + ) -> None: """Initialize a Google Wifi sensor.""" self.entity_description = description self._api = api self._attr_name = f"{name}_{description.key}" @property - def available(self): + def available(self) -> bool: """Return availability of Google Wifi API.""" return self._api.available - def update(self): + def update(self) -> None: """Get the latest data from the Google Wifi API.""" self._api.update() if self.available: diff --git a/homeassistant/components/gpslogger/device_tracker.py b/homeassistant/components/gpslogger/device_tracker.py index 22e9529706f4..f18b486917a9 100644 --- a/homeassistant/components/gpslogger/device_tracker.py +++ b/homeassistant/components/gpslogger/device_tracker.py @@ -122,7 +122,7 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity): """Return the source type, eg gps or router, of the device.""" return SourceType.GPS - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register state update callback.""" await super().async_added_to_hass() self._unsub_dispatcher = async_dispatcher_connect( @@ -158,7 +158,7 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity): } self._battery = attr.get(ATTR_BATTERY_LEVEL) - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Clean up after entity before removal.""" await super().async_will_remove_from_hass() self._unsub_dispatcher() diff --git a/homeassistant/components/gree/climate.py b/homeassistant/components/gree/climate.py index 2b6833dff2c5..6d8f32aa21c4 100644 --- a/homeassistant/components/gree/climate.py +++ b/homeassistant/components/gree/climate.py @@ -2,6 +2,7 @@ from __future__ import annotations import logging +from typing import Any from greeclimate.device import ( TEMP_MAX, @@ -166,7 +167,7 @@ class GreeClimateEntity(CoordinatorEntity, ClimateEntity): """Return the target temperature for the device.""" return self.coordinator.device.target_temperature - async def async_set_temperature(self, **kwargs): + async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" if ATTR_TEMPERATURE not in kwargs: raise ValueError(f"Missing parameter {ATTR_TEMPERATURE}") @@ -265,7 +266,7 @@ class GreeClimateEntity(CoordinatorEntity, ClimateEntity): return PRESET_BOOST return PRESET_NONE - async def async_set_preset_mode(self, preset_mode): + async def async_set_preset_mode(self, preset_mode: str) -> None: """Set new preset mode.""" if preset_mode not in PRESET_MODES: raise ValueError(f"Invalid preset mode: {preset_mode}") @@ -304,7 +305,7 @@ class GreeClimateEntity(CoordinatorEntity, ClimateEntity): speed = self.coordinator.device.fan_speed return FAN_MODES.get(speed) - async def async_set_fan_mode(self, fan_mode): + async def async_set_fan_mode(self, fan_mode: str) -> None: """Set new target fan mode.""" if fan_mode not in FAN_MODES_REVERSE: raise ValueError(f"Invalid fan mode: {fan_mode}") @@ -332,7 +333,7 @@ class GreeClimateEntity(CoordinatorEntity, ClimateEntity): return SWING_VERTICAL return SWING_OFF - async def async_set_swing_mode(self, swing_mode): + async def async_set_swing_mode(self, swing_mode: str) -> None: """Set new target swing operation.""" if swing_mode not in SWING_MODES: raise ValueError(f"Invalid swing mode: {swing_mode}") diff --git a/homeassistant/components/gree/switch.py b/homeassistant/components/gree/switch.py index 91e407d246b6..62189fdde063 100644 --- a/homeassistant/components/gree/switch.py +++ b/homeassistant/components/gree/switch.py @@ -1,6 +1,8 @@ """Support for interface with a Gree climate systems.""" from __future__ import annotations +from typing import Any + from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -60,13 +62,13 @@ class GreePanelLightSwitchEntity(GreeEntity, SwitchEntity): """Return if the light is turned on.""" return self.coordinator.device.light - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" self.coordinator.device.light = True await self.coordinator.push_state_update() self.async_write_ha_state() - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" self.coordinator.device.light = False await self.coordinator.push_state_update() @@ -90,13 +92,13 @@ class GreeQuietModeSwitchEntity(GreeEntity, SwitchEntity): """Return if the state is turned on.""" return self.coordinator.device.quiet - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" self.coordinator.device.quiet = True await self.coordinator.push_state_update() self.async_write_ha_state() - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" self.coordinator.device.quiet = False await self.coordinator.push_state_update() @@ -120,13 +122,13 @@ class GreeFreshAirSwitchEntity(GreeEntity, SwitchEntity): """Return if the state is turned on.""" return self.coordinator.device.fresh_air - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" self.coordinator.device.fresh_air = True await self.coordinator.push_state_update() self.async_write_ha_state() - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" self.coordinator.device.fresh_air = False await self.coordinator.push_state_update() @@ -150,13 +152,13 @@ class GreeXFanSwitchEntity(GreeEntity, SwitchEntity): """Return if the state is turned on.""" return self.coordinator.device.xfan - async def async_turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" self.coordinator.device.xfan = True await self.coordinator.push_state_update() self.async_write_ha_state() - async def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" self.coordinator.device.xfan = False await self.coordinator.push_state_update() diff --git a/homeassistant/components/growatt_server/sensor.py b/homeassistant/components/growatt_server/sensor.py index c90bfa6f3fbe..eceba2f7bce8 100644 --- a/homeassistant/components/growatt_server/sensor.py +++ b/homeassistant/components/growatt_server/sensor.py @@ -171,7 +171,7 @@ class GrowattInverter(SensorEntity): return self.probe.get_data("currency") return super().native_unit_of_measurement - def update(self): + def update(self) -> None: """Get the latest data from the Growat API and updates the state.""" self.probe.update() diff --git a/homeassistant/components/gstreamer/media_player.py b/homeassistant/components/gstreamer/media_player.py index 723be2880ff6..87329bdbc66f 100644 --- a/homeassistant/components/gstreamer/media_player.py +++ b/homeassistant/components/gstreamer/media_player.py @@ -2,6 +2,7 @@ from __future__ import annotations import logging +from typing import Any from gsp import GstreamerPlayer import voluptuous as vol @@ -78,7 +79,7 @@ class GstreamerDevice(MediaPlayerEntity): self._artist = None self._album = None - def update(self): + def update(self) -> None: """Update properties.""" self._state = self._player.state self._volume = self._player.volume @@ -88,11 +89,13 @@ class GstreamerDevice(MediaPlayerEntity): self._album = self._player.album self._artist = self._player.artist - def set_volume_level(self, volume): + def set_volume_level(self, volume: float) -> None: """Set the volume level.""" self._player.volume = volume - 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: """Play media.""" # Handle media_source if media_source.is_media_source_id(media_id): @@ -109,15 +112,15 @@ class GstreamerDevice(MediaPlayerEntity): await self.hass.async_add_executor_job(self._player.queue, media_id) - def media_play(self): + def media_play(self) -> None: """Play.""" self._player.play() - def media_pause(self): + def media_pause(self) -> None: """Pause.""" self._player.pause() - def media_next_track(self): + def media_next_track(self) -> None: """Next track.""" self._player.next() @@ -167,7 +170,7 @@ class GstreamerDevice(MediaPlayerEntity): return self._album async def async_browse_media( - self, media_content_type=None, media_content_id=None + self, media_content_type: str | None = None, media_content_id: str | None = None ) -> BrowseMedia: """Implement the websocket media browsing helper.""" return await media_source.async_browse_media(