UpCloud API and typing update (#50624)

This commit is contained in:
Ville Skyttä
2021-05-15 07:49:41 +03:00
committed by GitHub
parent 7221b1e09d
commit ed10856cc4
11 changed files with 71 additions and 27 deletions

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
import dataclasses
from datetime import timedelta
import logging
from typing import Dict
from typing import Any, Dict
import requests.exceptions
import upcloud_api
@@ -28,6 +28,7 @@ from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
@@ -117,7 +118,7 @@ class UpCloudHassData:
scan_interval_migrations: dict[str, int] = dataclasses.field(default_factory=dict)
async def async_setup(hass: HomeAssistant, config) -> bool:
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up UpCloud component."""
domain_config = config.get(DOMAIN)
if not domain_config:
@@ -228,7 +229,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True
async def async_unload_entry(hass, config_entry):
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload the config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, CONFIG_ENTRY_DOMAINS
@@ -242,7 +243,11 @@ async def async_unload_entry(hass, config_entry):
class UpCloudServerEntity(CoordinatorEntity):
"""Entity class for UpCloud servers."""
def __init__(self, coordinator, uuid):
def __init__(
self,
coordinator: DataUpdateCoordinator[dict[str, upcloud_api.Server]],
uuid: str,
) -> None:
"""Initialize the UpCloud server entity."""
super().__init__(coordinator)
self.uuid = uuid
@@ -257,7 +262,7 @@ class UpCloudServerEntity(CoordinatorEntity):
return self.uuid
@property
def name(self):
def name(self) -> str:
"""Return the name of the component."""
try:
return DEFAULT_COMPONENT_NAME.format(self._server.title)
@@ -265,12 +270,12 @@ class UpCloudServerEntity(CoordinatorEntity):
return DEFAULT_COMPONENT_NAME.format(self.uuid)
@property
def icon(self):
def icon(self) -> str:
"""Return the icon of this server."""
return "mdi:server" if self.is_on else "mdi:server-off"
@property
def state(self):
def state(self) -> str | None:
"""Return state of the server."""
try:
return STATE_MAP.get(self._server.state, self._server.state)
@@ -278,17 +283,17 @@ class UpCloudServerEntity(CoordinatorEntity):
return None
@property
def is_on(self):
def is_on(self) -> bool:
"""Return true if the server is on."""
return self.state == STATE_ON
@property
def device_class(self):
def device_class(self) -> str:
"""Return the class of this server."""
return DEFAULT_COMPONENT_DEVICE_CLASS
@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the UpCloud server."""
return {
x: getattr(self._server, x, None)