Attrs cleanups (#37849)

This commit is contained in:
Ville Skyttä 2020-07-14 20:30:30 +03:00 committed by GitHub
parent 7e280e2b27
commit ac0dbb17af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 141 additions and 148 deletions

View file

@ -77,10 +77,10 @@ def _verify_otp(secret: str, otp: str, count: int) -> bool:
class NotifySetting:
"""Store notify setting for one user."""
secret = attr.ib(type=str, factory=_generate_secret) # not persistent
counter = attr.ib(type=int, factory=_generate_random) # not persistent
notify_service = attr.ib(type=Optional[str], default=None)
target = attr.ib(type=Optional[str], default=None)
secret: str = attr.ib(factory=_generate_secret) # not persistent
counter: int = attr.ib(factory=_generate_random) # not persistent
notify_service: Optional[str] = attr.ib(default=None)
target: Optional[str] = attr.ib(default=None)
_UsersDict = Dict[str, NotifySetting]

View file

@ -20,39 +20,35 @@ TOKEN_TYPE_LONG_LIVED_ACCESS_TOKEN = "long_lived_access_token"
class Group:
"""A group."""
name = attr.ib(type=Optional[str])
policy = attr.ib(type=perm_mdl.PolicyType)
id = attr.ib(type=str, factory=lambda: uuid.uuid4().hex)
system_generated = attr.ib(type=bool, default=False)
name: Optional[str] = attr.ib()
policy: perm_mdl.PolicyType = attr.ib()
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
system_generated: bool = attr.ib(default=False)
@attr.s(slots=True)
class User:
"""A user."""
name = attr.ib(type=Optional[str])
perm_lookup = attr.ib(type=perm_mdl.PermissionLookup, eq=False, order=False)
id = attr.ib(type=str, factory=lambda: uuid.uuid4().hex)
is_owner = attr.ib(type=bool, default=False)
is_active = attr.ib(type=bool, default=False)
system_generated = attr.ib(type=bool, default=False)
name: Optional[str] = attr.ib()
perm_lookup: perm_mdl.PermissionLookup = attr.ib(eq=False, order=False)
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
is_owner: bool = attr.ib(default=False)
is_active: bool = attr.ib(default=False)
system_generated: bool = attr.ib(default=False)
groups = attr.ib(type=List[Group], factory=list, eq=False, order=False)
groups: List[Group] = attr.ib(factory=list, eq=False, order=False)
# List of credentials of a user.
credentials = attr.ib(type=List["Credentials"], factory=list, eq=False, order=False)
credentials: List["Credentials"] = attr.ib(factory=list, eq=False, order=False)
# Tokens associated with a user.
refresh_tokens = attr.ib(
type=Dict[str, "RefreshToken"], factory=dict, eq=False, order=False
refresh_tokens: Dict[str, "RefreshToken"] = attr.ib(
factory=dict, eq=False, order=False
)
_permissions = attr.ib(
type=Optional[perm_mdl.PolicyPermissions],
init=False,
eq=False,
order=False,
default=None,
_permissions: Optional[perm_mdl.PolicyPermissions] = attr.ib(
init=False, eq=False, order=False, default=None,
)
@property
@ -88,39 +84,38 @@ class User:
class RefreshToken:
"""RefreshToken for a user to grant new access tokens."""
user = attr.ib(type=User)
client_id = attr.ib(type=Optional[str])
access_token_expiration = attr.ib(type=timedelta)
client_name = attr.ib(type=Optional[str], default=None)
client_icon = attr.ib(type=Optional[str], default=None)
token_type = attr.ib(
type=str,
user: User = attr.ib()
client_id: Optional[str] = attr.ib()
access_token_expiration: timedelta = attr.ib()
client_name: Optional[str] = attr.ib(default=None)
client_icon: Optional[str] = attr.ib(default=None)
token_type: str = attr.ib(
default=TOKEN_TYPE_NORMAL,
validator=attr.validators.in_(
(TOKEN_TYPE_NORMAL, TOKEN_TYPE_SYSTEM, TOKEN_TYPE_LONG_LIVED_ACCESS_TOKEN)
),
)
id = attr.ib(type=str, factory=lambda: uuid.uuid4().hex)
created_at = attr.ib(type=datetime, factory=dt_util.utcnow)
token = attr.ib(type=str, factory=lambda: secrets.token_hex(64))
jwt_key = attr.ib(type=str, factory=lambda: secrets.token_hex(64))
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
created_at: datetime = attr.ib(factory=dt_util.utcnow)
token: str = attr.ib(factory=lambda: secrets.token_hex(64))
jwt_key: str = attr.ib(factory=lambda: secrets.token_hex(64))
last_used_at = attr.ib(type=Optional[datetime], default=None)
last_used_ip = attr.ib(type=Optional[str], default=None)
last_used_at: Optional[datetime] = attr.ib(default=None)
last_used_ip: Optional[str] = attr.ib(default=None)
@attr.s(slots=True)
class Credentials:
"""Credentials for a user on an auth provider."""
auth_provider_type = attr.ib(type=str)
auth_provider_id = attr.ib(type=Optional[str])
auth_provider_type: str = attr.ib()
auth_provider_id: Optional[str] = attr.ib()
# Allow the auth provider to store data to represent their auth.
data = attr.ib(type=dict)
data: dict = attr.ib()
id = attr.ib(type=str, factory=lambda: uuid.uuid4().hex)
is_new = attr.ib(type=bool, default=True)
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
is_new: bool = attr.ib(default=True)
class UserMeta(NamedTuple):

View file

@ -13,5 +13,5 @@ if TYPE_CHECKING:
class PermissionLookup:
"""Class to hold data for permission lookups."""
entity_registry = attr.ib(type="ent_reg.EntityRegistry")
device_registry = attr.ib(type="dev_reg.DeviceRegistry")
entity_registry: "ent_reg.EntityRegistry" = attr.ib()
device_registry: "dev_reg.DeviceRegistry" = attr.ib()

View file

@ -119,8 +119,8 @@ SCHEMA_WS_CAMERA_THUMBNAIL = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
class Image:
"""Represent an image."""
content_type = attr.ib(type=str)
content = attr.ib(type=bytes)
content_type: str = attr.ib()
content: bytes = attr.ib()
@bind_hass

View file

@ -14,14 +14,14 @@ class ChromecastInfo:
This also has the same attributes as the mDNS fields by zeroconf.
"""
services = attr.ib(type=Optional[set])
host = attr.ib(type=Optional[str], default=None)
port = attr.ib(type=Optional[int], default=0)
uuid = attr.ib(
type=Optional[str], converter=attr.converters.optional(str), default=None
services: Optional[set] = attr.ib()
host: Optional[str] = attr.ib(default=None)
port: Optional[int] = attr.ib(default=0)
uuid: Optional[str] = attr.ib(
converter=attr.converters.optional(str), default=None
) # always convert UUID to string if not None
model_name = attr.ib(type=str, default="")
friendly_name = attr.ib(type=Optional[str], default=None)
model_name: str = attr.ib(default="")
friendly_name: Optional[str] = attr.ib(default=None)
@property
def is_audio_group(self) -> bool:

View file

@ -35,9 +35,9 @@ class DeviceTrackerPlatform:
"setup_scanner",
)
name = attr.ib(type=str)
platform = attr.ib(type=ModuleType)
config = attr.ib(type=Dict)
name: str = attr.ib()
platform: ModuleType = attr.ib()
config: Dict = attr.ib()
@property
def type(self):

View file

@ -1,6 +1,6 @@
"""Runtime entry data for ESPHome stored in hass.data."""
import asyncio
from typing import Any, Callable, Dict, List, Optional, Set, Tuple
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple
from aioesphomeapi import (
COMPONENT_TYPE_TO_INFO,
@ -26,6 +26,9 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.storage import Store
from homeassistant.helpers.typing import HomeAssistantType
if TYPE_CHECKING:
from . import APIClient
DATA_KEY = "esphome"
# Mapping from ESPHome info type to HA platform
@ -46,26 +49,26 @@ INFO_TYPE_TO_PLATFORM = {
class RuntimeEntryData:
"""Store runtime data for esphome config entries."""
entry_id = attr.ib(type=str)
client = attr.ib(type="APIClient")
store = attr.ib(type=Store)
reconnect_task = attr.ib(type=Optional[asyncio.Task], default=None)
state = attr.ib(type=Dict[str, Dict[str, Any]], factory=dict)
info = attr.ib(type=Dict[str, Dict[str, Any]], factory=dict)
entry_id: str = attr.ib()
client: "APIClient" = attr.ib()
store: Store = attr.ib()
reconnect_task: Optional[asyncio.Task] = attr.ib(default=None)
state: Dict[str, Dict[str, Any]] = attr.ib(factory=dict)
info: Dict[str, Dict[str, Any]] = attr.ib(factory=dict)
# A second list of EntityInfo objects
# This is necessary for when an entity is being removed. HA requires
# some static info to be accessible during removal (unique_id, maybe others)
# If an entity can't find anything in the info array, it will look for info here.
old_info = attr.ib(type=Dict[str, Dict[str, Any]], factory=dict)
old_info: Dict[str, Dict[str, Any]] = attr.ib(factory=dict)
services = attr.ib(type=Dict[int, "UserService"], factory=dict)
available = attr.ib(type=bool, default=False)
device_info = attr.ib(type=DeviceInfo, default=None)
cleanup_callbacks = attr.ib(type=List[Callable[[], None]], factory=list)
disconnect_callbacks = attr.ib(type=List[Callable[[], None]], factory=list)
loaded_platforms = attr.ib(type=Set[str], factory=set)
platform_load_lock = attr.ib(type=asyncio.Lock, factory=asyncio.Lock)
services: Dict[int, "UserService"] = attr.ib(factory=dict)
available: bool = attr.ib(default=False)
device_info: Optional[DeviceInfo] = attr.ib(default=None)
cleanup_callbacks: List[Callable[[], None]] = attr.ib(factory=list)
disconnect_callbacks: List[Callable[[], None]] = attr.ib(factory=list)
loaded_platforms: Set[str] = attr.ib(factory=set)
platform_load_lock: asyncio.Lock = attr.ib(factory=asyncio.Lock)
@callback
def async_update_entity(

View file

@ -612,10 +612,10 @@ async def async_setup_entry(hass, entry):
class Subscription:
"""Class to hold data about an active subscription."""
topic = attr.ib(type=str)
callback = attr.ib(type=MessageCallbackType)
qos = attr.ib(type=int, default=0)
encoding = attr.ib(type=str, default="utf-8")
topic: str = attr.ib()
callback: MessageCallbackType = attr.ib()
qos: int = attr.ib(default=0)
encoding: str = attr.ib(default="utf-8")
class MQTT:

View file

@ -1,6 +1,6 @@
"""Provides device automations for MQTT."""
import logging
from typing import Callable, List
from typing import Callable, List, Optional
import attr
import voluptuous as vol
@ -75,10 +75,10 @@ DEVICE_TRIGGERS = "mqtt_device_triggers"
class TriggerInstance:
"""Attached trigger settings."""
action = attr.ib(type=AutomationActionType)
automation_info = attr.ib(type=dict)
trigger = attr.ib(type="Trigger")
remove = attr.ib(type=CALLBACK_TYPE, default=None)
action: AutomationActionType = attr.ib()
automation_info: dict = attr.ib()
trigger: "Trigger" = attr.ib()
remove: Optional[CALLBACK_TYPE] = attr.ib(default=None)
async def async_attach_trigger(self):
"""Attach MQTT trigger."""
@ -101,16 +101,16 @@ class TriggerInstance:
class Trigger:
"""Device trigger settings."""
device_id = attr.ib(type=str)
discovery_data = attr.ib(type=dict)
hass = attr.ib(type=HomeAssistantType)
payload = attr.ib(type=str)
qos = attr.ib(type=int)
remove_signal = attr.ib(type=Callable[[], None])
subtype = attr.ib(type=str)
topic = attr.ib(type=str)
type = attr.ib(type=str)
trigger_instances = attr.ib(type=[TriggerInstance], default=attr.Factory(list))
device_id: str = attr.ib()
discovery_data: dict = attr.ib()
hass: HomeAssistantType = attr.ib()
payload: str = attr.ib()
qos: int = attr.ib()
remove_signal: Callable[[], None] = attr.ib()
subtype: str = attr.ib()
topic: str = attr.ib()
type: str = attr.ib()
trigger_instances: List[TriggerInstance] = attr.ib(factory=list)
async def add_trigger(self, action, automation_info):
"""Add MQTT trigger."""

View file

@ -1,6 +1,6 @@
"""Modesl used by multiple MQTT modules."""
import datetime as dt
from typing import Callable, Union
from typing import Callable, Optional, Union
import attr
@ -11,12 +11,12 @@ PublishPayloadType = Union[str, bytes, int, float, None]
class Message:
"""MQTT Message."""
topic = attr.ib(type=str)
payload = attr.ib(type=PublishPayloadType)
qos = attr.ib(type=int)
retain = attr.ib(type=bool)
subscribed_topic = attr.ib(type=str, default=None)
timestamp = attr.ib(type=dt.datetime, default=None)
topic: str = attr.ib()
payload: PublishPayloadType = attr.ib()
qos: int = attr.ib()
retain: bool = attr.ib()
subscribed_topic: Optional[str] = attr.ib(default=None)
timestamp: Optional[dt.datetime] = attr.ib(default=None)
MessageCallbackType = Callable[[Message], None]

View file

@ -19,12 +19,12 @@ _LOGGER = logging.getLogger(__name__)
class EntitySubscription:
"""Class to hold data about an active entity topic subscription."""
hass = attr.ib(type=HomeAssistantType)
topic = attr.ib(type=str)
message_callback = attr.ib(type=MessageCallbackType)
unsubscribe_callback = attr.ib(type=Optional[Callable[[], None]])
qos = attr.ib(type=int, default=0)
encoding = attr.ib(type=str, default="utf-8")
hass: HomeAssistantType = attr.ib()
topic: str = attr.ib()
message_callback: MessageCallbackType = attr.ib()
unsubscribe_callback: Optional[Callable[[], None]] = attr.ib()
qos: int = attr.ib(default=0)
encoding: str = attr.ib(default="utf-8")
async def resubscribe_if_necessary(self, hass, other):
"""Re-subscribe to the new topic if necessary."""

View file

@ -21,19 +21,19 @@ PROVIDERS = Registry()
class StreamBuffer:
"""Represent a segment."""
segment = attr.ib(type=io.BytesIO)
segment: io.BytesIO = attr.ib()
output = attr.ib() # type=av.OutputContainer
vstream = attr.ib() # type=av.VideoStream
astream = attr.ib(default=None) # type=av.AudioStream
astream = attr.ib(default=None) # type=Optional[av.AudioStream]
@attr.s
class Segment:
"""Represent a segment."""
sequence = attr.ib(type=int)
segment = attr.ib(type=io.BytesIO)
duration = attr.ib(type=float)
sequence: int = attr.ib()
segment: io.BytesIO = attr.ib()
duration: float = attr.ib()
class StreamOutput:

View file

@ -4,7 +4,7 @@ from collections import OrderedDict
import datetime
import logging
import time
from typing import MutableMapping, cast
from typing import MutableMapping, Optional, cast
import attr
@ -28,9 +28,9 @@ TOMBSTONE_LIFETIME = datetime.timedelta(days=60).total_seconds()
class ZhaDeviceEntry:
"""Zha Device storage Entry."""
name = attr.ib(type=str, default=None)
ieee = attr.ib(type=str, default=None)
last_seen = attr.ib(type=float, default=None)
name: Optional[str] = attr.ib(default=None)
ieee: Optional[str] = attr.ib(default=None)
last_seen: Optional[float] = attr.ib(default=None)
class ZhaStorage:

View file

@ -1025,7 +1025,7 @@ class OptionsFlow(data_entry_flow.FlowHandler):
class SystemOptions:
"""Config entry system options."""
disable_new_entities = attr.ib(type=bool, default=False)
disable_new_entities: bool = attr.ib(default=False)
def update(self, *, disable_new_entities: bool) -> None:
"""Update properties."""

View file

@ -456,9 +456,9 @@ class HomeAssistant:
class Context:
"""The context that triggered something."""
user_id = attr.ib(type=str, default=None)
parent_id = attr.ib(type=Optional[str], default=None)
id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
user_id: str = attr.ib(default=None)
parent_id: Optional[str] = attr.ib(default=None)
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
def as_dict(self) -> dict:
"""Return a dictionary representation of the context."""

View file

@ -25,8 +25,8 @@ SAVE_DELAY = 10
class AreaEntry:
"""Area Registry Entry."""
name = attr.ib(type=str, default=None)
id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
name: Optional[str] = attr.ib(default=None)
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
class AreaRegistry:

View file

@ -40,7 +40,7 @@ class CheckConfigError(NamedTuple):
class HomeAssistantConfig(OrderedDict):
"""Configuration result with errors attribute."""
errors: List[CheckConfigError] = attr.ib(default=attr.Factory(list))
errors: List[CheckConfigError] = attr.ib(factory=list)
def add_error(
self,

View file

@ -57,13 +57,9 @@ class DeletedDeviceEntry:
class DeviceEntry:
"""Device Registry Entry."""
config_entries: Set[str] = attr.ib(converter=set, default=attr.Factory(set))
connections: Set[Tuple[str, str]] = attr.ib(
converter=set, default=attr.Factory(set)
)
identifiers: Set[Tuple[str, str]] = attr.ib(
converter=set, default=attr.Factory(set)
)
config_entries: Set[str] = attr.ib(converter=set, factory=set)
connections: Set[Tuple[str, str]] = attr.ib(converter=set, factory=set)
identifiers: Set[Tuple[str, str]] = attr.ib(converter=set, factory=set)
manufacturer: str = attr.ib(default=None)
model: str = attr.ib(default=None)
name: str = attr.ib(default=None)
@ -72,7 +68,7 @@ class DeviceEntry:
area_id: str = attr.ib(default=None)
name_by_user: str = attr.ib(default=None)
entry_type: str = attr.ib(default=None)
id: str = attr.ib(default=attr.Factory(lambda: uuid.uuid4().hex))
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
# This value is not stored, just used to keep track of events to fire.
is_new: bool = attr.ib(default=False)

View file

@ -78,15 +78,14 @@ ENTITY_DESCRIBING_ATTRIBUTES = {
class RegistryEntry:
"""Entity Registry Entry."""
entity_id = attr.ib(type=str)
unique_id = attr.ib(type=str)
platform = attr.ib(type=str)
name = attr.ib(type=str, default=None)
icon = attr.ib(type=str, default=None)
entity_id: str = attr.ib()
unique_id: str = attr.ib()
platform: str = attr.ib()
name: Optional[str] = attr.ib(default=None)
icon: Optional[str] = attr.ib(default=None)
device_id: Optional[str] = attr.ib(default=None)
config_entry_id: Optional[str] = attr.ib(default=None)
disabled_by = attr.ib(
type=Optional[str],
disabled_by: Optional[str] = attr.ib(
default=None,
validator=attr.validators.in_(
(
@ -105,7 +104,7 @@ class RegistryEntry:
# As set by integration
original_name: Optional[str] = attr.ib(default=None)
original_icon: Optional[str] = attr.ib(default=None)
domain = attr.ib(type=str, init=False, repr=False)
domain: str = attr.ib(init=False, repr=False)
@domain.default
def _domain_default(self) -> str:

View file

@ -400,7 +400,7 @@ track_time_interval = threaded_listener_factory(async_track_time_interval)
class SunListener:
"""Helper class to help listen to sun events."""
hass = attr.ib(type=HomeAssistant)
hass: HomeAssistant = attr.ib()
action: Callable[..., None] = attr.ib()
event: str = attr.ib()
offset: Optional[timedelta] = attr.ib()

View file

@ -167,8 +167,8 @@ COLORS = {
class XYPoint:
"""Represents a CIE 1931 XY coordinate pair."""
x = attr.ib(type=float) # pylint: disable=invalid-name
y = attr.ib(type=float) # pylint: disable=invalid-name
x: float = attr.ib() # pylint: disable=invalid-name
y: float = attr.ib() # pylint: disable=invalid-name
@attr.s()
@ -176,9 +176,9 @@ class GamutType:
"""Represents the Gamut of a light."""
# ColorGamut = gamut(xypoint(xR,yR),xypoint(xG,yG),xypoint(xB,yB))
red = attr.ib(type=XYPoint)
green = attr.ib(type=XYPoint)
blue = attr.ib(type=XYPoint)
red: XYPoint = attr.ib()
green: XYPoint = attr.ib()
blue: XYPoint = attr.ib()
def color_name_to_rgb(color_name: str) -> Tuple[int, int, int]:

View file

@ -11,9 +11,9 @@ import attr
class Error:
"""Error validating an integration."""
plugin = attr.ib(type=str)
error = attr.ib(type=str)
fixable = attr.ib(type=bool, default=False)
plugin: str = attr.ib()
error: str = attr.ib()
fixable: bool = attr.ib(default=False)
def __str__(self) -> str:
"""Represent error as string."""
@ -63,10 +63,10 @@ class Integration:
return integrations
path = attr.ib(type=pathlib.Path)
manifest = attr.ib(type=dict, default=None)
errors = attr.ib(type=List[Error], factory=list)
warnings = attr.ib(type=List[Error], factory=list)
path: pathlib.Path = attr.ib()
manifest: Optional[dict] = attr.ib(default=None)
errors: List[Error] = attr.ib(factory=list)
warnings: List[Error] = attr.ib(factory=list)
@property
def domain(self) -> str: