Skip to content

Commit

Permalink
Add hacs and log class mixins (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Aug 15, 2021
1 parent 0f57b00 commit 7139b46
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
18 changes: 6 additions & 12 deletions custom_components/hacs/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
RELEASE_LIMIT,
hacs_config_option_schema,
)
from custom_components.hacs.helpers.functions.logger import getLogger
from custom_components.hacs.share import get_hacs
from custom_components.hacs.mixin import HacsMixin

from .base import HacsBase

_LOGGER = getLogger()


class HacsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class HacsFlowHandler(HacsMixin, config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for HACS."""

VERSION = 1
Expand Down Expand Up @@ -84,7 +79,7 @@ async def _wait_for_activation(_=None):
},
)
except AIOGitHubAPIException as exception:
_LOGGER.error(exception)
self.hacs.log.error(exception)
return self.async_abort(reason="github")

return self.async_show_progress_done(next_step_id="device_done")
Expand Down Expand Up @@ -131,7 +126,7 @@ def async_get_options_flow(config_entry):
return HacsOptionsFlowHandler(config_entry)


class HacsOptionsFlowHandler(config_entries.OptionsFlow):
class HacsOptionsFlowHandler(HacsMixin, config_entries.OptionsFlow):
"""HACS config flow options handler."""

def __init__(self, config_entry):
Expand All @@ -144,17 +139,16 @@ async def async_step_init(self, _user_input=None):

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
hacs: HacsBase = get_hacs()
if user_input is not None:
limit = int(user_input.get(RELEASE_LIMIT, 5))
if limit <= 0 or limit > 100:
return self.async_abort(reason="release_limit_value")
return self.async_create_entry(title="", data=user_input)

if hacs.configuration is None:
if self.hacs.configuration is None:
return self.async_abort(reason="not_setup")

if hacs.configuration.config_type == "yaml":
if self.hacs.configuration.config_type == "yaml":
schema = {vol.Optional("not_in_use", default=""): str}
else:
schema = hacs_config_option_schema(self.config_entry.options)
Expand Down
23 changes: 23 additions & 0 deletions custom_components/hacs/mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Mixin classes."""
# pylint: disable=too-few-public-methods
from __future__ import annotations
from logging import Logger
from typing import TYPE_CHECKING

from .share import get_hacs
from .helpers.functions.logger import getLogger

if TYPE_CHECKING:
from .hacsbase.hacs import Hacs


class HacsMixin:
"""Mixin to provide 'self.hacs' to classes."""

hacs: Hacs = get_hacs()


class LogMixin:
"""Mixin to provide 'self.log' to classes."""

log: Logger = getLogger()
11 changes: 5 additions & 6 deletions custom_components/hacs/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from homeassistant.helpers.entity import Entity

from custom_components.hacs.const import DOMAIN, INTEGRATION_VERSION, NAME_SHORT
from custom_components.hacs.share import get_hacs
from custom_components.hacs.mixin import HacsMixin


async def async_setup_platform(
Expand All @@ -18,7 +18,7 @@ async def async_setup_entry(_hass, _config_entry, async_add_devices):
async_add_devices([HACSSensor()])


class HACSDevice(Entity):
class HACSDevice(HacsMixin, Entity):
"""HACS Device class."""

@property
Expand Down Expand Up @@ -60,16 +60,15 @@ def _update_and_write_state(self, *_):
@callback
def _update(self):
"""Update the sensor."""
hacs = get_hacs()
if hacs.status.background_task:
if self.hacs.status.background_task:
return

self.repositories = []

for repository in hacs.repositories:
for repository in self.hacs.repositories:
if (
repository.pending_upgrade
and repository.data.category in hacs.common.categories
and repository.data.category in self.hacs.common.categories
):
self.repositories.append(repository)
self._state = len(self.repositories)
Expand Down

0 comments on commit 7139b46

Please sign in to comment.