Skip to content

Commit

Permalink
Merge pull request #239 from jm-73/develop
Browse files Browse the repository at this point in the history
Config flow and user-agent improvements
  • Loading branch information
sander1988 authored Jun 30, 2024
2 parents d6bf786 + a8e4de7 commit 56a4099
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
19 changes: 14 additions & 5 deletions custom_components/indego/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from homeassistant.config_entries import OptionsFlowWithConfigEntry, ConfigEntry
from homeassistant.core import callback

from pyIndego.const import DEFAULT_HEADERS
from pyIndego import IndegoAsyncClient

from .const import (
Expand All @@ -24,6 +23,8 @@
CONF_USER_AGENT,
OAUTH2_CLIENT_ID,
HTTP_HEADER_USER_AGENT,
HTTP_HEADER_USER_AGENT_DEFAULT,
HTTP_HEADER_USER_AGENT_DEFAULTS
)

_LOGGER: Final = logging.getLogger(__name__)
Expand All @@ -32,11 +33,19 @@
def default_user_agent_in_config(config: dict) -> bool:
if CONF_USER_AGENT not in config:
return True

if config[CONF_USER_AGENT] is None:
return True

if config[CONF_USER_AGENT] == "":
return True
return config[CONF_USER_AGENT] == DEFAULT_HEADERS[HTTP_HEADER_USER_AGENT]

for default_header in HTTP_HEADER_USER_AGENT_DEFAULTS:
# Not perfect, but the best what we can do...
# Test for exact match and header + space, which probably has the version suffix.
if config[CONF_USER_AGENT] == default_header or config[CONF_USER_AGENT].startswith(default_header + ' '):
return True
return False


class IndegoOptionsFlowHandler(OptionsFlowWithConfigEntry):
Expand All @@ -57,7 +66,7 @@ async def async_step_init(
vol.Optional(
CONF_USER_AGENT,
description={
"suggested_value": self.options.get(CONF_USER_AGENT, DEFAULT_HEADERS[HTTP_HEADER_USER_AGENT])
"suggested_value": self.options.get(CONF_USER_AGENT, HTTP_HEADER_USER_AGENT_DEFAULT)
},
): str,
vol.Optional(
Expand All @@ -77,7 +86,7 @@ async def async_step_init(
def _save_config(self, data: dict[str, Any]) -> FlowResult:
"""Save the updated options."""

if default_user_agent_in_config(data):
if CONF_USER_AGENT in data and default_user_agent_in_config(data):
del data[CONF_USER_AGENT]

_LOGGER.debug("Updating config options: '%s'", data)
Expand Down Expand Up @@ -177,7 +186,7 @@ async def async_step_advanced(
vol.Optional(
CONF_USER_AGENT,
description={
"suggested_value": DEFAULT_HEADERS[HTTP_HEADER_USER_AGENT]
"suggested_value": HTTP_HEADER_USER_AGENT_DEFAULT
},
): str,
vol.Optional(
Expand Down
5 changes: 5 additions & 0 deletions custom_components/indego/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@
ENTITY_LAWN_MOWER: Final = "lawn_mower"

HTTP_HEADER_USER_AGENT: Final = "User-Agent"
HTTP_HEADER_USER_AGENT_DEFAULT: Final = "HA/Indego"
HTTP_HEADER_USER_AGENT_DEFAULTS: Final = [
"HomeAssistant/Indego",
"HA/Indego"
]
4 changes: 2 additions & 2 deletions custom_components/indego/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"documentation": "https://github.com/jm-73/Indego",
"dependencies": ["application_credentials"],
"codeowners": ["@jm-73", "@eavanvalkenburg", "@sander1988"],
"requirements": ["git+https://github.com/jm-73/[email protected].0"],
"requirements": ["git+https://github.com/jm-73/[email protected].1"],
"iot_class": "cloud_push",
"version": "5.7.1",
"version": "5.7.2",
"loggers": ["custom_components.indego", "pyIndego"]
}

0 comments on commit 56a4099

Please sign in to comment.