Skip to content

Commit

Permalink
Fix lastest version in updater for Supervisor enabled installs (#38773)
Browse files Browse the repository at this point in the history
* Fix lastest version in update for Supervisor enabled installs

* Fix updater tests
  • Loading branch information
frenck committed Aug 12, 2020
1 parent e60e82c commit c0730a5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 27 deletions.
22 changes: 11 additions & 11 deletions homeassistant/components/hassio/__init__.py
Expand Up @@ -44,6 +44,7 @@

DATA_INFO = "hassio_info"
DATA_HOST_INFO = "hassio_host_info"
DATA_CORE_INFO = "hassio_core_info"
HASSIO_UPDATE_INTERVAL = timedelta(minutes=55)

SERVICE_ADDON_START = "addon_start"
Expand Down Expand Up @@ -142,34 +143,32 @@ async def async_get_addon_info(hass: HomeAssistantType, addon_id: str) -> dict:

@callback
@bind_hass
def get_homeassistant_version(hass):
"""Return latest available Home Assistant version.
def get_info(hass):
"""Return generic information from Supervisor.
Async friendly.
"""
if DATA_INFO not in hass.data:
return None
return hass.data[DATA_INFO].get("homeassistant")
return hass.data.get(DATA_INFO)


@callback
@bind_hass
def get_info(hass):
"""Return generic information from Supervisor.
def get_host_info(hass):
"""Return generic host information.
Async friendly.
"""
return hass.data.get(DATA_INFO)
return hass.data.get(DATA_HOST_INFO)


@callback
@bind_hass
def get_host_info(hass):
"""Return generic host information.
def get_core_info(hass):
"""Return Home Assistant Core information from Supervisor.
Async friendly.
"""
return hass.data.get(DATA_HOST_INFO)
return hass.data.get(DATA_CORE_INFO)


@callback
Expand Down Expand Up @@ -301,6 +300,7 @@ async def update_info_data(now):
try:
hass.data[DATA_INFO] = await hassio.get_info()
hass.data[DATA_HOST_INFO] = await hassio.get_host_info()
hass.data[DATA_CORE_INFO] = await hassio.get_core_info()
except HassioAPIError as err:
_LOGGER.warning("Can't read last version: %s", err)

Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/hassio/handler.py
Expand Up @@ -82,6 +82,14 @@ def get_host_info(self):
"""
return self.send_command("/host/info", method="get")

@_api_data
def get_core_info(self):
"""Return data for Home Asssistant Core.
This method returns a coroutine.
"""
return self.send_command("/core/info", method="get")

@_api_data
def get_addon_info(self, addon):
"""Return data for a Add-on.
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/updater/__init__.py
Expand Up @@ -76,9 +76,10 @@ async def check_new_version() -> Updater:
if "dev" in current_version:
return Updater(False, "", "")

# Load data from supervisor on Hass.io
# Load data from Supervisor
if hass.components.hassio.is_hassio():
newest = hass.components.hassio.get_homeassistant_version()
core_info = hass.components.hassio.get_core_info()
newest = core_info["version_latest"]

# Validate version
update_available = False
Expand Down
24 changes: 24 additions & 0 deletions tests/components/hassio/test_handler.py
Expand Up @@ -92,6 +92,30 @@ async def test_api_host_info_error(hassio_handler, aioclient_mock):
assert aioclient_mock.call_count == 1


async def test_api_core_info(hassio_handler, aioclient_mock):
"""Test setup with API Home Assistant Core info."""
aioclient_mock.get(
"http://127.0.0.1/core/info",
json={"result": "ok", "data": {"version_latest": "1.0.0"}},
)

data = await hassio_handler.get_core_info()
assert aioclient_mock.call_count == 1
assert data["version_latest"] == "1.0.0"


async def test_api_core_info_error(hassio_handler, aioclient_mock):
"""Test setup with API Home Assistant Core info error."""
aioclient_mock.get(
"http://127.0.0.1/core/info", json={"result": "error", "message": None}
)

with pytest.raises(HassioAPIError):
await hassio_handler.get_core_info()

assert aioclient_mock.call_count == 1


async def test_api_homeassistant_stop(hassio_handler, aioclient_mock):
"""Test setup with API Home Assistant stop."""
aioclient_mock.post("http://127.0.0.1/homeassistant/stop", json={"result": "ok"})
Expand Down
20 changes: 12 additions & 8 deletions tests/components/hassio/test_init.py
Expand Up @@ -40,6 +40,10 @@ def mock_all(aioclient_mock):
},
},
)
aioclient_mock.get(
"http://127.0.0.1/core/info",
json={"result": "ok", "data": {"version_latest": "1.0.0"}},
)
aioclient_mock.get(
"http://127.0.0.1/ingress/panels", json={"result": "ok", "data": {"panels": {}}}
)
Expand All @@ -51,8 +55,8 @@ async def test_setup_api_ping(hass, aioclient_mock):
result = await async_setup_component(hass, "hassio", {})
assert result

assert aioclient_mock.call_count == 6
assert hass.components.hassio.get_homeassistant_version() == "0.110.0"
assert aioclient_mock.call_count == 7
assert hass.components.hassio.get_core_info()["version_latest"] == "1.0.0"
assert hass.components.hassio.is_hassio()


Expand Down Expand Up @@ -90,7 +94,7 @@ async def test_setup_api_push_api_data(hass, aioclient_mock):
)
assert result

assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 7
assert not aioclient_mock.mock_calls[1][2]["ssl"]
assert aioclient_mock.mock_calls[1][2]["port"] == 9999
assert aioclient_mock.mock_calls[1][2]["watchdog"]
Expand All @@ -106,7 +110,7 @@ async def test_setup_api_push_api_data_server_host(hass, aioclient_mock):
)
assert result

assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 7
assert not aioclient_mock.mock_calls[1][2]["ssl"]
assert aioclient_mock.mock_calls[1][2]["port"] == 9999
assert not aioclient_mock.mock_calls[1][2]["watchdog"]
Expand All @@ -118,7 +122,7 @@ async def test_setup_api_push_api_data_default(hass, aioclient_mock, hass_storag
result = await async_setup_component(hass, "hassio", {"http": {}, "hassio": {}})
assert result

assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 7
assert not aioclient_mock.mock_calls[1][2]["ssl"]
assert aioclient_mock.mock_calls[1][2]["port"] == 8123
refresh_token = aioclient_mock.mock_calls[1][2]["refresh_token"]
Expand Down Expand Up @@ -165,7 +169,7 @@ async def test_setup_api_existing_hassio_user(hass, aioclient_mock, hass_storage
result = await async_setup_component(hass, "hassio", {"http": {}, "hassio": {}})
assert result

assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 7
assert not aioclient_mock.mock_calls[1][2]["ssl"]
assert aioclient_mock.mock_calls[1][2]["port"] == 8123
assert aioclient_mock.mock_calls[1][2]["refresh_token"] == token.token
Expand All @@ -179,7 +183,7 @@ async def test_setup_core_push_timezone(hass, aioclient_mock):
result = await async_setup_component(hass, "hassio", {"hassio": {}})
assert result

assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 7
assert aioclient_mock.mock_calls[2][2]["timezone"] == "testzone"

await hass.config.async_update(time_zone="America/New_York")
Expand All @@ -195,7 +199,7 @@ async def test_setup_hassio_no_additional_data(hass, aioclient_mock):
result = await async_setup_component(hass, "hassio", {"hassio": {}})
assert result

assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 7
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"


Expand Down
7 changes: 1 addition & 6 deletions tests/components/updater/test_init.py
Expand Up @@ -154,12 +154,7 @@ async def test_new_version_shows_entity_after_hour_hassio(
"""Test if binary sensor gets updated if new version is available / Hass.io."""
mock_get_uuid.return_value = MOCK_HUUID
mock_component(hass, "hassio")
hass.data["hassio_info"] = {"hassos": None, "homeassistant": "999.0"}
hass.data["hassio_host"] = {
"supervisor": "222",
"chassis": "vm",
"operating_system": "HassOS 4.6",
}
hass.data["hassio_core_info"] = {"version_latest": "999.0"}

assert await async_setup_component(hass, updater.DOMAIN, {updater.DOMAIN: {}})

Expand Down

0 comments on commit c0730a5

Please sign in to comment.