From 581b554a665b169eb82e23215cb5f1fd6c4506f1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 16 Jan 2026 08:23:13 +0100 Subject: [PATCH] Improve type hints in digital_ocean (#161006) --- .../components/digital_ocean/__init__.py | 22 +++----------- .../components/digital_ocean/binary_sensor.py | 13 ++++---- .../components/digital_ocean/const.py | 30 +++++++++++++++++++ .../components/digital_ocean/switch.py | 6 ++-- 4 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 homeassistant/components/digital_ocean/const.py diff --git a/homeassistant/components/digital_ocean/__init__.py b/homeassistant/components/digital_ocean/__init__.py index 306ddc8e9a5..b4bd6ab1b92 100644 --- a/homeassistant/components/digital_ocean/__init__.py +++ b/homeassistant/components/digital_ocean/__init__.py @@ -1,6 +1,7 @@ """Support for Digital Ocean.""" -from datetime import timedelta +from __future__ import annotations + import logging import digitalocean @@ -12,27 +13,12 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType from homeassistant.util import Throttle +from .const import DATA_DIGITAL_OCEAN, DOMAIN, MIN_TIME_BETWEEN_UPDATES + _LOGGER = logging.getLogger(__name__) -ATTR_CREATED_AT = "created_at" -ATTR_DROPLET_ID = "droplet_id" -ATTR_DROPLET_NAME = "droplet_name" -ATTR_FEATURES = "features" -ATTR_IPV4_ADDRESS = "ipv4_address" -ATTR_IPV6_ADDRESS = "ipv6_address" -ATTR_MEMORY = "memory" -ATTR_REGION = "region" -ATTR_VCPUS = "vcpus" -ATTRIBUTION = "Data provided by Digital Ocean" - -CONF_DROPLETS = "droplets" - -DATA_DIGITAL_OCEAN = "data_do" DIGITAL_OCEAN_PLATFORMS = [Platform.SWITCH, Platform.BINARY_SENSOR] -DOMAIN = "digital_ocean" - -MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) CONFIG_SCHEMA = vol.Schema( {DOMAIN: vol.Schema({vol.Required(CONF_ACCESS_TOKEN): cv.string})}, diff --git a/homeassistant/components/digital_ocean/binary_sensor.py b/homeassistant/components/digital_ocean/binary_sensor.py index b0041f5220b..6439a97ade8 100644 --- a/homeassistant/components/digital_ocean/binary_sensor.py +++ b/homeassistant/components/digital_ocean/binary_sensor.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any import voluptuous as vol @@ -16,7 +17,7 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import ( +from .const import ( ATTR_CREATED_AT, ATTR_DROPLET_ID, ATTR_DROPLET_NAME, @@ -65,6 +66,7 @@ class DigitalOceanBinarySensor(BinarySensorEntity): """Representation of a Digital Ocean droplet sensor.""" _attr_attribution = ATTRIBUTION + _attr_device_class = BinarySensorDeviceClass.MOVING def __init__(self, do, droplet_id): """Initialize a new Digital Ocean sensor.""" @@ -79,17 +81,12 @@ class DigitalOceanBinarySensor(BinarySensorEntity): return self.data.name @property - def is_on(self): + def is_on(self) -> bool: """Return true if the binary sensor is on.""" return self.data.status == "active" @property - def device_class(self) -> BinarySensorDeviceClass: - """Return the class of this sensor.""" - return BinarySensorDeviceClass.MOVING - - @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Return the state attributes of the Digital Ocean droplet.""" return { ATTR_CREATED_AT: self.data.created_at, diff --git a/homeassistant/components/digital_ocean/const.py b/homeassistant/components/digital_ocean/const.py new file mode 100644 index 00000000000..77dfb1bf4e2 --- /dev/null +++ b/homeassistant/components/digital_ocean/const.py @@ -0,0 +1,30 @@ +"""Support for Digital Ocean.""" + +from __future__ import annotations + +from datetime import timedelta +from typing import TYPE_CHECKING + +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from . import DigitalOcean + +ATTR_CREATED_AT = "created_at" +ATTR_DROPLET_ID = "droplet_id" +ATTR_DROPLET_NAME = "droplet_name" +ATTR_FEATURES = "features" +ATTR_IPV4_ADDRESS = "ipv4_address" +ATTR_IPV6_ADDRESS = "ipv6_address" +ATTR_MEMORY = "memory" +ATTR_REGION = "region" +ATTR_VCPUS = "vcpus" + +ATTRIBUTION = "Data provided by Digital Ocean" + +CONF_DROPLETS = "droplets" + +DOMAIN = "digital_ocean" +DATA_DIGITAL_OCEAN: HassKey[DigitalOcean] = HassKey(DOMAIN) + +MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) diff --git a/homeassistant/components/digital_ocean/switch.py b/homeassistant/components/digital_ocean/switch.py index 409fa63c1c2..a3e6b4f95bf 100644 --- a/homeassistant/components/digital_ocean/switch.py +++ b/homeassistant/components/digital_ocean/switch.py @@ -16,7 +16,7 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import ( +from .const import ( ATTR_CREATED_AT, ATTR_DROPLET_ID, ATTR_DROPLET_NAME, @@ -80,12 +80,12 @@ class DigitalOceanSwitch(SwitchEntity): return self.data.name @property - def is_on(self): + def is_on(self) -> bool: """Return true if switch is on.""" return self.data.status == "active" @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Return the state attributes of the Digital Ocean droplet.""" return { ATTR_CREATED_AT: self.data.created_at,