Skip to content

Commit

Permalink
Use token api
Browse files Browse the repository at this point in the history
(Requires version of neohubapi dependency containing websocket fixes
  of 2025-02-13 and -11)
  • Loading branch information
mr-miles committed Feb 18, 2025
1 parent 21fc70c commit f0b028c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
9 changes: 7 additions & 2 deletions custom_components/heatmiserneo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_API_TOKEN, Platform
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv

Expand Down Expand Up @@ -47,9 +47,14 @@ async def async_setup_entry(
# Set the Hub up to use and save
host = entry.data[CONF_HOST]
port = entry.data[CONF_PORT]
token = entry.data[CONF_API_TOKEN]

# Make this configurable or retrieve from an API later.
hub_serial_number = f"NEOHUB-SN:000000-{host}"
hub = NeoHub(host, port)
if token:
hub = NeoHub(host, port, token=token)
else:
hub = NeoHub(host, port)

coordinator = HeatmiserNeoCoordinator(hass, hub)

Expand Down
37 changes: 34 additions & 3 deletions custom_components/heatmiserneo/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from homeassistant import config_entries
from homeassistant.components.climate import HVACMode
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_API_TOKEN
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_registry import (
Expand All @@ -19,7 +19,7 @@
)
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import CONF_HVAC_MODES, DEFAULT_HOST, DEFAULT_PORT, DOMAIN, AvailableMode
from .const import CONF_HVAC_MODES, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_TOKEN, DOMAIN, AvailableMode

_LOGGER = logging.getLogger(__name__)

Expand All @@ -37,14 +37,43 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow."""

VERSION = 1
MINOR_VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH

def __init__(self) -> None:
"""Initialize Heatmiser Neo options flow."""
self._host = DEFAULT_HOST
self._port = DEFAULT_PORT
self._token = DEFAULT_TOKEN
self._errors = None

async def async_migrate_entry(self, config_entry: config_entries.ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug("Migrating configuration from version %s.%s",
config_entry.version,
config_entry.minor_version
)

if config_entry.version != 1:
# This means the user has downgraded from a future version
return False

if config_entry.minor_version is None or config_entry.minor_version < 1:
new_data = {**config_entry.data, CONF_API_TOKEN: DEFAULT_TOKEN}

self.config_entries.async_update_entry(config_entry,
data=new_data,
minor_version=FlowHandler.MINOR_VERSION,
version=FlowHandler.VERSION
)

_LOGGER.debug("Migration to configuration version %s.%s successful",
config_entry.version,
config_entry.minor_version
)

return True

async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType):
"""Handle zeroconf discovery."""
_LOGGER.debug("Zeroconfig discovered %s", discovery_info)
Expand Down Expand Up @@ -85,7 +114,7 @@ async def try_connection(self):
def _async_get_entry(self):
return self.async_create_entry(
title=f"{self._host}:{self._port}",
data={CONF_HOST: self._host, CONF_PORT: self._port},
data={CONF_HOST: self._host, CONF_PORT: self._port, CONF_API_TOKEN: self._token},
)

async def async_step_user(self, user_input=None):
Expand All @@ -95,6 +124,7 @@ async def async_step_user(self, user_input=None):
if user_input is not None:
self._host = user_input[CONF_HOST]
self._port = user_input[CONF_PORT]
self._token = user_input[CONF_API_TOKEN]

await self.async_set_unique_id(f"{self._host}:{self._port}")
self._abort_if_unique_id_configured()
Expand All @@ -111,6 +141,7 @@ async def async_step_user(self, user_input=None):
{
vol.Required(CONF_HOST, default=self._host): str,
vol.Required(CONF_PORT, default=self._port): int,
vol.Optional(CONF_API_TOKEN, default=self._token): str
}
),
errors=self._errors,
Expand Down
6 changes: 4 additions & 2 deletions custom_components/heatmiserneo/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"title": "Heatmiser Neo",
"data": {
"host": "Host",
"port": "Port"
"port": "Port",
"api_token": "API Token"
},
"data_description": {
"host": "The hostname or IP address of the NeoHub to connect to.",
"port": "The TCP port of the NeoHub to connect to (4242 for the Legacy API)."
"port": "The TCP port of the NeoHub to connect to (4242 for the Legacy API).",
"api_token": "The API token to access the NeoHub API"
}
}
},
Expand Down

0 comments on commit f0b028c

Please sign in to comment.