Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beta: Update ruff and mypy #426

Merged
merged 4 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions custom_components/keymaster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,18 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
updated_config[CONF_NOTIFY_SCRIPT_NAME] = (
f"keymaster_{updated_config.get(CONF_LOCK_NAME)}_manual_notify"
)
elif isinstance(
updated_config.get(CONF_NOTIFY_SCRIPT_NAME), str
) and updated_config[CONF_NOTIFY_SCRIPT_NAME].startswith("script."):
updated_config[CONF_NOTIFY_SCRIPT_NAME] = updated_config[
CONF_NOTIFY_SCRIPT_NAME
].split(".", maxsplit=1)[1]
elif isinstance(updated_config.get(CONF_NOTIFY_SCRIPT_NAME), str) and updated_config[
CONF_NOTIFY_SCRIPT_NAME
].startswith("script."):
updated_config[CONF_NOTIFY_SCRIPT_NAME] = updated_config[CONF_NOTIFY_SCRIPT_NAME].split(
".", maxsplit=1
)[1]

if updated_config.get(CONF_DOOR_SENSOR_ENTITY_ID) == DEFAULT_DOOR_SENSOR:
updated_config[CONF_DOOR_SENSOR_ENTITY_ID] = None
if (
updated_config.get(CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID)
== DEFAULT_ALARM_LEVEL_SENSOR
):
if updated_config.get(CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID) == DEFAULT_ALARM_LEVEL_SENSOR:
updated_config[CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID] = None
if (
updated_config.get(CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID)
== DEFAULT_ALARM_TYPE_SENSOR
):
if updated_config.get(CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID) == DEFAULT_ALARM_TYPE_SENSOR:
updated_config[CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID] = None

if updated_config != config_entry.data:
Expand Down Expand Up @@ -153,9 +147,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
"Sunday",
]
):
dow_slots[i] = KeymasterCodeSlotDayOfWeek(
day_of_week_num=i, day_of_week_name=dow
)
dow_slots[i] = KeymasterCodeSlotDayOfWeek(day_of_week_num=i, day_of_week_name=dow)
code_slots[x] = KeymasterCodeSlot(number=x, accesslimit_day_of_week=dow_slots)

kmlock = KeymasterLock(
Expand Down Expand Up @@ -225,8 +217,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->

if coordinator.count_locks_not_pending_delete == 0:
_LOGGER.debug(
"[async_unload_entry] Possibly empty coordinator. "
"Will evaluate for removal at %s",
"[async_unload_entry] Possibly empty coordinator. Will evaluate for removal at %s",
datetime.now().astimezone() + timedelta(seconds=20),
)
async_call_later(
Expand Down Expand Up @@ -265,9 +256,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
data = config_entry.data.copy()

data[CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID] = data.pop(CONF_ALARM_LEVEL, None)
data[CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID] = data.pop(
CONF_ALARM_TYPE, None
)
data[CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID] = data.pop(CONF_ALARM_TYPE, None)
data[CONF_LOCK_ENTITY_ID] = data.pop(CONF_ENTITY_ID)
if CONF_HIDE_PINS not in data:
data[CONF_HIDE_PINS] = DEFAULT_HIDE_PINS
Expand Down
4 changes: 1 addition & 3 deletions custom_components/keymaster/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ async def async_setup_entry(
):
"""Create the keymaster Binary Sensors."""
coordinator: KeymasterCoordinator = hass.data[DOMAIN][COORDINATOR]
kmlock = await coordinator.get_lock_by_config_entry_id(
config_entry.entry_id
)
kmlock = await coordinator.get_lock_by_config_entry_id(config_entry.entry_id)
entities: list = []
if async_using_zwave_js(hass=hass, kmlock=kmlock):
entities.append(
Expand Down
7 changes: 3 additions & 4 deletions custom_components/keymaster/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ async def async_setup_entry(


@dataclass(frozen=True, kw_only=True)
class KeymasterButtonEntityDescription(
KeymasterEntityDescription, ButtonEntityDescription
):
class KeymasterButtonEntityDescription(KeymasterEntityDescription, ButtonEntityDescription):
"""Entity Description for Keymaster Buttons."""


Expand All @@ -89,7 +87,8 @@ def _handle_coordinator_update(self) -> None:
return

if (
".code_slots" in self._property and isinstance(self._kmlock.code_slots, MutableMapping)
".code_slots" in self._property
and isinstance(self._kmlock.code_slots, MutableMapping)
and self._code_slot not in self._kmlock.code_slots
):
self._attr_available = False
Expand Down
52 changes: 22 additions & 30 deletions custom_components/keymaster/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.binary_sensor import DOMAIN as BINARY_DOMAIN
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult, OptionsFlow
from homeassistant.core import HomeAssistant, callback
from homeassistant.util import slugify

Expand Down Expand Up @@ -45,11 +44,9 @@
_LOGGER: logging.Logger = logging.getLogger(__name__)


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

domain: str = DOMAIN

VERSION: int = 3
DEFAULTS: MutableMapping[str, Any] = {
CONF_SLOTS: DEFAULT_CODE_SLOTS,
Expand Down Expand Up @@ -85,16 +82,16 @@ async def async_step_user(
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
config_entry: ConfigEntry,
) -> KeymasterOptionsFlow:
"""Get the options flow for this handler."""
return KeymasterOptionsFlow(config_entry)


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

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

Expand Down Expand Up @@ -210,15 +207,11 @@ def _get_default(key: str, fallback_default: Any = None) -> Any:
script_default: str | None = _get_default(CONF_NOTIFY_SCRIPT_NAME)
if isinstance(script_default, str) and not script_default.startswith("script."):
script_default = f"script.{script_default}"
_LOGGER.debug(
"[get_schema] script_default: %s (%s)", script_default, type(script_default)
)
_LOGGER.debug("[get_schema] script_default: %s (%s)", script_default, type(script_default))
schema = vol.Schema(
{
vol.Required(CONF_LOCK_NAME, default=_get_default(CONF_LOCK_NAME)): str,
vol.Required(
CONF_LOCK_ENTITY_ID, default=_get_default(CONF_LOCK_ENTITY_ID)
): vol.In(
vol.Required(CONF_LOCK_ENTITY_ID, default=_get_default(CONF_LOCK_ENTITY_ID)): vol.In(
_get_entities(
hass=hass,
domain=LOCK_DOMAIN,
Expand All @@ -227,15 +220,15 @@ def _get_default(key: str, fallback_default: Any = None) -> Any:
),
)
),
vol.Optional(
CONF_PARENT, default=_get_default(CONF_PARENT, "(none)")
): vol.In(_available_parent_locks(hass, entry_id)),
vol.Required(
CONF_SLOTS, default=_get_default(CONF_SLOTS, DEFAULT_CODE_SLOTS)
): vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Required(
CONF_START, default=_get_default(CONF_START, DEFAULT_START)
): vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Optional(CONF_PARENT, default=_get_default(CONF_PARENT, "(none)")): vol.In(
_available_parent_locks(hass, entry_id)
),
vol.Required(CONF_SLOTS, default=_get_default(CONF_SLOTS, DEFAULT_CODE_SLOTS)): vol.All(
vol.Coerce(int), vol.Range(min=1)
),
vol.Required(CONF_START, default=_get_default(CONF_START, DEFAULT_START)): vol.All(
vol.Coerce(int), vol.Range(min=1)
),
vol.Optional(
CONF_DOOR_SENSOR_ENTITY_ID,
default=_get_default(CONF_DOOR_SENSOR_ENTITY_ID, DEFAULT_DOOR_SENSOR),
Expand Down Expand Up @@ -310,7 +303,7 @@ def _get_default(key: str, fallback_default: Any = None) -> Any:


async def _start_config_flow(
cls: KeymasterFlowHandler | KeymasterOptionsFlow,
cls: KeymasterConfigFlow | KeymasterOptionsFlow,
step_id: str,
title: str,
user_input: MutableMapping[str, Any] | None,
Expand Down Expand Up @@ -345,18 +338,17 @@ async def _start_config_flow(
step_id,
user_input,
)
if step_id == "user" or not entry_id:
if isinstance(cls, KeymasterConfigFlow) or step_id == "user" or not entry_id:
return cls.async_create_entry(title=title, data=user_input)
assert isinstance(cls, KeymasterOptionsFlow)
cls.hass.config_entries.async_update_entry(
cls.config_entry, data=user_input
)
cls.hass.config_entries.async_update_entry(cls.config_entry, data=user_input)
await cls.hass.config_entries.async_reload(entry_id)
return cls.async_create_entry(title="", data={})

return cls.async_show_form(
step_id=step_id,
data_schema=_get_schema(hass=cls.hass, user_input=user_input, default_dict=defaults, entry_id=entry_id),
data_schema=_get_schema(
hass=cls.hass, user_input=user_input, default_dict=defaults, entry_id=entry_id
),
errors=errors,
description_placeholders=description_placeholders,
)
Loading
Loading