Skip to content
New issue

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

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

Already on GitHub? # to your account

v1.0.2 #5

Merged
merged 5 commits into from
Feb 21, 2022
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.2] (2022-02-21)

If your integration lacks a title and icon, click "Configure" and save the settings. This should update the title and display correctly in the integrations page.

### Fixes
- Fixed [#1](https://github.com/DSorlov/swemail/issues/1)
- Fixed [#2](https://github.com/DSorlov/swemail/issues/2)
- Fixed [#3](https://github.com/DSorlov/swemail/issues/3), thanks to @ehn for pull

### Added
- Now checks the postcode for validity before submitting it to the configuration

## [1.0.1] (2022-02-18)

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![maintained](https://img.shields.io/maintenance/yes/2022.svg)
[![hacs_badge](https://img.shields.io/badge/hacs-default-green.svg)](https://github.com/custom-components/hacs)
[![ha_version](https://img.shields.io/badge/home%20assistant-2021.12%2B-green.svg)](https://www.home-assistant.io)
![version](https://img.shields.io/badge/version-1.0.1-green.svg)
![version](https://img.shields.io/badge/version-1.0.2-green.svg)
![stability-alpha](https://img.shields.io/badge/stability-stable-green.svg)
[![maintainer](https://img.shields.io/badge/maintainer-dsorlov-blue.svg)](https://github.com/DSorlov)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
58 changes: 45 additions & 13 deletions custom_components/swemail/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"""Config flow"""
import logging
import voluptuous as vol
from homeassistant import config_entries
from homeassistant import config_entries, exceptions
from homeassistant.core import callback
from .woker import HttpWorker

from .const import CONF_POSTALCODE, DOMAIN, CONF_PROVIDER_POSTNORD, CONF_PROVIDER_CITYMAIL
from .const import CONF_POSTALCODE, DOMAIN, CONF_PROVIDER_POSTNORD, CONF_PROVIDER_CITYMAIL, CONF_TITLE

_LOGGER = logging.getLogger(__name__)

class InvalidPostalCode(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""

class SweMailDeliveryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Component config flow."""
Expand All @@ -28,17 +35,38 @@ async def async_step_user(self, user_input=None):
if (user_input is None):
return self.async_show_form(step_id="user", data_schema=data_schema)
else:
postalcode = user_input[CONF_POSTALCODE]
unique_id = f"{DOMAIN} {postalcode}"
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=postalcode, data={CONF_POSTALCODE: postalcode,
CONF_PROVIDER_POSTNORD: user_input[CONF_PROVIDER_POSTNORD],
CONF_PROVIDER_CITYMAIL: user_input[CONF_PROVIDER_CITYMAIL]}
error_schema = vol.Schema(
{
vol.Required(CONF_POSTALCODE, default=user_input[CONF_POSTALCODE]): vol.All(vol.Coerce(int), vol.Range(min=10000, max=99999)),
vol.Optional(CONF_PROVIDER_POSTNORD, default=user_input[CONF_PROVIDER_POSTNORD]): bool,
vol.Optional(CONF_PROVIDER_CITYMAIL, default=user_input[CONF_PROVIDER_CITYMAIL]): bool
}
)

try:
postalcode = user_input[CONF_POSTALCODE]
postalCity = await HttpWorker().fetchPostalCity(postalcode)
except:
return self.async_show_form(step_id="user", data_schema=error_schema, errors={ "base": "invalid_postcode"})

try:
entryTitle = f'{postalCity} {postalcode}'
unique_id = f"{DOMAIN} {postalcode}"
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=entryTitle, data={CONF_POSTALCODE: postalcode,
CONF_PROVIDER_POSTNORD: user_input[CONF_PROVIDER_POSTNORD],
CONF_PROVIDER_CITYMAIL: user_input[CONF_PROVIDER_CITYMAIL]}
)
except:
return self.async_show_form(step_id="user", data_schema=error_schema, errors={ "base": "creation_error"})




@staticmethod
@callback
def async_get_options_flow(config_entry):
Expand All @@ -65,17 +93,21 @@ async def async_step_user(self, user_input=None):

data_schema = vol.Schema(
{
vol.Optional(CONF_PROVIDER_POSTNORD, default=self.config_entry.options.get(CONF_PROVIDER_POSTNORD)): bool,
vol.Optional(CONF_PROVIDER_CITYMAIL, default=self.config_entry.options.get(CONF_PROVIDER_CITYMAIL)): bool
vol.Optional(CONF_PROVIDER_POSTNORD, default=self.config_entry.data.get(CONF_PROVIDER_POSTNORD)): bool,
vol.Optional(CONF_PROVIDER_CITYMAIL, default=self.config_entry.data.get(CONF_PROVIDER_CITYMAIL)): bool
}
)

if (user_input is None):
return self.async_show_form(step_id="user", data_schema=data_schema)
else:
postalcode = self.config_entry.data[CONF_POSTALCODE]

postalCity = await HttpWorker().fetchPostalCity(postalcode)
entryTitle = f'{postalCity} {postalcode}'

return self.async_create_entry(
title=postalcode, data={CONF_POSTALCODE: postalcode,
title=entryTitle, data={CONF_POSTALCODE: postalcode,
CONF_PROVIDER_POSTNORD: user_input[CONF_PROVIDER_POSTNORD],
CONF_PROVIDER_CITYMAIL: user_input[CONF_PROVIDER_CITYMAIL]}
)
3 changes: 2 additions & 1 deletion custom_components/swemail/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Constants for the component."""

# Component domain, used to store component data in hass data.
__version__ = "1.0.1"
__version__ = "1.0.2"
VERSION = __version__
DOMAIN = "swemail"

Expand All @@ -12,6 +12,7 @@
DEVICE_VERSION = __version__

CONF_POSTALCODE = "postalcode"
CONF_TITLE = "title"

CONF_PROVIDER_POSTNORD = "postnord"
CONF_PROVIDER_CITYMAIL = "citymail"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/swemail/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"issue_tracker": "https://github.com/dsorlov/swemail/issues",
"codeowners": ["@dsorlov"],
"iot_class": "cloud_polling",
"version": "1.0.1",
"version": "1.0.2",
"quality_scale": "silver"
}
2 changes: 0 additions & 2 deletions custom_components/swemail/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ async def async_update(self):
attributes[provider+'_days_left'] = ''
attributes[provider+'_logo'] = f"https://logo.clearbit.com/{provider}.se"

_LOGGER.error(f"value = {self._value}")

if (self._value is None):
attributes['next_provider'] = ''
attributes['next_logo'] = ''
Expand Down
8 changes: 6 additions & 2 deletions custom_components/swemail/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"abort": {
"already_configured": "The entered postal code is already configured."
},
"error": {
"invalid_postcode": "The entered postcode is not valid",
"creation_error": "An error occured while creating configuration object"
},
"step": {
"user": {
"data": {
"postalcode": "Postal Code",
"postnord": "Enable Postnord check",
"cotymail": "Enable Citymail check"
"citymail": "Enable Citymail check"
},
"title": "Swedish Mail Delivery"
}
Expand All @@ -23,7 +27,7 @@
"user": {
"data": {
"postnord": "Enable Postnord check",
"cotymail": "Enable Citymail check"
"citymail": "Enable Citymail check"
},
"title": "Swedish Mail Delivery"
}
Expand Down
4 changes: 4 additions & 0 deletions custom_components/swemail/translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"abort": {
"already_configured": "Det angivna postnummret finns redan"
},
"error": {
"invalid_postcode": "Det angivna postnummret är ogilltigt",
"creation_error": "Ett internt fel uppstod vid skapandet av konfigurationsobjektet"
},
"step": {
"user": {
"data": {
Expand Down
18 changes: 17 additions & 1 deletion custom_components/swemail/woker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
from pydoc import resolve
import re
from datetime import datetime
import html
import aiohttp
import asyncio
import requests

from ..const import CONF_PROVIDER_CITYMAIL, CONF_PROVIDER_POSTNORD
Expand Down Expand Up @@ -60,6 +63,16 @@ def __init__(self):
def data(self):
return self._data

async def _fetch_data_async(self, url, datatype):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if (datatype=='json'):
result = await resp.json()
else:
result = await resp.text()

return result

def _fetch_data(self, url):
r = requests.get(url)
r.raise_for_status()
Expand Down Expand Up @@ -101,6 +114,10 @@ def _handle_cm_data(self, data,postalcode):
}


async def fetchPostalCity(self, postalcode):
data = await self._fetch_data_async(self._URL[CONF_PROVIDER_POSTNORD].format(postalcode),'json')
return data["city"].capitalize()

def fetch(self,postalcode,provider):
data = self._fetch_data(self._URL[provider].format(postalcode))

Expand All @@ -109,4 +126,3 @@ def fetch(self,postalcode,provider):

if (provider==CONF_PROVIDER_CITYMAIL):
self._handle_cm_data(data.text,postalcode)