From 149a3165e6bd787d8dbf068c91890b3b7524b94c Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Tue, 23 Jun 2020 06:22:52 +0200 Subject: [PATCH] Fix coronavirus worldwide sum (#36737) Co-authored-by: Franck Nijhof Co-authored-by: Paulus Schoutsen --- homeassistant/components/coronavirus/sensor.py | 11 ++++++++--- tests/components/coronavirus/conftest.py | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/coronavirus/sensor.py b/homeassistant/components/coronavirus/sensor.py index 2887427ec6b..d24c33a7752 100644 --- a/homeassistant/components/coronavirus/sensor.py +++ b/homeassistant/components/coronavirus/sensor.py @@ -51,9 +51,14 @@ class CoronavirusSensor(Entity): def state(self): """State of the sensor.""" if self.country == OPTION_WORLDWIDE: - return sum( - getattr(case, self.info_type) for case in self.coordinator.data.values() - ) + sum_cases = 0 + for case in self.coordinator.data.values(): + value = getattr(case, self.info_type) + if value is None: + continue + sum_cases += value + + return sum_cases return getattr(self.coordinator.data[self.country], self.info_type) diff --git a/tests/components/coronavirus/conftest.py b/tests/components/coronavirus/conftest.py index 6e49d2aa164..bbe5a463802 100644 --- a/tests/components/coronavirus/conftest.py +++ b/tests/components/coronavirus/conftest.py @@ -13,6 +13,13 @@ def mock_cases(): return_value=[ Mock(country="Netherlands", confirmed=10, recovered=8, deaths=1, current=1), Mock(country="Germany", confirmed=1, recovered=0, deaths=0, current=0), + Mock( + country="Sweden", + confirmed=None, + recovered=None, + deaths=None, + current=None, + ), ], ) as mock_get_cases: yield mock_get_cases