Get hostname id from get_settings

This commit is contained in:
Erik Badman
2023-04-28 16:34:19 +00:00
committed by Erik Badman
parent 2bfa521068
commit a537b8091e
2 changed files with 17 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ 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 from .const import DOMAIN
from .helper import get_hostname_id
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -32,9 +33,10 @@ async def test_connection(hass: HomeAssistant, data) -> str:
session = async_get_clientsession(hass) session = async_get_clientsession(hass)
async with ApiClient(session, data["host"]) as client: async with ApiClient(session, data["host"]) as client:
await client.login(data["password"]) await client.login(data["password"])
values = await client.get_setting_values("scb:network", "Hostname") hostname_id = await get_hostname_id(client)
values = await client.get_setting_values("scb:network", hostname_id)
return values["scb:network"]["Hostname"] return values["scb:network"][hostname_id]
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

View File

@@ -23,6 +23,7 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_DataT = TypeVar("_DataT") _DataT = TypeVar("_DataT")
KNOWN_HOSTNAME_IDS = ("Network:Hostname", "Hostname")
class Plenticore: class Plenticore:
@@ -69,6 +70,7 @@ class Plenticore:
) )
# get some device meta data # get some device meta data
hostname_id = await get_hostname_id(self._client)
settings = await self._client.get_setting_values( settings = await self._client.get_setting_values(
{ {
"devices:local": [ "devices:local": [
@@ -78,7 +80,7 @@ class Plenticore:
"Properties:VersionIOC", "Properties:VersionIOC",
"Properties:VersionMC", "Properties:VersionMC",
], ],
"scb:network": ["Hostname"], "scb:network": [hostname_id],
} }
) )
@@ -91,7 +93,7 @@ class Plenticore:
identifiers={(DOMAIN, device_local["Properties:SerialNo"])}, identifiers={(DOMAIN, device_local["Properties:SerialNo"])},
manufacturer="Kostal", manufacturer="Kostal",
model=f"{prod1} {prod2}", model=f"{prod1} {prod2}",
name=settings["scb:network"]["Hostname"], name=settings["scb:network"][hostname_id],
sw_version=f'IOC: {device_local["Properties:VersionIOC"]}' sw_version=f'IOC: {device_local["Properties:VersionIOC"]}'
+ f' MC: {device_local["Properties:VersionMC"]}', + f' MC: {device_local["Properties:VersionMC"]}',
) )
@@ -403,3 +405,12 @@ class PlenticoreDataFormatter:
return state return state
return PlenticoreDataFormatter.EM_STATES.get(value) return PlenticoreDataFormatter.EM_STATES.get(value)
async def get_hostname_id(client: ApiClient) -> str:
"""Check for known existing hostname ids."""
all_settings = await client.get_settings()
for entry in all_settings["scb:network"]:
if entry.id in KNOWN_HOSTNAME_IDS:
return entry.id
raise ApiException("Hostname identifier not found in KNOWN_HOSTNAME_IDS")