forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new board type (home-assistant#99334)
- Loading branch information
1 parent
63c538b
commit f9b2e10
Showing
15 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""The Home Assistant Green integration.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.components.hassio import get_os_info | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryNotReady | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up a Home Assistant Green config entry.""" | ||
if (os_info := get_os_info(hass)) is None: | ||
# The hassio integration has not yet fetched data from the supervisor | ||
raise ConfigEntryNotReady | ||
|
||
board: str | None | ||
if (board := os_info.get("board")) is None or board != "green": | ||
# Not running on a Home Assistant Green, Home Assistant may have been migrated | ||
hass.async_create_task(hass.config_entries.async_remove(entry.entry_id)) | ||
return False | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload a config entry.""" | ||
return True |
22 changes: 22 additions & 0 deletions
22
homeassistant/components/homeassistant_green/config_flow.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Config flow for the Home Assistant Green integration.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from homeassistant.config_entries import ConfigFlow | ||
from homeassistant.data_entry_flow import FlowResult | ||
|
||
from .const import DOMAIN | ||
|
||
|
||
class HomeAssistantGreenConfigFlow(ConfigFlow, domain=DOMAIN): | ||
"""Handle a config flow for Home Assistant Green.""" | ||
|
||
VERSION = 1 | ||
|
||
async def async_step_system(self, data: dict[str, Any] | None = None) -> FlowResult: | ||
"""Handle the initial step.""" | ||
if self._async_current_entries(): | ||
return self.async_abort(reason="single_instance_allowed") | ||
|
||
return self.async_create_entry(title="Home Assistant Green", data={}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""Constants for the Home Assistant Green integration.""" | ||
|
||
DOMAIN = "homeassistant_green" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""The Home Assistant Green hardware platform.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.components.hardware.models import BoardInfo, HardwareInfo | ||
from homeassistant.components.hassio import get_os_info | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.exceptions import HomeAssistantError | ||
|
||
from .const import DOMAIN | ||
|
||
BOARD_NAME = "Home Assistant Green" | ||
MANUFACTURER = "homeassistant" | ||
MODEL = "green" | ||
|
||
|
||
@callback | ||
def async_info(hass: HomeAssistant) -> list[HardwareInfo]: | ||
"""Return board info.""" | ||
if (os_info := get_os_info(hass)) is None: | ||
raise HomeAssistantError | ||
board: str | None | ||
if (board := os_info.get("board")) is None: | ||
raise HomeAssistantError | ||
if not board == "green": | ||
raise HomeAssistantError | ||
|
||
config_entries = [ | ||
entry.entry_id for entry in hass.config_entries.async_entries(DOMAIN) | ||
] | ||
|
||
return [ | ||
HardwareInfo( | ||
board=BoardInfo( | ||
hassio_board_id=board, | ||
manufacturer=MANUFACTURER, | ||
model=MODEL, | ||
revision=None, | ||
), | ||
config_entries=config_entries, | ||
dongle=None, | ||
name=BOARD_NAME, | ||
url=None, | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"domain": "homeassistant_green", | ||
"name": "Home Assistant Green", | ||
"codeowners": ["@home-assistant/core"], | ||
"config_flow": false, | ||
"dependencies": ["hardware", "hassio", "homeassistant_hardware"], | ||
"documentation": "https://www.home-assistant.io/integrations/homeassistant_green", | ||
"integration_type": "hardware" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Tests for the Home Assistant Green integration.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Test the Home Assistant Green config flow.""" | ||
from unittest.mock import patch | ||
|
||
from homeassistant.components.homeassistant_green.const import DOMAIN | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.data_entry_flow import FlowResultType | ||
|
||
from tests.common import MockConfigEntry, MockModule, mock_integration | ||
|
||
|
||
async def test_config_flow(hass: HomeAssistant) -> None: | ||
"""Test the config flow.""" | ||
mock_integration(hass, MockModule("hassio")) | ||
|
||
with patch( | ||
"homeassistant.components.homeassistant_green.async_setup_entry", | ||
return_value=True, | ||
) as mock_setup_entry: | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": "system"} | ||
) | ||
|
||
assert result["type"] == FlowResultType.CREATE_ENTRY | ||
assert result["title"] == "Home Assistant Green" | ||
assert result["data"] == {} | ||
assert result["options"] == {} | ||
assert len(mock_setup_entry.mock_calls) == 1 | ||
|
||
config_entry = hass.config_entries.async_entries(DOMAIN)[0] | ||
assert config_entry.data == {} | ||
assert config_entry.options == {} | ||
assert config_entry.title == "Home Assistant Green" | ||
|
||
|
||
async def test_config_flow_single_entry(hass: HomeAssistant) -> None: | ||
"""Test only a single entry is allowed.""" | ||
mock_integration(hass, MockModule("hassio")) | ||
|
||
# Setup the config entry | ||
config_entry = MockConfigEntry( | ||
data={}, | ||
domain=DOMAIN, | ||
options={}, | ||
title="Home Assistant Green", | ||
) | ||
config_entry.add_to_hass(hass) | ||
|
||
with patch( | ||
"homeassistant.components.homeassistant_green.async_setup_entry", | ||
return_value=True, | ||
) as mock_setup_entry: | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": "system"} | ||
) | ||
|
||
assert result["type"] == FlowResultType.ABORT | ||
assert result["reason"] == "single_instance_allowed" | ||
mock_setup_entry.assert_not_called() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Test the Home Assistant Green hardware platform.""" | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.homeassistant_green.const import DOMAIN | ||
from homeassistant.core import HomeAssistant | ||
|
||
from tests.common import MockConfigEntry, MockModule, mock_integration | ||
from tests.typing import WebSocketGenerator | ||
|
||
|
||
async def test_hardware_info( | ||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator | ||
) -> None: | ||
"""Test we can get the board info.""" | ||
mock_integration(hass, MockModule("hassio")) | ||
|
||
# Setup the config entry | ||
config_entry = MockConfigEntry( | ||
data={}, | ||
domain=DOMAIN, | ||
options={}, | ||
title="Home Assistant Green", | ||
) | ||
config_entry.add_to_hass(hass) | ||
with patch( | ||
"homeassistant.components.homeassistant_green.get_os_info", | ||
return_value={"board": "green"}, | ||
): | ||
assert await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
client = await hass_ws_client(hass) | ||
|
||
with patch( | ||
"homeassistant.components.homeassistant_green.hardware.get_os_info", | ||
return_value={"board": "green"}, | ||
): | ||
await client.send_json({"id": 1, "type": "hardware/info"}) | ||
msg = await client.receive_json() | ||
|
||
assert msg["id"] == 1 | ||
assert msg["success"] | ||
assert msg["result"] == { | ||
"hardware": [ | ||
{ | ||
"board": { | ||
"hassio_board_id": "green", | ||
"manufacturer": "homeassistant", | ||
"model": "green", | ||
"revision": None, | ||
}, | ||
"config_entries": [config_entry.entry_id], | ||
"dongle": None, | ||
"name": "Home Assistant Green", | ||
"url": None, | ||
} | ||
] | ||
} | ||
|
||
|
||
@pytest.mark.parametrize("os_info", [None, {"board": None}, {"board": "other"}]) | ||
async def test_hardware_info_fail( | ||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, os_info | ||
) -> None: | ||
"""Test async_info raises if os_info is not as expected.""" | ||
mock_integration(hass, MockModule("hassio")) | ||
|
||
# Setup the config entry | ||
config_entry = MockConfigEntry( | ||
data={}, | ||
domain=DOMAIN, | ||
options={}, | ||
title="Home Assistant Green", | ||
) | ||
config_entry.add_to_hass(hass) | ||
with patch( | ||
"homeassistant.components.homeassistant_green.get_os_info", | ||
return_value={"board": "green"}, | ||
): | ||
assert await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
client = await hass_ws_client(hass) | ||
|
||
with patch( | ||
"homeassistant.components.homeassistant_green.hardware.get_os_info", | ||
return_value=os_info, | ||
): | ||
await client.send_json({"id": 1, "type": "hardware/info"}) | ||
msg = await client.receive_json() | ||
|
||
assert msg["id"] == 1 | ||
assert msg["success"] | ||
assert msg["result"] == {"hardware": []} |
Oops, something went wrong.