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>
This commit is contained in:
parent
733bbf9cd1
commit
8e3ad2d1f3
7 changed files with 112 additions and 11 deletions
|
@ -13,7 +13,7 @@ from homeassistant.const import CONF_HOST, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
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
|
from .exceptions import CannotConnect, PoweredOff
|
||||||
|
|
||||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||||
|
@ -22,13 +22,17 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def validate_projector(
|
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."""
|
"""Validate the given projector host allows us to connect."""
|
||||||
epson_proj = Projector(
|
epson_proj = Projector(
|
||||||
host=host,
|
host=host,
|
||||||
websession=async_get_clientsession(hass, verify_ssl=False),
|
websession=async_get_clientsession(hass, verify_ssl=False),
|
||||||
type=HTTP,
|
type=conn_type,
|
||||||
)
|
)
|
||||||
if check_power:
|
if check_power:
|
||||||
_power = await epson_proj.get_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(
|
projector = await validate_projector(
|
||||||
hass=hass,
|
hass=hass,
|
||||||
host=entry.data[CONF_HOST],
|
host=entry.data[CONF_HOST],
|
||||||
|
conn_type=entry.data[CONF_CONNECTION_TYPE],
|
||||||
check_power=False,
|
check_power=False,
|
||||||
check_powered_on=False,
|
check_powered_on=False,
|
||||||
)
|
)
|
||||||
|
@ -60,5 +65,33 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
projector = hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
projector.close()
|
||||||
return unload_ok
|
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
|
||||||
|
|
|
@ -7,13 +7,21 @@ import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
||||||
|
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
|
||||||
|
|
||||||
from . import validate_projector
|
from . import validate_projector
|
||||||
from .const import DOMAIN
|
from .const import CONF_CONNECTION_TYPE, DOMAIN, HTTP, SERIAL
|
||||||
from .exceptions import CannotConnect, PoweredOff
|
from .exceptions import CannotConnect, PoweredOff
|
||||||
|
|
||||||
|
ALLOWED_CONNECTION_TYPE = [HTTP, SERIAL]
|
||||||
|
|
||||||
DATA_SCHEMA = vol.Schema(
|
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_HOST): str,
|
||||||
vol.Required(CONF_NAME, default=DOMAIN): str,
|
vol.Required(CONF_NAME, default=DOMAIN): str,
|
||||||
}
|
}
|
||||||
|
@ -26,6 +34,7 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for epson."""
|
"""Handle a config flow for epson."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
MINOR_VERSION = 2
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
@ -33,12 +42,16 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
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:
|
try:
|
||||||
projector = await validate_projector(
|
projector = await validate_projector(
|
||||||
hass=self.hass,
|
hass=self.hass,
|
||||||
|
conn_type=user_input[CONF_CONNECTION_TYPE],
|
||||||
host=user_input[CONF_HOST],
|
host=user_input[CONF_HOST],
|
||||||
check_power=True,
|
check_power=True,
|
||||||
check_powered_on=True,
|
check_powered_on=check_power,
|
||||||
)
|
)
|
||||||
except CannotConnect:
|
except CannotConnect:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
@ -55,6 +68,9 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input.pop(CONF_NAME), data=user_input
|
title=user_input.pop(CONF_NAME), data=user_input
|
||||||
)
|
)
|
||||||
|
finally:
|
||||||
|
if projector:
|
||||||
|
projector.close()
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
DOMAIN = "epson"
|
DOMAIN = "epson"
|
||||||
SERVICE_SELECT_CMODE = "select_cmode"
|
SERVICE_SELECT_CMODE = "select_cmode"
|
||||||
|
CONF_CONNECTION_TYPE = "connection_type"
|
||||||
|
|
||||||
ATTR_CMODE = "cmode"
|
ATTR_CMODE = "cmode"
|
||||||
HTTP = "http"
|
HTTP = "http"
|
||||||
|
SERIAL = "serial"
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
"step": {
|
"step": {
|
||||||
"user": {
|
"user": {
|
||||||
"data": {
|
"data": {
|
||||||
|
"connection_type": "Connection type",
|
||||||
"host": "[%key:common::config_flow::data::host%]",
|
"host": "[%key:common::config_flow::data::host%]",
|
||||||
"name": "[%key:common::config_flow::data::name%]"
|
"name": "[%key:common::config_flow::data::name%]"
|
||||||
},
|
},
|
||||||
"data_description": {
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
||||||
from epson_projector.const import PWR_OFF_STATE
|
from epson_projector.const import PWR_OFF_STATE
|
||||||
|
|
||||||
from homeassistant import config_entries
|
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.const import CONF_HOST, CONF_NAME, STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
@ -33,6 +33,10 @@ async def test_form(hass: HomeAssistant) -> None:
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.epson.async_setup_entry",
|
"homeassistant.components.epson.async_setup_entry",
|
||||||
return_value=True,
|
return_value=True,
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.epson.Projector.close",
|
||||||
|
return_value=True,
|
||||||
) as mock_setup_entry,
|
) as mock_setup_entry,
|
||||||
):
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
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["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2["title"] == "test-epson"
|
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
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
|
37
tests/components/epson/test_init.py
Normal file
37
tests/components/epson/test_init.py
Normal 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"
|
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
||||||
|
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
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.const import CONF_HOST
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
@ -22,7 +22,7 @@ async def test_set_unique_id(
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
title="Epson",
|
title="Epson",
|
||||||
data={CONF_HOST: "1.1.1.1"},
|
data={CONF_CONNECTION_TYPE: HTTP, CONF_HOST: "1.1.1.1"},
|
||||||
entry_id="1cb78c095906279574a0442a1f0003ef",
|
entry_id="1cb78c095906279574a0442a1f0003ef",
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
Loading…
Add table
Reference in a new issue