Extended epson projector integration to include serial connections (#121630)

* Extended epson projector integration to include serial connections

* Fix review changes

* Improve epson types and translations

* Fix comment

---------

Co-authored-by: Joostlek <joostlek@outlook.com>
pull/125151/head
S 2024-09-03 14:46:57 +01:00 committed by GitHub
parent 733bbf9cd1
commit 8e3ad2d1f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 112 additions and 11 deletions

View File

@ -13,7 +13,7 @@ from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN, HTTP
from .const import CONF_CONNECTION_TYPE, DOMAIN, HTTP
from .exceptions import CannotConnect, PoweredOff
PLATFORMS = [Platform.MEDIA_PLAYER]
@ -22,13 +22,17 @@ _LOGGER = logging.getLogger(__name__)
async def validate_projector(
hass: HomeAssistant, host, check_power=True, check_powered_on=True
hass: HomeAssistant,
host: str,
conn_type: str,
check_power: bool = True,
check_powered_on: bool = True,
):
"""Validate the given projector host allows us to connect."""
epson_proj = Projector(
host=host,
websession=async_get_clientsession(hass, verify_ssl=False),
type=HTTP,
type=conn_type,
)
if check_power:
_power = await epson_proj.get_power()
@ -46,6 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
projector = await validate_projector(
hass=hass,
host=entry.data[CONF_HOST],
conn_type=entry.data[CONF_CONNECTION_TYPE],
check_power=False,
check_powered_on=False,
)
@ -60,5 +65,33 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
projector = hass.data[DOMAIN].pop(entry.entry_id)
projector.close()
return unload_ok
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate old entry."""
_LOGGER.debug(
"Migrating configuration from version %s.%s",
config_entry.version,
config_entry.minor_version,
)
if config_entry.version > 1 or config_entry.minor_version > 1:
# This means the user has downgraded from a future version
return False
if config_entry.version == 1 and config_entry.minor_version == 1:
new_data = {**config_entry.data}
new_data[CONF_CONNECTION_TYPE] = HTTP
hass.config_entries.async_update_entry(
config_entry, data=new_data, version=1, minor_version=2
)
_LOGGER.debug(
"Migration to configuration version %s successful", config_entry.version
)
return True

View File

@ -7,13 +7,21 @@ import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
from . import validate_projector
from .const import DOMAIN
from .const import CONF_CONNECTION_TYPE, DOMAIN, HTTP, SERIAL
from .exceptions import CannotConnect, PoweredOff
ALLOWED_CONNECTION_TYPE = [HTTP, SERIAL]
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_CONNECTION_TYPE, default=HTTP): SelectSelector(
SelectSelectorConfig(
options=ALLOWED_CONNECTION_TYPE, translation_key="connection_type"
)
),
vol.Required(CONF_HOST): str,
vol.Required(CONF_NAME, default=DOMAIN): str,
}
@ -26,6 +34,7 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for epson."""
VERSION = 1
MINOR_VERSION = 2
async def async_step_user(
self, user_input: dict[str, Any] | None = None
@ -33,12 +42,16 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle the initial step."""
errors = {}
if user_input is not None:
# Epson projector doesn't appear to need to be on for serial
check_power = user_input[CONF_CONNECTION_TYPE] != SERIAL
projector = None
try:
projector = await validate_projector(
hass=self.hass,
conn_type=user_input[CONF_CONNECTION_TYPE],
host=user_input[CONF_HOST],
check_power=True,
check_powered_on=True,
check_powered_on=check_power,
)
except CannotConnect:
errors["base"] = "cannot_connect"
@ -55,6 +68,9 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_create_entry(
title=user_input.pop(CONF_NAME), data=user_input
)
finally:
if projector:
projector.close()
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)

View File

@ -2,6 +2,8 @@
DOMAIN = "epson"
SERVICE_SELECT_CMODE = "select_cmode"
CONF_CONNECTION_TYPE = "connection_type"
ATTR_CMODE = "cmode"
HTTP = "http"
SERIAL = "serial"

View File

@ -3,11 +3,12 @@
"step": {
"user": {
"data": {
"connection_type": "Connection type",
"host": "[%key:common::config_flow::data::host%]",
"name": "[%key:common::config_flow::data::name%]"
},
"data_description": {
"host": "The hostname or IP address of your Epson projector."
"host": "The hostname, IP address or serial port of your Epson projector."
}
}
},
@ -30,5 +31,13 @@
}
}
}
},
"selector": {
"connection_type": {
"options": {
"http": "HTTP",
"serial": "Serial"
}
}
}
}

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
from epson_projector.const import PWR_OFF_STATE
from homeassistant import config_entries
from homeassistant.components.epson.const import DOMAIN
from homeassistant.components.epson.const import CONF_CONNECTION_TYPE, DOMAIN, HTTP
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -33,6 +33,10 @@ async def test_form(hass: HomeAssistant) -> None:
patch(
"homeassistant.components.epson.async_setup_entry",
return_value=True,
),
patch(
"homeassistant.components.epson.Projector.close",
return_value=True,
) as mock_setup_entry,
):
result2 = await hass.config_entries.flow.async_configure(
@ -43,7 +47,7 @@ async def test_form(hass: HomeAssistant) -> None:
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "test-epson"
assert result2["data"] == {CONF_HOST: "1.1.1.1"}
assert result2["data"] == {CONF_CONNECTION_TYPE: HTTP, CONF_HOST: "1.1.1.1"}
assert len(mock_setup_entry.mock_calls) == 1

View File

@ -0,0 +1,37 @@
"""Test the epson init."""
from unittest.mock import patch
from homeassistant.components.epson.const import CONF_CONNECTION_TYPE, DOMAIN
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_migrate_entry(hass: HomeAssistant) -> None:
"""Test successful migration of entry data from version 1 to 1.2."""
mock_entry = MockConfigEntry(
domain=DOMAIN,
title="Epson",
version=1,
minor_version=1,
data={CONF_HOST: "1.1.1.1"},
entry_id="1cb78c095906279574a0442a1f0003ef",
)
assert mock_entry.version == 1
mock_entry.add_to_hass(hass)
# Create entity entry to migrate to new unique ID
with patch("homeassistant.components.epson.Projector.get_power"):
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
# Check that is now has connection_type
assert mock_entry
assert mock_entry.version == 1
assert mock_entry.minor_version == 2
assert mock_entry.data.get(CONF_CONNECTION_TYPE) == "http"
assert mock_entry.data.get(CONF_HOST) == "1.1.1.1"

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from homeassistant.components.epson.const import DOMAIN
from homeassistant.components.epson.const import CONF_CONNECTION_TYPE, DOMAIN, HTTP
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -22,7 +22,7 @@ async def test_set_unique_id(
entry = MockConfigEntry(
domain=DOMAIN,
title="Epson",
data={CONF_HOST: "1.1.1.1"},
data={CONF_CONNECTION_TYPE: HTTP, CONF_HOST: "1.1.1.1"},
entry_id="1cb78c095906279574a0442a1f0003ef",
)
entry.add_to_hass(hass)