Skip to content

Commit

Permalink
Use ipv4 in zeroconf when core is using ipv4 address (#110)
Browse files Browse the repository at this point in the history
* Set ipv4 autodiscovery as default
* update readme for ip autodetect
  • Loading branch information
posixx authored Jul 18, 2023
1 parent 64f9c85 commit 04550ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,4 @@ This is a HACS custom integration for enphase envoys with firmware version 7.X.
- Username / Password / Use Enlighten [#73](https://github.com/briancmpbll/home_assistant_custom_envoy/issues/73)\
When configuring the Envoy with firmware 7 or higher specify your Enphase Enlighten username and password, the envoy serial number and check the 'Use Enlighten' box at the bottom. This will allow the integration to collect a token from the enphase website and use it to access the Envoy locally. It does this at first configuration, at each HA startup or at reload of the integration. The Enphase web-site is known to be slow or satured at times. When an *Unknown Error* is reported during configuration try again until success. [#81](https://github.com/briancmpbll/home_assistant_custom_envoy/issues/81) \
\
Upon changing your password on the Enphase web site you will have to update the password information in HA. To update it, delete the envoy integration from the Settings / Integrations window. Restart HA and then in Integrations window configure it again. All data is kept and will show again once it's configured.

- [#75 Invalid Port and IPV6 autodetect](https://github.com/briancmpbll/home_assistant_custom_envoy/issues/75) \
HA performs auto discovery for Envoy on the network. When it identifies an Envoy it will use the Envoy serial number
to identify a configured Envoy and then update the IP addres of the Envoy. If the auto discovery returns an IPV6 address it will update the Envoy with reported IPv6 address even if it was configured with an IPv4 address before. This causes the integration to fail as IPv6 addresses cause issues in the communication to the Envoy. \
\
To solve this and prevent this from happening again, remove the Envoy in the Integrations panel, restart HA and configure the Envoy again with the IPv4 address. This may require to change the IP address to the IPv4 on the auto detected Envoy or add manually an Envoy Integration. \
\
Then open the *System options* on the Envoy Integrations menu (3 vertical dots). In the System options panel de-activate the *Enable Newly Added Entities* option to turn it off. This will cause the Envoy Intgeration to ignore autodetect updates and keep the configured IP address. Make sure the Envoy is using a fixed IP address to avoid loosing connection if it changes its IP.
Upon changing your password on the Enphase web site you will have to update the password information in HA. To update it, delete the envoy integration from the Settings / Integrations window. Restart HA and then in Integrations window configure it again. All data is kept and will show again once it's configured.
18 changes: 12 additions & 6 deletions custom_components/enphase_envoy_custom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import network
from homeassistant.components import zeroconf
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util.network import is_ipv4_address

from .const import DOMAIN, CONF_SERIAL, CONF_USE_ENLIGHTEN, DEFAULT_SCAN_INTERVAL

Expand Down Expand Up @@ -47,6 +49,12 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> EnvoyRead

return envoy_reader

async def ipv4asdefault(hass: HomeAssistant):
adapters = await network.async_get_adapters(hass)
for adapter in adapters:
if adapter["default"]:
return adapter["ipv4"] is not None
return False

class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Enphase Envoy."""
Expand Down Expand Up @@ -93,12 +101,10 @@ async def async_step_zeroconf(
serial = discovery_info.properties["serialnum"]
await self.async_set_unique_id(serial)

#75 If system option to enable newly discoverd entries is off (by user) and uniqueid is this serial then skip updating ip
for entry in self._async_current_entries(include_ignore=False):
if entry.pref_disable_new_entities and entry.unique_id is not None:
if entry.unique_id == serial:
_LOGGER.debug("Envoy autodiscovery/ip update disabled for: %s, IP detected: %s %s",serial, discovery_info.host,entry.unique_id)
return self.async_abort(reason="pref_disable_new_entities")
ipv4_default = await ipv4asdefault(self.hass)

if ipv4_default and not is_ipv4_address(discovery_info.host):
return self.async_abort(reason="not_ipv4_address")

# autodiscovery is updating the ip address of an existing envoy with matching serial to new detected ip adress
self.ip_address = discovery_info.host
Expand Down

0 comments on commit 04550ce

Please sign in to comment.