31 Commits

Author SHA1 Message Date
Adrian Rumpold
8fb322283c Merge pull request #14 from AdrianoKF/fix/extra_state_attributes
fix: Replace deprecated device_state_attributes property
2022-04-06 11:38:20 +02:00
Adrian Rumpold
35ed25cf3f fix: Replace deprecated device_state_attributes property
See https://developers.home-assistant.io/docs/core/entity/?_highlight=extra_state_attributes#generic-properties for details
2022-04-06 11:31:46 +02:00
Adrian Rumpold
d20b48e6b1 Merge pull request #12 from AdrianoKF/fix-div-ids
Fix div IDs
2022-03-18 09:06:54 +01:00
Adrian Rumpold
d1438f9a0b chore: Bump versin number 2022-03-18 09:01:21 +01:00
Adrian Rumpold
c1b7b16251 fix: Update div IDs to mnatch new page structure 2022-03-18 09:00:15 +01:00
Adrian Rumpold
e6c2644ea5 chore: Dependency updates 2022-03-18 08:59:53 +01:00
Adrian Rumpold
fc5f6e7c93 Merge pull request #11 from AdrianoKF/10-negative-first-vaccination-doses
Fix for negative number of first vaccination doses
2021-11-11 19:56:00 +01:00
Adrian Rumpold
fa052cdfc9 chore: Bump version number for release 2021-11-11 19:51:26 +01:00
Adrian Rumpold
f6f799e85f fix(crawler): Fix invalid calculation of number of first vaccinations
Numbers for second shot are actually inclusive of booster
shots, so booster shots do not
have to be subtracted from the
total number of vaccinations.

Fixes #10.
2021-11-11 19:50:44 +01:00
Adrian Rumpold
a44308a4e1 Merge pull request #9 from AdrianoKF/8-new-vaccination-report-format
Support new vaccination report format
2021-10-09 14:02:52 +02:00
Adrian Rumpold
dd5bb2916b chore: Bump manifest version number 2021-10-09 14:00:12 +02:00
Adrian Rumpold
67bb1e49ef feat(parser): Support new vaccination report format
Closes #8
2021-10-09 13:59:09 +02:00
Adrian Rumpold
aaea39657e Merge pull request #7 from AdrianoKF/6-integer-incidence-parse-failure
Correctly handle incidence values without decimals
2021-09-20 07:54:05 +02:00
Adrian Rumpold
81c19b552d fix(parser): Correctly handle incidence values without decimals
Closes #6
2021-09-20 07:51:52 +02:00
Adrian Rumpold
0d609ade9a chore: Bump version number 2021-09-17 08:36:54 +02:00
Adrian Rumpold
2b453f4b5e Merge pull request #5 from AdrianoKF/4-infection-data-parsing-error
Update infection data parser for new web page layout
2021-09-17 08:34:55 +02:00
Adrian Rumpold
62904f4c09 fix(parser): Update infection data parser for new web page layout
Closes #4
2021-09-17 08:30:48 +02:00
Adrian Rumpold
35d5232d8e feat: Use syringe MDI icon for vaccination entity 2021-08-11 10:57:03 +02:00
Adrian Rumpold
1c3b0ae0b5 Merge pull request #3 from AdrianoKF/feature/vaccination-data
Crawling and parsing of vaccination data, see #2
2021-08-11 10:35:21 +02:00
Adrian Rumpold
d2e8f77725 docs: Update readme 2021-08-11 10:26:48 +02:00
Adrian Rumpold
92e99e03ef feat: Include ratio of at-least-once vaccinated persons 2021-08-11 10:26:18 +02:00
Adrian Rumpold
903a512f99 docs: Update readme 2021-08-11 10:21:34 +02:00
Adrian Rumpold
f385ee3a5a fix: Return vaccination percentage instead of ratio 2021-08-11 10:07:00 +02:00
Adrian Rumpold
573f91e2f9 chore: Bump manifest version 2021-08-11 09:47:20 +02:00
Adrian Rumpold
025a6475dd fix: Actually add vaccination entity to integratoin 2021-08-11 08:52:56 +02:00
Adrian Rumpold
216775e68f fix: Fix and disable some HACS validations 2021-08-11 08:39:56 +02:00
Adrian Rumpold
403efb937b feat(CI): Add HACS validation step to Github Actions 2021-08-11 08:32:51 +02:00
Adrian Rumpold
559a463140 feat: Add new entity for vaccination data to Home Asssistant integration 2021-08-11 08:28:24 +02:00
Adrian Rumpold
2f73be9010 chore: Refactor duplicated HTTP fetching code 2021-08-11 08:12:48 +02:00
Adrian Rumpold
b6184be32f fix: Enable commented out code 2021-08-10 20:11:23 +02:00
Adrian Rumpold
70fa0619d4 fix: Simply HTTP error handling 2021-08-10 20:11:14 +02:00
9 changed files with 730 additions and 529 deletions

View File

@@ -50,3 +50,13 @@ jobs:
- name: Test with pytest
run: |
poetry run pytest
validate:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- name: HACS validation
uses: "hacs/action@main"
with:
category: "integration"
ignore: brands wheels

View File

@@ -2,7 +2,8 @@
## Adding to your dashboard
You can add an overview of the current infection numbers to your dashboard using the [multiple-entity-row](https://github.com/benct/lovelace-multiple-entity-row) card:
You can add an overview of the current infection and vaccination numbers to your dashboard
using the [multiple-entity-row](https://github.com/benct/lovelace-multiple-entity-row) card:
```yaml
type: entities
@@ -24,4 +25,25 @@ entities:
secondary_info:
attribute: incidence
unit: cases/100k
- type: custom:multiple-entity-row
entity: sensor.covid_19_vaccinations_augsburg
entities:
- attribute: ratio_vaccinated_once
name: Once
format: precision1
unit: '%'
- attribute: ratio_vaccinated_full
name: Fully
format: precision1
unit: '%'
- attribute: ratio_vaccinated_total
name: Total
format: precision1
unit: '%'
show_state: false
icon: mdi:needle
name: COVID-19 Vaccinations
secondary_info:
attribute: date
format: date
```

View File

@@ -12,11 +12,11 @@ if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .crawler import CovidCrawler, IncidenceData
from .crawler import CovidCrawler
_LOGGER = logging.getLogger(__name__)
__version__ = "0.1.0"
__version__ = "1.3.0"
PLATFORMS = ["sensor"]
@@ -67,9 +67,12 @@ async def get_coordinator(hass: HomeAssistant):
if DOMAIN in hass.data:
return hass.data[DOMAIN]
async def async_get_data() -> IncidenceData:
async def async_get_data() -> dict:
crawler = CovidCrawler(hass)
return await crawler.crawl_incidence()
return {
"incidence": await crawler.crawl_incidence(),
"vaccination": await crawler.crawl_vaccination(),
}
hass.data[DOMAIN] = DataUpdateCoordinator(
hass,

View File

@@ -59,9 +59,12 @@ class VaccinationData:
total_vaccinations: int = 0
num_vaccinated_once: int = 0
num_vaccinated_full: int = 0
num_vaccinated_booster: int = 0
ratio_vaccinated_once: float = 0.0
ratio_vaccinated_full: float = 0.0
ratio_vaccinated_total: float = 0.0
ratio_vaccinated_booster: float = 0.0
class CovidCrawlerBase(ABC):
@@ -78,6 +81,22 @@ class CovidCrawler(CovidCrawlerBase):
def __init__(self, hass=None) -> None:
self.hass = hass
async def _fetch(self, url: str) -> str:
"""Fetch a URL, using either the current Home Assistant instance or requests"""
if self.hass:
from homeassistant.helpers import aiohttp_client
result = await aiohttp_client.async_get_clientsession(self.hass).get(url)
soup = BeautifulSoup(await result.text(), "html.parser")
else:
import requests
result = requests.get(url)
result.raise_for_status()
soup = BeautifulSoup(result.text, "html.parser")
return soup
async def crawl_incidence(self) -> IncidenceData:
"""
Fetch COVID-19 infection data from the target website.
@@ -88,24 +107,13 @@ class CovidCrawler(CovidCrawlerBase):
url = (
"https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/fallzahlen"
)
if self.hass:
from homeassistant.helpers import aiohttp_client
soup = await self._fetch(url)
result = await aiohttp_client.async_get_clientsession(self.hass).get(url)
soup = BeautifulSoup(await result.text(), "html.parser")
else:
import requests
result = requests.get(url)
if not result.ok:
result.raise_for_status()
soup = BeautifulSoup(result.text, "html.parser")
match = soup.find(class_="frame--type-textpic")
text = match.p.text
match = soup.find(id="c1075340")
text = match.text.strip()
_log.debug(f"Infection data text: {text}")
matches = re.search(r"(\d+,\d+)\sNeuinfektion", text)
matches = re.search(r"(\d+(,\d+)?)\sNeuinfektion", text)
if not matches:
raise ValueError(
f"Could not extract incidence from scraped web page, {text=}"
@@ -114,18 +122,15 @@ class CovidCrawler(CovidCrawlerBase):
incidence = parse_num(matches.group(1), t=float)
_log.debug(f"Parsed incidence: {incidence}")
text = match.h2.text
matches = re.search(r"\((\d+)\. (\w+).*\)", text)
match = soup.find(id="c1052517")
text = match.text.strip()
matches = re.search(r"Stand: (\d+)\. (\w+) (\d{4})", text)
if not matches:
raise ValueError(f"Could not extract date from scraped web page, {text=}")
date = parse_date(matches.group(1), matches.group(2))
date = parse_date(matches.group(1), matches.group(2), matches.group(3))
_log.debug(f"Parsed date: {date}")
match = match.find_next_sibling(class_="frame--type-textpic")
text = match.text
_log.debug(f"Infection counts text: {text}")
regexes = [
r"Insgesamt: (?P<total_cases>[0-9.]+)",
r"genesen: (?P<num_recovered>[0-9.]+)",
@@ -153,28 +158,18 @@ class CovidCrawler(CovidCrawlerBase):
async def crawl_vaccination(self) -> VaccinationData:
_log.info("Fetching COVID-19 vaccination data update")
url = "https://www.augsburg.de/umwelt-sozgcoiales/gesundheit/coronavirus/impfzentrum"
url = (
"https://www.augsburg.de/umwelt-soziales/gesundheit/coronavirus/impfzentrum"
)
soup = await self._fetch(url)
container_id = "c1088140"
if self.hass:
from homeassistant.helpers import aiohttp_client
result = await aiohttp_client.async_get_clientsession(self.hass).get(url)
soup = BeautifulSoup(await result.text(), "html.parser")
else:
import requests
result = requests.get(url)
if not result.ok:
result.raise_for_status()
soup = BeautifulSoup(result.text, "html.parser")
result = soup.find(id=container_id)
text = re.sub(r"\s+", " ", result.text)
regexes = [
r"(?P<total_vaccinations>\d+[.]\d+) Impfdosen",
r"Weitere (?P<num_vaccinated_once>\d+[.]\d+) Personen haben die Erstimpfung erhalten",
r"(?P<num_vaccinated_full>\d+[.]\d+) Personen sind bereits vollständig geimpft",
r"(?P<total_vaccinations>\d+([.]\d+)?) Personen in Augsburg",
r"(?P<num_vaccinated_full>\d+([.]\d+)?) Personen in Augsburg",
r"(?P<num_vaccinated_booster>\d+([.]\d+)?) Personen, also",
]
values = {}
for r in regexes:
@@ -192,15 +187,24 @@ class CovidCrawler(CovidCrawlerBase):
if not matches:
raise ValueError(f"Could not extract date from scraped web page, {text=}")
values["num_vaccinated_once"] = (
values["total_vaccinations"] - values["num_vaccinated_full"]
)
values["date"] = parse_date(**matches.groupdict()).strftime("%Y-%m-%d")
result = VaccinationData(**values)
# Total population in Augsburg as of 2020
# https://www.augsburg.de/fileadmin/user_upload/buergerservice_rathaus/rathaus/statisiken_und_geodaten/statistiken/Monitoring/Demografiemonitoring_der_Stadt_Augsburg_2021.pdf
population = 299021
# Total population in Augsburg as listed on the crawled page
population = 298014
result.ratio_vaccinated_full = result.num_vaccinated_full / population
result.ratio_vaccinated_once = result.num_vaccinated_once / population
result.ratio_vaccinated_full = result.num_vaccinated_full / population * 100
result.ratio_vaccinated_once = result.num_vaccinated_once / population * 100
result.ratio_vaccinated_total = (
result.ratio_vaccinated_once + result.ratio_vaccinated_full
)
result.ratio_vaccinated_booster = (
result.num_vaccinated_booster / population * 100
)
_log.debug(f"Result data: {result}")
return result

View File

@@ -3,8 +3,8 @@ from .crawler import CovidCrawler
async def main():
crawler = CovidCrawler()
# result = await crawler.crawl()
# print(result)
result = await crawler.crawl_incidence()
print(result)
result = await crawler.crawl_vaccination()
print(result)

View File

@@ -1,10 +1,15 @@
{
"domain": "covid19_augsburg",
"name": "COVID-19 Augsburg",
"version": "0.1.0",
"version": "1.3.0",
"config_flow": true,
"documentation": "https://github.com/AdrianoKF/home-assistant-covid19-augsburg",
"requirements": ["beautifulsoup4==4.8.2"],
"issue_tracker": "https://github.com/AdrianoKF/home-assistant-covid19-augsburg/issues",
"requirements": [
"beautifulsoup4==4.8.2"
],
"dependencies": [],
"codeowners": ["@AdrianoKF"]
"codeowners": [
"@AdrianoKF"
]
}

View File

@@ -1,3 +1,5 @@
from dataclasses import asdict
from homeassistant.helpers.entity import Entity
from . import get_coordinator
@@ -7,7 +9,12 @@ async def async_setup_entry(hass, _, async_add_entities):
"""Defer sensor setup to the shared sensor module."""
coordinator = await get_coordinator(hass)
async_add_entities([CoronaAugsburgSensor(coordinator)])
async_add_entities(
[
CoronaAugsburgSensor(coordinator),
CoronaAugsburgVaccinationSensor(coordinator),
]
)
class CoronaAugsburgSensor(Entity):
@@ -41,18 +48,59 @@ class CoronaAugsburgSensor(Entity):
@property
def state(self):
return self.coordinator.data.incidence
return self.coordinator.data["incidence"].incidence
@property
def device_state_attributes(self):
return {
"date": self.coordinator.data.date,
"incidence": self.coordinator.data.incidence,
"total_cases": self.coordinator.data.total_cases,
"num_dead": self.coordinator.data.num_dead,
"num_recovered": self.coordinator.data.num_recovered,
"num_infected": self.coordinator.data.num_infected,
}
data = self.coordinator.data["incidence"]
return asdict(data)
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.coordinator.async_add_listener(self.async_write_ha_state)
async def async_will_remove_from_hass(self):
"""When entity will be removed from hass."""
self.coordinator.async_remove_listener(self.async_write_ha_state)
class CoronaAugsburgVaccinationSensor(Entity):
"""Representation of vaccination data for the city of Augsburg"""
def __init__(self, coordinator):
"""Initialize sensor."""
self.coordinator = coordinator
self._name = "COVID-19 Vaccinations Augsburg"
self._state = None
@property
def available(self):
return self.coordinator.last_update_success and self.coordinator.data
@property
def name(self):
return self._name
@property
def unique_id(self):
return self._name
@property
def icon(self):
return "mdi:needle"
@property
def unit_of_measurement(self):
return ""
@property
def state(self):
return self.coordinator.data["vaccination"].total_vaccinations
@property
def device_state_attributes(self):
data = self.coordinator.data["vaccination"]
return asdict(data)
async def async_added_to_hass(self):
"""When entity is added to hass."""

1021
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "git add re"
version = "0.1.0"
name = "home_assistant_covid19_augsburg"
version = "1.3.0"
description = ""
authors = ["Adrian Rumpold <a.rumpold@gmail.com>"]
packages = [