From d8d054e7dd62ce0f0e83c48cf7c466b266edb989 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 17 Feb 2025 14:45:00 +0100 Subject: [PATCH] Improve type hints in base entities (#138708) --- homeassistant/components/broadlink/entity.py | 6 +++--- homeassistant/components/enocean/entity.py | 2 +- homeassistant/components/flo/entity.py | 4 ++-- homeassistant/components/hlk_sw16/entity.py | 4 ++-- homeassistant/components/homematic/entity.py | 4 ++-- homeassistant/components/ihc/entity.py | 2 +- homeassistant/components/insteon/entity.py | 4 ++-- homeassistant/components/lupusec/entity.py | 2 +- homeassistant/components/lutron_caseta/entity.py | 2 +- homeassistant/components/onvif/entity.py | 2 +- homeassistant/components/pilight/entity.py | 4 ++-- homeassistant/components/plaato/entity.py | 4 ++-- homeassistant/components/point/entity.py | 4 ++-- homeassistant/components/qwikswitch/entity.py | 2 +- homeassistant/components/raincloud/entity.py | 2 +- homeassistant/components/rflink/entity.py | 8 ++++---- homeassistant/components/roomba/entity.py | 2 +- homeassistant/components/soma/entity.py | 2 +- homeassistant/components/starline/entity.py | 8 ++++---- homeassistant/components/tellduslive/entity.py | 6 +++--- homeassistant/components/tellstick/entity.py | 4 ++-- homeassistant/components/upb/entity.py | 4 ++-- homeassistant/components/velux/entity.py | 2 +- homeassistant/components/vera/entity.py | 4 ++-- homeassistant/components/volvooncall/entity.py | 2 +- homeassistant/components/wiffi/entity.py | 2 +- homeassistant/components/wirelesstag/entity.py | 4 ++-- homeassistant/components/xiaomi_aqara/entity.py | 4 ++-- homeassistant/components/xiaomi_miio/entity.py | 2 +- homeassistant/components/xs1/entity.py | 2 +- homeassistant/components/yamaha_musiccast/entity.py | 4 ++-- 31 files changed, 54 insertions(+), 54 deletions(-) diff --git a/homeassistant/components/broadlink/entity.py b/homeassistant/components/broadlink/entity.py index 6c956d8c80a..a97374680f9 100644 --- a/homeassistant/components/broadlink/entity.py +++ b/homeassistant/components/broadlink/entity.py @@ -17,13 +17,13 @@ class BroadlinkEntity(Entity): self._device = device self._coordinator = device.update_manager.coordinator - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when the entity is added to hass.""" self.async_on_remove(self._coordinator.async_add_listener(self._recv_data)) if self._coordinator.data: self._update_state(self._coordinator.data) - async def async_update(self): + async def async_update(self) -> None: """Update the state of the entity.""" await self._coordinator.async_request_refresh() @@ -49,7 +49,7 @@ class BroadlinkEntity(Entity): """ @property - def available(self): + def available(self) -> bool: """Return True if the entity is available.""" return self._device.available diff --git a/homeassistant/components/enocean/entity.py b/homeassistant/components/enocean/entity.py index 5c12fc12a68..b2d73e65443 100644 --- a/homeassistant/components/enocean/entity.py +++ b/homeassistant/components/enocean/entity.py @@ -16,7 +16,7 @@ class EnOceanEntity(Entity): """Initialize the device.""" self.dev_id = dev_id - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register callbacks.""" self.async_on_remove( async_dispatcher_connect( diff --git a/homeassistant/components/flo/entity.py b/homeassistant/components/flo/entity.py index b0cf8d04313..072afbae4f2 100644 --- a/homeassistant/components/flo/entity.py +++ b/homeassistant/components/flo/entity.py @@ -45,10 +45,10 @@ class FloEntity(Entity): """Return True if device is available.""" return self._device.available - async def async_update(self): + async def async_update(self) -> None: """Update Flo entity.""" await self._device.async_request_refresh() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """When entity is added to hass.""" self.async_on_remove(self._device.async_add_listener(self.async_write_ha_state)) diff --git a/homeassistant/components/hlk_sw16/entity.py b/homeassistant/components/hlk_sw16/entity.py index fdef5f6764b..91510760968 100644 --- a/homeassistant/components/hlk_sw16/entity.py +++ b/homeassistant/components/hlk_sw16/entity.py @@ -35,7 +35,7 @@ class SW16Entity(Entity): self.async_write_ha_state() @property - def available(self): + def available(self) -> bool: """Return True if entity is available.""" return bool(self._client.is_connected) @@ -44,7 +44,7 @@ class SW16Entity(Entity): """Update availability state.""" self.async_write_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register update callback.""" self._client.register_status_callback( self.handle_event_callback, self._device_port diff --git a/homeassistant/components/homematic/entity.py b/homeassistant/components/homematic/entity.py index 5a5b2a3b8c8..44e95e98f38 100644 --- a/homeassistant/components/homematic/entity.py +++ b/homeassistant/components/homematic/entity.py @@ -62,7 +62,7 @@ class HMDevice(Entity): if self._state: self._state = self._state.upper() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Load data init callbacks.""" self._subscribe_homematic_events() @@ -77,7 +77,7 @@ class HMDevice(Entity): return self._name @property - def available(self): + def available(self) -> bool: """Return true if device is available.""" return self._available diff --git a/homeassistant/components/ihc/entity.py b/homeassistant/components/ihc/entity.py index f90b2ee943c..8847ffc9f49 100644 --- a/homeassistant/components/ihc/entity.py +++ b/homeassistant/components/ihc/entity.py @@ -54,7 +54,7 @@ class IHCEntity(Entity): self.ihc_note = "" self.ihc_position = "" - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Add callback for IHC changes.""" _LOGGER.debug("Adding IHC entity notify event: %s", self.ihc_id) self.ihc_controller.add_notify_event(self.ihc_id, self.on_ihc_change, True) diff --git a/homeassistant/components/insteon/entity.py b/homeassistant/components/insteon/entity.py index 79e5c18a934..b7886723fdf 100644 --- a/homeassistant/components/insteon/entity.py +++ b/homeassistant/components/insteon/entity.py @@ -109,7 +109,7 @@ class InsteonEntity(Entity): ) self.async_write_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register INSTEON update events.""" _LOGGER.debug( "Tracking updates for device %s group %d name %s", @@ -137,7 +137,7 @@ class InsteonEntity(Entity): ) ) - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Unsubscribe to INSTEON update events.""" _LOGGER.debug( "Remove tracking updates for device %s group %d name %s", diff --git a/homeassistant/components/lupusec/entity.py b/homeassistant/components/lupusec/entity.py index dc0dac89dc8..8cfb559b84f 100644 --- a/homeassistant/components/lupusec/entity.py +++ b/homeassistant/components/lupusec/entity.py @@ -18,7 +18,7 @@ class LupusecDevice(Entity): self._device = device self._attr_unique_id = device.device_id - def update(self): + def update(self) -> None: """Update automation state.""" self._device.refresh() diff --git a/homeassistant/components/lutron_caseta/entity.py b/homeassistant/components/lutron_caseta/entity.py index f954be74f1d..5ab211ed87b 100644 --- a/homeassistant/components/lutron_caseta/entity.py +++ b/homeassistant/components/lutron_caseta/entity.py @@ -63,7 +63,7 @@ class LutronCasetaEntity(Entity): info[ATTR_SUGGESTED_AREA] = area self._attr_device_info = info - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register callbacks.""" self._smartbridge.add_subscriber(self.device_id, self.async_write_ha_state) diff --git a/homeassistant/components/onvif/entity.py b/homeassistant/components/onvif/entity.py index c9900106256..783df743e86 100644 --- a/homeassistant/components/onvif/entity.py +++ b/homeassistant/components/onvif/entity.py @@ -17,7 +17,7 @@ class ONVIFBaseEntity(Entity): self.device: ONVIFDevice = device @property - def available(self): + def available(self) -> bool: """Return True if device is available.""" return self.device.available diff --git a/homeassistant/components/pilight/entity.py b/homeassistant/components/pilight/entity.py index fbb924d7f8f..fbfa5cfb5e1 100644 --- a/homeassistant/components/pilight/entity.py +++ b/homeassistant/components/pilight/entity.py @@ -86,7 +86,7 @@ class PilightBaseDevice(RestoreEntity): self._brightness = 255 - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity about to be added to hass.""" await super().async_added_to_hass() if state := await self.async_get_last_state(): @@ -99,7 +99,7 @@ class PilightBaseDevice(RestoreEntity): return self._name @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return True if unable to access real state of the entity.""" return True diff --git a/homeassistant/components/plaato/entity.py b/homeassistant/components/plaato/entity.py index 7ab8367bd1d..9cc63a38a64 100644 --- a/homeassistant/components/plaato/entity.py +++ b/homeassistant/components/plaato/entity.py @@ -73,13 +73,13 @@ class PlaatoEntity(entity.Entity): return None @property - def available(self): + def available(self) -> bool: """Return if sensor is available.""" if self._coordinator is not None: return self._coordinator.last_update_success return True - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """When entity is added to hass.""" if self._coordinator is not None: self.async_on_remove( diff --git a/homeassistant/components/point/entity.py b/homeassistant/components/point/entity.py index 4784dd43180..5c52e81e6f7 100644 --- a/homeassistant/components/point/entity.py +++ b/homeassistant/components/point/entity.py @@ -52,7 +52,7 @@ class MinutPointEntity(Entity): ) await self._update_callback() - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Disconnect dispatcher listener when removed.""" if self._async_unsub_dispatcher_connect: self._async_unsub_dispatcher_connect() @@ -61,7 +61,7 @@ class MinutPointEntity(Entity): """Update the value of the sensor.""" @property - def available(self): + def available(self) -> bool: """Return true if device is not offline.""" return self._client.is_available(self.device_id) diff --git a/homeassistant/components/qwikswitch/entity.py b/homeassistant/components/qwikswitch/entity.py index 3a2ec5a9206..ff7a1d2e98a 100644 --- a/homeassistant/components/qwikswitch/entity.py +++ b/homeassistant/components/qwikswitch/entity.py @@ -35,7 +35,7 @@ class QSEntity(Entity): """Receive update packet from QSUSB. Match dispather_send signature.""" self.async_write_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Listen for updates from QSUSb via dispatcher.""" self.async_on_remove( async_dispatcher_connect(self.hass, self.qsid, self.update_packet) diff --git a/homeassistant/components/raincloud/entity.py b/homeassistant/components/raincloud/entity.py index 337324d96eb..b45684ac72b 100644 --- a/homeassistant/components/raincloud/entity.py +++ b/homeassistant/components/raincloud/entity.py @@ -45,7 +45,7 @@ class RainCloudEntity(Entity): """Return the name of the sensor.""" return self._name - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register callbacks.""" self.async_on_remove( async_dispatcher_connect( diff --git a/homeassistant/components/rflink/entity.py b/homeassistant/components/rflink/entity.py index 26153acf7ba..0caec4ea2c3 100644 --- a/homeassistant/components/rflink/entity.py +++ b/homeassistant/components/rflink/entity.py @@ -105,12 +105,12 @@ class RflinkDevice(Entity): return self._state @property - def assumed_state(self): + def assumed_state(self) -> bool: """Assume device state until first device event sets state.""" return self._state is None @property - def available(self): + def available(self) -> bool: """Return True if entity is available.""" return self._available @@ -120,7 +120,7 @@ class RflinkDevice(Entity): self._available = availability self.async_write_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register update callback.""" await super().async_added_to_hass() # Remove temporary bogus entity_id if added @@ -300,7 +300,7 @@ class RflinkCommand(RflinkDevice): class SwitchableRflinkDevice(RflinkCommand, RestoreEntity): """Rflink entity which can switch on/off (eg: light, switch).""" - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Restore RFLink device state (ON/OFF).""" await super().async_added_to_hass() if (old_state := await self.async_get_last_state()) is not None: diff --git a/homeassistant/components/roomba/entity.py b/homeassistant/components/roomba/entity.py index ae5577da4e4..14c7ac3af3e 100644 --- a/homeassistant/components/roomba/entity.py +++ b/homeassistant/components/roomba/entity.py @@ -80,7 +80,7 @@ class IRobotEntity(Entity): return None return dt_util.utc_from_timestamp(ts) - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register callback function.""" self.vacuum.register_on_message_callback(self.on_message) diff --git a/homeassistant/components/soma/entity.py b/homeassistant/components/soma/entity.py index f9824d107b1..4b2fcee5405 100644 --- a/homeassistant/components/soma/entity.py +++ b/homeassistant/components/soma/entity.py @@ -71,7 +71,7 @@ class SomaEntity(Entity): self.api_is_available = True @property - def available(self): + def available(self) -> bool: """Return true if the last API commands returned successfully.""" return self.is_available diff --git a/homeassistant/components/starline/entity.py b/homeassistant/components/starline/entity.py index 74807996dfb..f8846c2a97f 100644 --- a/homeassistant/components/starline/entity.py +++ b/homeassistant/components/starline/entity.py @@ -27,20 +27,20 @@ class StarlineEntity(Entity): self._unsubscribe_api: Callable | None = None @property - def available(self): + def available(self) -> bool: """Return True if entity is available.""" return self._account.api.available - def update(self): + def update(self) -> None: """Read new state data.""" self.schedule_update_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity about to be added to Home Assistant.""" await super().async_added_to_hass() self._unsubscribe_api = self._account.api.add_update_listener(self.update) - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Call when entity is being removed from Home Assistant.""" await super().async_will_remove_from_hass() if self._unsubscribe_api is not None: diff --git a/homeassistant/components/tellduslive/entity.py b/homeassistant/components/tellduslive/entity.py index a71fcb685c0..5366e4c27df 100644 --- a/homeassistant/components/tellduslive/entity.py +++ b/homeassistant/components/tellduslive/entity.py @@ -33,7 +33,7 @@ class TelldusLiveEntity(Entity): self._id = device_id self._client = client - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Call when entity is added to hass.""" _LOGGER.debug("Created device %s", self) self.async_on_remove( @@ -58,12 +58,12 @@ class TelldusLiveEntity(Entity): return self.device.state @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if unable to access real state of entity.""" return True @property - def available(self): + def available(self) -> bool: """Return true if device is not offline.""" return self._client.is_available(self.device_id) diff --git a/homeassistant/components/tellstick/entity.py b/homeassistant/components/tellstick/entity.py index 746c7f4dd4d..5be3d1f48f4 100644 --- a/homeassistant/components/tellstick/entity.py +++ b/homeassistant/components/tellstick/entity.py @@ -40,7 +40,7 @@ class TellstickDevice(Entity): self._attr_name = tellcore_device.name self._attr_unique_id = tellcore_device.id - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register callbacks.""" self.async_on_remove( async_dispatcher_connect( @@ -146,6 +146,6 @@ class TellstickDevice(Entity): except TelldusError as err: _LOGGER.error(err) - def update(self): + def update(self) -> None: """Poll the current state of the device.""" self._update_from_tellcore() diff --git a/homeassistant/components/upb/entity.py b/homeassistant/components/upb/entity.py index 13037adf680..8a9afa453b1 100644 --- a/homeassistant/components/upb/entity.py +++ b/homeassistant/components/upb/entity.py @@ -30,7 +30,7 @@ class UpbEntity(Entity): return self._element.as_dict() @property - def available(self): + def available(self) -> bool: """Is the entity available to be updated.""" return self._upb.is_connected() @@ -43,7 +43,7 @@ class UpbEntity(Entity): self._element_changed(element, changeset) self.async_write_ha_state() - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Register callback for UPB changes and update entity state.""" self._element.add_callback(self._element_callback) self._element_callback(self._element, {}) diff --git a/homeassistant/components/velux/entity.py b/homeassistant/components/velux/entity.py index 674ba5dde45..1231a98e0a8 100644 --- a/homeassistant/components/velux/entity.py +++ b/homeassistant/components/velux/entity.py @@ -31,6 +31,6 @@ class VeluxEntity(Entity): self.node.register_device_updated_cb(after_update_callback) - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Store register state change callback.""" self.async_register_callbacks() diff --git a/homeassistant/components/vera/entity.py b/homeassistant/components/vera/entity.py index 84e21e54983..b3013c288c1 100644 --- a/homeassistant/components/vera/entity.py +++ b/homeassistant/components/vera/entity.py @@ -52,7 +52,7 @@ class VeraEntity[_DeviceTypeT: veraApi.VeraDevice](Entity): """Update the state.""" self.schedule_update_ha_state(True) - def update(self): + def update(self) -> None: """Force a refresh from the device if the device is unavailable.""" refresh_needed = self.vera_device.should_poll or not self.available _LOGGER.debug("%s: update called (refresh=%s)", self._name, refresh_needed) @@ -90,7 +90,7 @@ class VeraEntity[_DeviceTypeT: veraApi.VeraDevice](Entity): return attr @property - def available(self): + def available(self) -> bool: """If device communications have failed return false.""" return not self.vera_device.comm_failure diff --git a/homeassistant/components/volvooncall/entity.py b/homeassistant/components/volvooncall/entity.py index 6ebc4bdc754..5a1194e8b1a 100644 --- a/homeassistant/components/volvooncall/entity.py +++ b/homeassistant/components/volvooncall/entity.py @@ -57,7 +57,7 @@ class VolvoEntity(CoordinatorEntity[VolvoUpdateCoordinator]): return f"{self._vehicle_name} {self._entity_name}" @property - def assumed_state(self): + def assumed_state(self) -> bool: """Return true if unable to access real state of entity.""" return True diff --git a/homeassistant/components/wiffi/entity.py b/homeassistant/components/wiffi/entity.py index fd774c930c8..84bbc9b3df1 100644 --- a/homeassistant/components/wiffi/entity.py +++ b/homeassistant/components/wiffi/entity.py @@ -41,7 +41,7 @@ class WiffiEntity(Entity): self._value = None self._timeout = options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT) - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Entity has been added to hass.""" self.async_on_remove( async_dispatcher_connect( diff --git a/homeassistant/components/wirelesstag/entity.py b/homeassistant/components/wirelesstag/entity.py index 31f8ee99d0d..73b13cdc397 100644 --- a/homeassistant/components/wirelesstag/entity.py +++ b/homeassistant/components/wirelesstag/entity.py @@ -60,11 +60,11 @@ class WirelessTagBaseSensor(Entity): return f"{value:.1f}" @property - def available(self): + def available(self) -> bool: """Return True if entity is available.""" return self._tag.is_alive - def update(self): + def update(self) -> None: """Update state.""" if not self.should_poll: return diff --git a/homeassistant/components/xiaomi_aqara/entity.py b/homeassistant/components/xiaomi_aqara/entity.py index db47015c0cf..59107984ddf 100644 --- a/homeassistant/components/xiaomi_aqara/entity.py +++ b/homeassistant/components/xiaomi_aqara/entity.py @@ -57,7 +57,7 @@ class XiaomiDevice(Entity): self._is_gateway = False self._device_id = self._sid - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Start unavailability tracking.""" self._xiaomi_hub.callbacks[self._sid].append(self.push_data) self._async_track_unavailable() @@ -100,7 +100,7 @@ class XiaomiDevice(Entity): return device_info @property - def available(self): + def available(self) -> bool: """Return True if entity is available.""" return self._is_available diff --git a/homeassistant/components/xiaomi_miio/entity.py b/homeassistant/components/xiaomi_miio/entity.py index 0343a7526d7..ba1148985ba 100644 --- a/homeassistant/components/xiaomi_miio/entity.py +++ b/homeassistant/components/xiaomi_miio/entity.py @@ -185,7 +185,7 @@ class XiaomiGatewayDevice(CoordinatorEntity, Entity): ) @property - def available(self): + def available(self) -> bool: """Return if entity is available.""" if self.coordinator.data is None: return False diff --git a/homeassistant/components/xs1/entity.py b/homeassistant/components/xs1/entity.py index 7239a6fd446..c1ec43ec33c 100644 --- a/homeassistant/components/xs1/entity.py +++ b/homeassistant/components/xs1/entity.py @@ -17,7 +17,7 @@ class XS1DeviceEntity(Entity): """Initialize the XS1 device.""" self.device = device - async def async_update(self): + async def async_update(self) -> None: """Retrieve latest device state.""" async with UPDATE_LOCK: await self.hass.async_add_executor_job(self.device.update) diff --git a/homeassistant/components/yamaha_musiccast/entity.py b/homeassistant/components/yamaha_musiccast/entity.py index 4f1add825e4..8023b13c10a 100644 --- a/homeassistant/components/yamaha_musiccast/entity.py +++ b/homeassistant/components/yamaha_musiccast/entity.py @@ -78,13 +78,13 @@ class MusicCastDeviceEntity(MusicCastEntity): return device_info - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Run when this Entity has been added to HA.""" await super().async_added_to_hass() # All entities should register callbacks to update HA when their state changes self.coordinator.musiccast.register_callback(self.async_write_ha_state) - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Entity being removed from hass.""" await super().async_will_remove_from_hass() self.coordinator.musiccast.remove_callback(self.async_write_ha_state)