Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from thebino/Bugfix/config_flow
Browse files Browse the repository at this point in the history
fix config flow
  • Loading branch information
thebino authored Apr 19, 2021
2 parents 3b7a4d0 + 9fb8fd8 commit c14518b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 deletions.
58 changes: 58 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.3.0
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
args:
- --safe
- --quiet
files: ^((homeassistant|script|tests)/.+)?[^/]+\.py$
- repo: https://github.com/codespell-project/codespell
rev: v1.16.0
hooks:
- id: codespell
args:
- --ignore-words-list=hass,alot,datas,dof,dur,farenheit,hist,iff,ines,ist,lightsensor,mut,nd,pres,referer,ser,serie,te,technik,ue,uint,visability,wan,wanna,withing
- --skip="./.*,*.csv,*.json"
- --quiet-level=2
exclude_types: [csv, json]
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.1
hooks:
- id: flake8
additional_dependencies:
- flake8-docstrings==1.5.0
- pydocstyle==5.0.2
files: ^(homeassistant|script|tests)/.+\.py$
- repo: https://github.com/PyCQA/bandit
rev: 1.6.2
hooks:
- id: bandit
args:
- --quiet
- --format=custom
- --configfile=tests/bandit.yaml
files: ^(homeassistant|script|tests)/.+\.py$
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-executables-have-shebangs
stages: [manual]
- id: check-json
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
hooks:
- id: mypy
args:
- --pretty
- --show-error-codes
- --show-error-context
8 changes: 4 additions & 4 deletions custom_components/rki_covid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def async_setup_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
) -> bool:
"""Set up component from a config entry."""
_LOGGER.debug("setup component from config entry.")
_LOGGER.debug(f"Setup item from config entry: {entry.data}.")
# Forward the setup to the sensor platform.
for component in PLATFORMS:
hass.async_create_task(
Expand All @@ -47,7 +47,7 @@ async def async_unload_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
):
"""Unload a config entry."""
_LOGGER.debug("init#async_unload_entry()")
_LOGGER.debug(f"Unload item from config entry: {entry.data}.")
unload_ok = all(
await asyncio.gather(
*[
Expand Down Expand Up @@ -106,7 +106,7 @@ async def async_get_districts():
name = "BL " + state.name
items[name] = DistrictData(
name,
None,
name,
None,
state.population,
state.cases,
Expand All @@ -124,7 +124,7 @@ async def async_get_districts():
# country
items["Deutschland"] = DistrictData(
"Deutschland",
None,
"Deutschland",
None,
parser.country.population,
parser.country.cases,
Expand Down
25 changes: 14 additions & 11 deletions custom_components/rki_covid/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import voluptuous as vol

from . import get_coordinator
from .const import DOMAIN
from .const import ATTR_COUNTY, DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand All @@ -22,37 +22,40 @@ class RKICovidNumbersConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

_options = None

async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None):
async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None): # type: ignore
"""Invoke when a user initiates a flow via the user interface."""
_LOGGER.info("config_flow#async_step_user()")
_LOGGER.debug(f"User triggered configuration flow via UI: {user_input}")
_LOGGER.debug(
f"User triggered configuration flow via UI. user_input: {user_input}"
)

parser = RkiCovidParser(async_get_clientsession(self.hass))

errors: Dict[str, str] = {}

if self._options is None:
# add default county as first item
self._options = {}

# add items from coordinator
coordinator = await get_coordinator(self.hass, parser)
for case in sorted(coordinator.data.values(), key=lambda case: case.county):
self._options[case.county] = case.county
for case in sorted(coordinator.data.values(), key=lambda case: case.name):
if case.county:
self._options[case.county] = case.county
else:
self._options[case.name] = case.name

if user_input is not None:
await self.async_set_unique_id(user_input["county"])
await self.async_set_unique_id(user_input[ATTR_COUNTY])
self._abort_if_unique_id_configured()

# User is done adding sensors, create the config entry.
_LOGGER.debug("create entry from Configuration UI")
_LOGGER.debug("Create entry from Configuration UI")
return self.async_create_entry(
title=self._options[user_input["county"]], data=user_input
title=self._options[user_input[ATTR_COUNTY]], data=user_input
)

# Show user input for adding sensors.
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required("county"): vol.In(self._options)}),
data_schema=vol.Schema({vol.Required(ATTR_COUNTY): vol.In(self._options)}),
errors=errors,
)
2 changes: 1 addition & 1 deletion custom_components/rki_covid/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DistrictData:

name: str
county: Optional[str]
state: Optional[str]
state: str
population: str
count: int
deaths: int
Expand Down
2 changes: 1 addition & 1 deletion custom_components/rki_covid/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"domain": "rki_covid",
"version": "1.5.0",
"version": "1.5.1",
"name": "Robert-Koch Institut COVID Numbers",
"documentation": "https://github.com/thebino/rki_covid/blob/master/README.md",
"issue_tracker": "https://github.com/thebino/rki_covid/issues",
Expand Down
6 changes: 5 additions & 1 deletion custom_components/rki_covid/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ async def async_setup_entry(
parser = RkiCovidParser(session)
coordinator = await get_coordinator(hass, parser)

district = config_entry.data[ATTR_COUNTY]
try:
district = config_entry.data[ATTR_COUNTY]
except KeyError:
# handle deprecated entries
district = config_entry.data["county"]
sensors = [
RKICovidNumbersSensor(coordinator, district, info_type) for info_type in SENSORS
]
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ combine_as_imports = true

[mypy]
python_version = 3.7
show_error_codes = true
ignore_errors = true
follow_imports = silent
ignore_missing_imports = true
Expand Down

0 comments on commit c14518b

Please sign in to comment.