Skip to content

Commit

Permalink
Bump Intellifire to 4.1.9 (#121091)
Browse files Browse the repository at this point in the history
* rebase

* Minor patch to fix duplicate DeviceInfo beign created - if data hasnt updated yet

* rebase

* Minor patch to fix duplicate DeviceInfo beign created - if data hasnt updated yet

* fixing formatting

* Update homeassistant/components/intellifire/__init__.py

Co-authored-by: Erik Montnemery <[email protected]>

* Update homeassistant/components/intellifire/__init__.py

Co-authored-by: Erik Montnemery <[email protected]>

* Removing cloud connectivity sensor - leaving local one in

* Renaming class to something more useful

* addressing pr

* Update homeassistant/components/intellifire/__init__.py

Co-authored-by: Erik Montnemery <[email protected]>

* add ruff exception

* Fix test annotations

* remove access to private variable

* Bumping to 4.1.9 instead of 4.1.5

* A renaming

* rename

* Updated testing

* Update __init__.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

* updateing styrings

* Update tests/components/intellifire/conftest.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

* Testing refactor - WIP

* everything is passing - cleanup still needed

* cleaning up comments

* update pr

* unrename

* Update homeassistant/components/intellifire/coordinator.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

* fixing sentence

* fixed fixture and removed error codes

* reverted a bad change

* fixing strings.json

* revert renaming

* fix

* typing inother pr

* adding extra tests - one has a really dumb name

* using a real value

* added a migration in

* Update homeassistant/components/intellifire/config_flow.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

* Update tests/components/intellifire/test_init.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

* cleanup continues

* addressing pr

* switch back to debug

* Update tests/components/intellifire/conftest.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

* some changes

* restore property mock cuase didnt work otherwise

* cleanup has begun

* removed extra text

* addressing pr stuff

* fixed reauth

---------

Co-authored-by: Erik Montnemery <[email protected]>
Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
3 people authored Sep 1, 2024
1 parent 1661304 commit 12336f5
Show file tree
Hide file tree
Showing 28 changed files with 2,420 additions and 653 deletions.
170 changes: 109 additions & 61 deletions homeassistant/components/intellifire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@

from __future__ import annotations

from aiohttp import ClientConnectionError
from intellifire4py import IntellifireControlAsync
from intellifire4py.exceptions import LoginException
from intellifire4py.intellifire import IntellifireAPICloud, IntellifireAPILocal
import asyncio

from intellifire4py import UnifiedFireplace
from intellifire4py.cloud_interface import IntelliFireCloudInterface
from intellifire4py.model import IntelliFireCommonFireplaceData

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
CONF_HOST,
CONF_IP_ADDRESS,
CONF_PASSWORD,
CONF_USERNAME,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady

from .const import CONF_USER_ID, DOMAIN, LOGGER
from .const import (
CONF_AUTH_COOKIE,
CONF_CONTROL_MODE,
CONF_READ_MODE,
CONF_SERIAL,
CONF_USER_ID,
CONF_WEB_CLIENT_ID,
DOMAIN,
INIT_WAIT_TIME_SECONDS,
LOGGER,
STARTUP_TIMEOUT,
)
from .coordinator import IntellifireDataUpdateCoordinator

PLATFORMS = [
Expand All @@ -32,79 +45,114 @@
]


def _construct_common_data(entry: ConfigEntry) -> IntelliFireCommonFireplaceData:
"""Convert config entry data into IntelliFireCommonFireplaceData."""

return IntelliFireCommonFireplaceData(
auth_cookie=entry.data[CONF_AUTH_COOKIE],
user_id=entry.data[CONF_USER_ID],
web_client_id=entry.data[CONF_WEB_CLIENT_ID],
serial=entry.data[CONF_SERIAL],
api_key=entry.data[CONF_API_KEY],
ip_address=entry.data[CONF_IP_ADDRESS],
read_mode=entry.options[CONF_READ_MODE],
control_mode=entry.options[CONF_CONTROL_MODE],
)


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate entries."""
LOGGER.debug(
"Migrating configuration from version %s.%s",
config_entry.version,
config_entry.minor_version,
)

if config_entry.version == 1:
new = {**config_entry.data}

if config_entry.minor_version < 2:
username = config_entry.data[CONF_USERNAME]
password = config_entry.data[CONF_PASSWORD]

# Create a Cloud Interface
async with IntelliFireCloudInterface() as cloud_interface:
await cloud_interface.login_with_credentials(
username=username, password=password
)

new_data = cloud_interface.user_data.get_data_for_ip(new[CONF_HOST])

if not new_data:
raise ConfigEntryAuthFailed
new[CONF_API_KEY] = new_data.api_key
new[CONF_WEB_CLIENT_ID] = new_data.web_client_id
new[CONF_AUTH_COOKIE] = new_data.auth_cookie

new[CONF_IP_ADDRESS] = new_data.ip_address
new[CONF_SERIAL] = new_data.serial

hass.config_entries.async_update_entry(
config_entry,
data=new,
options={CONF_READ_MODE: "local", CONF_CONTROL_MODE: "local"},
unique_id=new[CONF_SERIAL],
version=1,
minor_version=2,
)
LOGGER.debug("Pseudo Migration %s successful", config_entry.version)

return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up IntelliFire from a config entry."""
LOGGER.debug("Setting up config entry: %s", entry.unique_id)

if CONF_USERNAME not in entry.data:
LOGGER.debug("Old config entry format detected: %s", entry.unique_id)
LOGGER.debug("Config entry without username detected: %s", entry.unique_id)
raise ConfigEntryAuthFailed

ift_control = IntellifireControlAsync(
fireplace_ip=entry.data[CONF_HOST],
)
try:
await ift_control.login(
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD],
)
except (ConnectionError, ClientConnectionError) as err:
raise ConfigEntryNotReady from err
except LoginException as err:
raise ConfigEntryAuthFailed(err) from err

finally:
await ift_control.close()

# Extract API Key and User_ID from ift_control
# Eventually this will migrate to using IntellifireAPICloud

if CONF_USER_ID not in entry.data or CONF_API_KEY not in entry.data:
LOGGER.info(
"Updating intellifire config entry for %s with api information",
entry.unique_id,
fireplace: UnifiedFireplace = (
await UnifiedFireplace.build_fireplace_from_common(
_construct_common_data(entry)
)
)
cloud_api = IntellifireAPICloud()
await cloud_api.login(
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD],
LOGGER.debug("Waiting for Fireplace to Initialize")
await asyncio.wait_for(
_async_wait_for_initialization(fireplace), timeout=STARTUP_TIMEOUT
)
api_key = cloud_api.get_fireplace_api_key()
user_id = cloud_api.get_user_id()
# Update data entry
hass.config_entries.async_update_entry(
entry,
data={
**entry.data,
CONF_API_KEY: api_key,
CONF_USER_ID: user_id,
},
)

else:
api_key = entry.data[CONF_API_KEY]
user_id = entry.data[CONF_USER_ID]

# Instantiate local control
api = IntellifireAPILocal(
fireplace_ip=entry.data[CONF_HOST],
api_key=api_key,
user_id=user_id,
except TimeoutError as err:
raise ConfigEntryNotReady(
"Initialization of fireplace timed out after 10 minutes"
) from err

# Construct coordinator
data_update_coordinator = IntellifireDataUpdateCoordinator(
hass=hass, fireplace=fireplace
)

# Define the update coordinator
coordinator = IntellifireDataUpdateCoordinator(
hass=hass,
api=api,
)
LOGGER.debug("Fireplace to Initialized - Awaiting first refresh")
await data_update_coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = data_update_coordinator

await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


async def _async_wait_for_initialization(
fireplace: UnifiedFireplace, timeout=STARTUP_TIMEOUT
):
"""Wait for a fireplace to be initialized."""
while (
fireplace.data.ipv4_address == "127.0.0.1" and fireplace.data.serial == "unset"
):
LOGGER.debug(f"Waiting for fireplace to initialize [{fireplace.read_mode}]")
await asyncio.sleep(INIT_WAIT_TIME_SECONDS)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/intellifire/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Callable
from dataclasses import dataclass

from intellifire4py import IntellifirePollData
from intellifire4py.model import IntelliFirePollData

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
Expand All @@ -26,7 +26,7 @@
class IntellifireBinarySensorRequiredKeysMixin:
"""Mixin for required keys."""

value_fn: Callable[[IntellifirePollData], bool]
value_fn: Callable[[IntelliFirePollData], bool]


@dataclass(frozen=True)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/intellifire/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(
super().__init__(coordinator, description)

if coordinator.data.thermostat_on:
self.last_temp = coordinator.data.thermostat_setpoint_c
self.last_temp = int(coordinator.data.thermostat_setpoint_c)

@property
def hvac_mode(self) -> HVACMode:
Expand Down
Loading

0 comments on commit 12336f5

Please sign in to comment.