Skip to content

Commit

Permalink
use more unique entity uids for new config entries (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlangbert committed Mar 4, 2024
1 parent 2fa4c4e commit 0cb56af
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
36 changes: 35 additions & 1 deletion custom_components/daikinone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from homeassistant.core import HomeAssistant
from homeassistant.util import Throttle

from custom_components.daikinone.const import PLATFORMS, DOMAIN, MIN_TIME_BETWEEN_UPDATES
from custom_components.daikinone.const import (
CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY,
PLATFORMS,
DOMAIN,
MIN_TIME_BETWEEN_UPDATES,
)
from custom_components.daikinone.daikinone import DaikinOne, DaikinUserCredentials

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -56,3 +61,32 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if ok:
hass.data.pop(DOMAIN)
return ok


async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate old entry."""
log.debug("Migrating from version %s.%s", entry.version, entry.minor_version)

if entry.version > 1:
log.error(
"Incompatible downgrade detected, please restore from a earlier backup or remove and re-add the integration",
entry.version,
entry.minor_version,
)
return False

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

# migrate to 1.2
if entry.minor_version < 2:
entry.minor_version = 2

# retain legacy id schema if this is an upgrade of an existing entry
new[CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY] = 0

hass.config_entries.async_update_entry(entry, data=new)

log.info("Migration to version %s.%s successful", entry.version, entry.minor_version)

return True
7 changes: 5 additions & 2 deletions custom_components/daikinone/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
from homeassistant import config_entries
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD

from .const import DOMAIN
from .const import DOMAIN, CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY
from .daikinone import DaikinOne, DaikinUserCredentials

log = logging.getLogger(__name__)


class DaikinSkyportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class DaikinOneConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Daikin One config flow."""

VERSION = 1
MINOR_VERSION = 2

@property
def schema(self):
Expand All @@ -40,6 +41,8 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None):
data={
CONF_EMAIL: email,
CONF_PASSWORD: password,
# internal options
CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY: 1,
},
)

Expand Down
2 changes: 2 additions & 0 deletions custom_components/daikinone/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
]

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)

CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY = "entity_uid_schema_version"
4 changes: 1 addition & 3 deletions custom_components/daikinone/daikinone.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ def __map_thermostat(self, payload: DaikinDeviceDataResponse) -> DaikinThermosta
if payload.data["ctSystemCapEmergencyHeat"]:
capabilities.add(DaikinThermostatCapability.EMERGENCY_HEAT)

schedule = DaikinThermostatSchedule(enabled=payload.data["schedEnabled"])

thermostat = DaikinThermostat(
id=payload.id,
location_id=payload.locationId,
Expand All @@ -268,7 +266,7 @@ def __map_thermostat(self, payload: DaikinDeviceDataResponse) -> DaikinThermosta
capabilities=capabilities,
mode=DaikinThermostatMode(payload.data["mode"]),
status=DaikinThermostatStatus(payload.data["equipmentStatus"]),
schedule=schedule,
schedule=DaikinThermostatSchedule(enabled=payload.data["schedEnabled"]),
indoor_temperature=Temperature.from_celsius(payload.data["tempIndoor"]),
indoor_humidity=payload.data["humIndoor"],
set_point_heat=Temperature.from_celsius(payload.data["hspActive"]),
Expand Down
19 changes: 17 additions & 2 deletions custom_components/daikinone/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from homeassistant.helpers.typing import StateType

from custom_components.daikinone import DOMAIN, DaikinOneData
from custom_components.daikinone.const import MANUFACTURER
from custom_components.daikinone.const import CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY, MANUFACTURER
from custom_components.daikinone.daikinone import (
DaikinDevice,
DaikinEEVCoil,
Expand Down Expand Up @@ -706,7 +706,6 @@ def __init__(
self._device: D = device
self._attribute = attribute

self._attr_unique_id = f"{self._device.id}-{self.name}"
self._attr_device_info = self.get_device_info()

def get_device_info(self) -> DeviceInfo | None:
Expand Down Expand Up @@ -746,6 +745,14 @@ def __init__(
) -> None:
super().__init__(description, data, device, attribute)

match data.entry.data[CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY]:
case 0:
self._attr_unique_id = f"{self._device.id}-{self.name}"
case 1:
self._attr_unique_id = f"{self._device.id}-{self.entity_description.key}"
case _:
raise ValueError("unexpected entity uid schema version")

@property
def device_name(self) -> str:
return f"{self._device.name} Thermostat"
Expand All @@ -763,6 +770,14 @@ def __init__(
) -> None:
super().__init__(description, data, device, attribute)

match data.entry.data[CONF_OPTION_ENTITY_UID_SCHEMA_VERSION_KEY]:
case 0:
self._attr_unique_id = f"{self._device.id}-{self.name}"
case 1:
self._attr_unique_id = f"{self._device.thermostat_id}-{self._device.id}-{self.entity_description.key}"
case _:
raise ValueError("unexpected entity uid schema version")

@property
def device_name(self) -> str:
thermostat = self._data.daikin.get_thermostat(self._device.thermostat_id)
Expand Down

0 comments on commit 0cb56af

Please sign in to comment.