Skip to content

Commit

Permalink
vacuum: fallback to UTC when encountering unknown timezone response (r…
Browse files Browse the repository at this point in the history
…ytilahti#932)

* vacuum: fallback to UTC when encountering unknown timezone response

* add tests for different timezone responses
  • Loading branch information
rytilahti authored and xvlady committed May 9, 2021
1 parent e056f6f commit 39efe6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 59 deletions.
73 changes: 15 additions & 58 deletions miio/tests/test_vacuum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from unittest import TestCase
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -152,62 +153,18 @@ def test_zoned_clean(self):
)
assert self.status().state_code == self.device.STATE_ZONED_CLEAN

@pytest.mark.xfail
def test_manual_control(self):
self.fail()

@pytest.mark.skip("unknown handling")
def test_log_upload(self):
self.fail()

@pytest.mark.xfail
def test_consumable_status(self):
self.fail()

@pytest.mark.skip("consumable reset is not implemented")
def test_consumable_reset(self):
self.fail()

@pytest.mark.xfail
def test_map(self):
self.fail()

@pytest.mark.xfail
def test_clean_history(self):
self.fail()

@pytest.mark.xfail
def test_clean_details(self):
self.fail()

@pytest.mark.skip("hard to test")
def test_find(self):
self.fail()

@pytest.mark.xfail
def test_timer(self):
self.fail()

@pytest.mark.xfail
def test_dnd(self):
self.fail()

@pytest.mark.xfail
def test_fan_speed(self):
self.fail()

@pytest.mark.xfail
def test_sound_info(self):
self.fail()

@pytest.mark.xfail
def test_serial_number(self):
self.fail()

@pytest.mark.xfail
def test_timezone(self):
self.fail()

@pytest.mark.xfail
def test_raw_command(self):
self.fail()
with patch.object(
self.device,
"send",
return_value=[
{"olson": "Europe/Berlin", "posix": "CET-1CEST,M3.5.0,M10.5.0/3"}
],
):
assert self.device.timezone() == "Europe/Berlin"

with patch.object(self.device, "send", return_value=["Europe/Berlin"]):
assert self.device.timezone() == "Europe/Berlin"

with patch.object(self.device, "send", return_value=[0]):
assert self.device.timezone() == "UTC"
12 changes: 11 additions & 1 deletion miio/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,21 @@ def locale(self):
def timezone(self):
"""Get the timezone."""
res = self.send("get_timezone")[0]

def _fallback_timezone(data):
fallback = "UTC"
_LOGGER.error(
"Unsupported timezone format (%s), falling back to %s", data, fallback
)
return fallback

if isinstance(res, int):
return _fallback_timezone(res)
if isinstance(res, dict):
# Xiaowa E25 example
# {'olson': 'Europe/Berlin', 'posix': 'CET-1CEST,M3.5.0,M10.5.0/3'}
if "olson" not in res:
raise VacuumException("Unsupported timezone format: %s" % res)
return _fallback_timezone(res)

return res["olson"]

Expand Down

0 comments on commit 39efe6d

Please sign in to comment.