Skip to content

Commit

Permalink
Linting Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Dec 8, 2024
1 parent 8e66d4f commit 846bb08
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 162 deletions.
11 changes: 6 additions & 5 deletions custom_components/keymaster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""keymaster Integration"""
"""keymaster Integration."""

from __future__ import annotations

Expand Down Expand Up @@ -49,15 +49,15 @@


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set up is called when Home Assistant is loading our component"""
"""Set up is called when Home Assistant is loading our component."""
hass.data.setdefault(DOMAIN, {})

updated_config = config_entry.data.copy()

updated_config[CONF_SLOTS] = int(updated_config.get(CONF_SLOTS))
updated_config[CONF_START] = int(updated_config.get(CONF_START))

if config_entry.data.get(CONF_PARENT) in (None, "(none)"):
if config_entry.data.get(CONF_PARENT) in {None, "(none)"}:
updated_config[CONF_PARENT] = None

if config_entry.data.get(CONF_PARENT_ENTRY_ID) == config_entry.entry_id:
Expand Down Expand Up @@ -206,7 +206,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Handle removal of an entry"""
"""Handle removal of an entry."""
lockname: str = config_entry.data.get(CONF_LOCK_NAME)
_LOGGER.info("Unloading %s", lockname)
unload_ok: bool = all(
Expand Down Expand Up @@ -237,6 +237,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->


async def delete_coordinator(hass: HomeAssistant, _: datetime):
"""Delete the coordinator if no more kmlock entities exist."""
_LOGGER.debug("[delete_coordinator] Triggered")
coordinator: KeymasterCoordinator = hass.data[DOMAIN][COORDINATOR]
if len(coordinator.data) == 0:
Expand All @@ -247,7 +248,7 @@ async def delete_coordinator(hass: HomeAssistant, _: datetime):


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate an old config entry"""
"""Migrate an old config entry."""
version = config_entry.version

# 2 -> 3: Migrate to integrated functions
Expand Down
9 changes: 5 additions & 4 deletions custom_components/keymaster/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Sensor for keymaster"""
"""Sensor for keymaster."""

from dataclasses import dataclass
import logging
Expand Down Expand Up @@ -27,7 +27,7 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
):
"""Setup config entry"""
"""Create the keymaster Binary Sensors."""
coordinator: KeymasterCoordinator = hass.data[DOMAIN][COORDINATOR]
kmlock: KeymasterLock = await coordinator.get_lock_by_config_entry_id(
config_entry.entry_id
Expand Down Expand Up @@ -76,16 +76,17 @@ async def async_setup_entry(
class KeymasterBinarySensorEntityDescription(
KeymasterEntityDescription, BinarySensorEntityDescription
):
pass
"""Entity Description for keymaster Binary Sensors."""


class KeymasterBinarySensor(KeymasterEntity, BinarySensorEntity):
"""Keymaster Binary Sensor Class."""

def __init__(
self,
entity_description: KeymasterBinarySensorEntityDescription,
) -> None:
"""Initialize binary sensor"""
"""Initialize binary sensor."""
super().__init__(
entity_description=entity_description,
)
Expand Down
5 changes: 3 additions & 2 deletions custom_components/keymaster/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def async_setup_entry(
class KeymasterButtonEntityDescription(
KeymasterEntityDescription, ButtonEntityDescription
):
pass
"""Entity Description for Keymaster Buttons."""


class KeymasterButton(KeymasterEntity, ButtonEntity):
Expand All @@ -71,7 +71,7 @@ def __init__(
self,
entity_description: KeymasterButtonEntityDescription,
) -> None:
"""Initialize button"""
"""Initialize button."""
super().__init__(
entity_description=entity_description,
)
Expand All @@ -96,6 +96,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()

async def async_press(self) -> None:
"""Take action when button is pressed."""
if self._property.endswith(".reset_lock"):
await self.coordinator.reset_lock(
config_entry_id=self._config_entry.entry_id,
Expand Down
46 changes: 22 additions & 24 deletions custom_components/keymaster/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Config flow for keymaster"""
"""Config flow for keymaster."""

from __future__ import annotations

Expand Down Expand Up @@ -44,7 +44,7 @@


class KeymasterFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for keymaster"""
"""Config flow for keymaster."""

VERSION = 3
DEFAULTS: Mapping[str, Any] = {
Expand All @@ -57,7 +57,7 @@ class KeymasterFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
}

async def get_unique_name_error(self, user_input) -> Mapping[str, str]:
"""Check if name is unique, returning dictionary error if so"""
"""Check if name is unique, returning dictionary error if so."""
# Validate that lock name is unique
existing_entry = await self.async_set_unique_id(
slugify(user_input[CONF_LOCK_NAME]).lower(), raise_on_progress=True
Expand All @@ -67,9 +67,9 @@ async def get_unique_name_error(self, user_input) -> Mapping[str, str]:
return {}

async def async_step_user(
self, user_input: Mapping[str, Any] = None
self, user_input: Mapping[str, Any] | None = None
) -> Mapping[str, Any]:
"""Handle a flow initialized by the user"""
"""Handle a flow initialized by the user."""
return await _start_config_flow(
cls=self,
step_id="user",
Expand All @@ -80,19 +80,20 @@ async def async_step_user(

@staticmethod
@callback
def async_get_options_flow(config_entry: config_entries.ConfigEntry):
def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> KeymasterOptionsFlow:
"""Get the options flow for this handler."""
return KeymasterOptionsFlow(config_entry)


class KeymasterOptionsFlow(config_entries.OptionsFlow):
"""Options flow for keymaster"""
"""Options flow for keymaster."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize"""
"""Initialize."""
self.config_entry = config_entry

async def get_unique_name_error(self, user_input) -> Mapping[str, str]:
"""Check if name is unique, returning dictionary error if so"""
"""Check if name is unique, returning dictionary error if so."""
# If lock name has changed, make sure new name isn't already being used
# otherwise show an error
if self.config_entry.unique_id != slugify(user_input[CONF_LOCK_NAME]).lower():
Expand All @@ -102,9 +103,9 @@ async def get_unique_name_error(self, user_input) -> Mapping[str, str]:
return {}

async def async_step_init(
self, user_input: Mapping[str, Any] = None
self, user_input: Mapping[str, Any] | None = None
) -> Mapping[str, Any]:
"""Handle a flow initialized by the user"""
"""Handle a flow initialized by the user."""
return await _start_config_flow(
cls=self,
step_id="init",
Expand All @@ -115,18 +116,15 @@ async def async_step_init(
)


def _available_parent_locks(hass: HomeAssistant, entry_id: str = None) -> list:
"""Find other keymaster configurations and list them as posible
parent locks if they are not a child lock already"""
def _available_parent_locks(hass: HomeAssistant, entry_id: str | None = None) -> list:
"""Return other keymaster locks if they are not already a child lock."""

data: list[str] = ["(none)"]
if DOMAIN not in hass.data:
return data

for entry in hass.config_entries.async_entries(DOMAIN):
if CONF_PARENT not in entry.data and entry.entry_id != entry_id:
data.append(entry.title)
elif entry.data[CONF_PARENT] is None and entry.entry_id != entry_id:
if CONF_PARENT not in entry.data and entry.entry_id != entry_id or entry.data[CONF_PARENT] is None and entry.entry_id != entry_id:
data.append(entry.title)

return data
Expand Down Expand Up @@ -184,19 +182,19 @@ def _get_schema(
hass: HomeAssistant,
user_input: Mapping[str, Any] | None,
default_dict: Mapping[str, Any],
entry_id: str = None,
entry_id: str | None = None,
) -> vol.Schema:
"""Gets a schema using the default_dict as a backup"""
"""Get a schema using the default_dict as a backup."""
if user_input is None:
user_input = {}

if CONF_PARENT in default_dict.keys() and default_dict[CONF_PARENT] is None:
if CONF_PARENT in default_dict and default_dict[CONF_PARENT] is None:
check_dict: Mapping[str, Any] = default_dict.copy()
check_dict.pop(CONF_PARENT, None)
default_dict = check_dict

def _get_default(key: str, fallback_default: Any = None) -> Any:
"""Gets default value for key"""
"""Get default value for key."""
default = user_input.get(key)
if default is None:
default = default_dict.get(key, fallback_default)
Expand Down Expand Up @@ -288,10 +286,10 @@ async def _start_config_flow(
step_id: str,
title: str,
user_input: Mapping[str, Any],
defaults: Mapping[str, Any] = None,
entry_id: str = None,
defaults: Mapping[str, Any] | None = None,
entry_id: str | None = None,
):
"""Start a config flow"""
"""Start a config flow."""
errors = {}
description_placeholders = {}

Expand Down
2 changes: 1 addition & 1 deletion custom_components/keymaster/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Constants for keymaster"""
"""Constants for keymaster."""

from enum import StrEnum

Expand Down
Loading

0 comments on commit 846bb08

Please sign in to comment.