mirror of
https://github.com/home-assistant/core.git
synced 2026-10-06 06:15:47 -04:00
Mods
This commit is contained in:
@@ -45,6 +45,7 @@ from .const import ( # noqa: F401
|
||||
ATTR_ORDER,
|
||||
ATTR_REMOVE_ENTITIES,
|
||||
CONF_HIDE_MEMBERS,
|
||||
CONF_IGNORE_NON_NUMERIC,
|
||||
DATA_COMPONENT,
|
||||
DOMAIN,
|
||||
GROUP_ORDER,
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
"""The min_max component."""
|
||||
|
||||
import logging
|
||||
from types import MappingProxyType
|
||||
|
||||
from homeassistant.components.group import DOMAIN as GROUP_DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.components.group import (
|
||||
CONF_ENTITIES,
|
||||
CONF_HIDE_MEMBERS,
|
||||
CONF_IGNORE_NON_NUMERIC,
|
||||
DOMAIN as GROUP_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .const import CONF_ENTITY_IDS, CONF_ROUND_DIGITS, DOMAIN
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -14,36 +24,41 @@ _LOGGER = logging.getLogger(__name__)
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Min/Max from a config entry."""
|
||||
|
||||
# Create group config from entry options
|
||||
config = dict(entry.options)
|
||||
config[CONF_ENTITIES] = config.pop(CONF_ENTITY_IDS)
|
||||
config.pop(CONF_ROUND_DIGITS)
|
||||
config[CONF_HIDE_MEMBERS] = False
|
||||
config[CONF_IGNORE_NON_NUMERIC] = False
|
||||
|
||||
# Create new config entry for group component and remove old entry
|
||||
new_config_entry = ConfigEntry(
|
||||
data={},
|
||||
discovery_keys=MappingProxyType({}),
|
||||
domain=GROUP_DOMAIN,
|
||||
minor_version=1,
|
||||
options=config,
|
||||
source=SOURCE_USER,
|
||||
subentries_data=[],
|
||||
title=entry.title,
|
||||
unique_id=None,
|
||||
version=1,
|
||||
)
|
||||
await hass.config_entries.async_add(new_config_entry)
|
||||
await hass.config_entries.async_remove(entry.entry_id)
|
||||
|
||||
# Migrate entity from old entry to new entry
|
||||
entity_reg = er.async_get(hass)
|
||||
if old_entity := entity_reg.async_get_entity_id(
|
||||
SENSOR_DOMAIN, DOMAIN, entry.entry_id
|
||||
):
|
||||
entity_reg.async_update_entity(
|
||||
old_entity, config_entry_id=new_config_entry.entry_id
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Migrate old entry."""
|
||||
_LOGGER.debug("Migrating from version %s.%s", entry.version, entry.minor_version)
|
||||
|
||||
if entry.version > 2:
|
||||
# This means the user has downgraded from a future version
|
||||
return False
|
||||
|
||||
if entry.version == 1:
|
||||
# Migration to group sensor and remove config entry
|
||||
# Sensor needs migration
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
GROUP_DOMAIN, context={"source": SOURCE_IMPORT}, data=entry.options
|
||||
)
|
||||
)
|
||||
hass.config_entries.async_update_entry(entry, version=2)
|
||||
|
||||
_LOGGER.debug(
|
||||
"Migration to version %s.%s successful",
|
||||
entry.version,
|
||||
entry.minor_version,
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"group": {
|
||||
"created_at": "2026-04-10T14:33:19.033744+00:00",
|
||||
"data": {},
|
||||
"disabled_by": null,
|
||||
"discovery_keys": {},
|
||||
"domain": "group",
|
||||
"entry_id": "01KNVWZHXSVPAZD4DGFF8TN94Z",
|
||||
"minor_version": 1,
|
||||
"modified_at": "2026-04-10T14:33:19.033750+00:00",
|
||||
"options": {
|
||||
"entities": ["sensor.carbon_dioxide", "sensor.carbon_monoxide"],
|
||||
"group_type": "sensor",
|
||||
"hide_members": false,
|
||||
"ignore_non_numeric": false,
|
||||
"name": "Olle",
|
||||
"round_digits": 2.0,
|
||||
"type": "min"
|
||||
},
|
||||
"pref_disable_new_entities": false,
|
||||
"pref_disable_polling": false,
|
||||
"source": "import",
|
||||
"subentries": [],
|
||||
"title": "Olle",
|
||||
"unique_id": null,
|
||||
"version": 1
|
||||
},
|
||||
"min_max": {
|
||||
"created_at": "2026-04-10T14:13:58.312444+00:00",
|
||||
"data": {},
|
||||
"disabled_by": null,
|
||||
"discovery_keys": {},
|
||||
"domain": "min_max",
|
||||
"entry_id": "01KNVVW4D8NK8ARQF8GCH7CD5A",
|
||||
"minor_version": 1,
|
||||
"modified_at": "2026-04-10T14:33:19.073168+00:00",
|
||||
"options": {
|
||||
"entity_ids": ["sensor.carbon_dioxide", "sensor.carbon_monoxide"],
|
||||
"name": "Olle",
|
||||
"round_digits": 2.0,
|
||||
"type": "min"
|
||||
},
|
||||
"pref_disable_new_entities": false,
|
||||
"pref_disable_polling": false,
|
||||
"source": "user",
|
||||
"subentries": [],
|
||||
"title": "Olle",
|
||||
"unique_id": null,
|
||||
"version": 2
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ from .const import DOMAIN
|
||||
class MinMaxConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for min_max integration."""
|
||||
|
||||
VERSION = 2
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
|
||||
@@ -6,8 +6,8 @@ import statistics
|
||||
from typing import Any, override
|
||||
|
||||
import voluptuous as vol
|
||||
import yaml
|
||||
|
||||
from homeassistant.components.group import CONF_ENTITIES
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
@@ -33,7 +33,7 @@ from homeassistant.helpers.event import async_track_state_change_event
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
||||
from homeassistant.util import ulid as ulid_util
|
||||
from homeassistant.util import ulid as ulid_util, yaml as yaml_util
|
||||
|
||||
from . import PLATFORMS
|
||||
from .const import CONF_ENTITY_IDS, CONF_ROUND_DIGITS, DOMAIN
|
||||
@@ -51,7 +51,6 @@ ATTR_LAST_ENTITY_ID = "last_entity_id"
|
||||
ATTR_RANGE = "range"
|
||||
ATTR_SUM = "sum"
|
||||
|
||||
CONF_ENTITIES = "entities"
|
||||
|
||||
ICON = "mdi:calculator"
|
||||
|
||||
@@ -87,7 +86,7 @@ async def yaml_deprecation_notice(hass: HomeAssistant, config: ConfigType) -> No
|
||||
platform_config.pop(CONF_PLATFORM)
|
||||
if CONF_NAME not in platform_config:
|
||||
platform_config[CONF_NAME] = f"{platform_config[CONF_TYPE]} sensor".capitalize()
|
||||
yaml_config = yaml.dump(platform_config)
|
||||
yaml_config = yaml_util.dump(platform_config)
|
||||
yaml_config = yaml_config.replace("\n", "\n ")
|
||||
yaml_config = "```yaml\nsensor:\n - platform: group\n " + yaml_config + "\n```"
|
||||
async_create_issue(
|
||||
@@ -118,7 +117,6 @@ async def async_setup_platform(
|
||||
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
await yaml_deprecation_notice(hass, config)
|
||||
print("hej", config)
|
||||
|
||||
async_add_entities(
|
||||
[MinMaxSensor(entity_ids, name, sensor_type, round_digits, unique_id)]
|
||||
|
||||
Reference in New Issue
Block a user