From 3856088e791f400ae39cc1bf91b82c21569e5c2c Mon Sep 17 00:00:00 2001 From: "d.dmitriev" Date: Sun, 16 Feb 2025 14:17:34 +0500 Subject: [PATCH 1/3] fix HA 2025.1 compatibility; cleanup --- custom_components/mega/binary_sensor.py | 2 +- custom_components/mega/const.py | 9 ++++--- custom_components/mega/entities.py | 1 - custom_components/mega/http.py | 2 +- custom_components/mega/hub.py | 8 +++--- custom_components/mega/i2c.py | 36 ++++++++++++------------- custom_components/mega/light.py | 24 ++++++++++------- custom_components/mega/manifest.json | 5 ++-- install.sh | 2 +- 9 files changed, 47 insertions(+), 42 deletions(-) diff --git a/custom_components/mega/binary_sensor.py b/custom_components/mega/binary_sensor.py index a4e3d45..5cf62ff 100644 --- a/custom_components/mega/binary_sensor.py +++ b/custom_components/mega/binary_sensor.py @@ -17,7 +17,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.helpers.template import Template -from .const import EVENT_BINARY_SENSOR, DOMAIN, CONF_CUSTOM, CONF_SKIP, CONF_INVERT, CONF_RESPONSE_TEMPLATE +from .const import DOMAIN, CONF_CUSTOM, CONF_SKIP, CONF_INVERT from .entities import MegaPushEntity from .hub import MegaD from .tools import int_ignore diff --git a/custom_components/mega/const.py b/custom_components/mega/const.py index 3c4b4f5..a06d0f4 100644 --- a/custom_components/mega/const.py +++ b/custom_components/mega/const.py @@ -1,6 +1,7 @@ """Constants for the mega integration.""" import re from itertools import permutations +from homeassistant.const import Platform DOMAIN = "mega" CONF_MEGA_ID = "mega_id" @@ -53,10 +54,10 @@ CONF_FILTER_HIGH = 'filter_high' CONF_1WBUS = '1wbus' CONF_ADDR = 'addr' PLATFORMS = [ - "light", - "switch", - "binary_sensor", - "sensor", + Platform.LIGHT, + Platform.SWITCH, + Platform.BINARY_SENSOR, + Platform.SENSOR, ] EVENT_BINARY_SENSOR = f'{DOMAIN}.sensor' EVENT_BINARY = f'{DOMAIN}.binary' diff --git a/custom_components/mega/entities.py b/custom_components/mega/entities.py index b9d79be..16d03ec 100644 --- a/custom_components/mega/entities.py +++ b/custom_components/mega/entities.py @@ -16,7 +16,6 @@ from .const import ( DOMAIN, CONF_CUSTOM, CONF_INVERT, - EVENT_BINARY_SENSOR, LONG, LONG_RELEASE, RELEASE, diff --git a/custom_components/mega/http.py b/custom_components/mega/http.py index 86d2698..28eef97 100644 --- a/custom_components/mega/http.py +++ b/custom_components/mega/http.py @@ -10,7 +10,7 @@ from aiohttp.web_response import Response from homeassistant.helpers.template import Template from homeassistant.components.http import HomeAssistantView from homeassistant.core import HomeAssistant -from .const import EVENT_BINARY_SENSOR, DOMAIN, CONF_RESPONSE_TEMPLATE +from .const import EVENT_BINARY_SENSOR, CONF_RESPONSE_TEMPLATE from .tools import make_ints from . import hub as h diff --git a/custom_components/mega/hub.py b/custom_components/mega/hub.py index e0c489d..200d87c 100644 --- a/custom_components/mega/hub.py +++ b/custom_components/mega/hub.py @@ -12,16 +12,15 @@ from bs4 import BeautifulSoup from homeassistant.components.sensor import SensorDeviceClass from homeassistant.config_entries import ConfigEntry -from homeassistant.const import TEMP_CELSIUS, PERCENTAGE, LIGHT_LUX +from homeassistant.const import PERCENTAGE, LIGHT_LUX, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator -from .config_parser import parse_config, DS2413, MCP230, MCP230_OUT, MCP230_IN, PCA9685 +from .config_parser import parse_config, DS2413, MCP230, PCA9685 from .const import ( TEMP, HUM, PRESS, LUX, - PATT_SPLIT, DOMAIN, CONF_HTTP, EVENT_BINARY_SENSOR, @@ -29,7 +28,6 @@ from .const import ( CONF_FORCE_D, CONF_DEF_RESPONSE, PATT_FW, - CONF_FORCE_I2C_SCAN, REMOVE_CONFIG, ) from .entities import set_events_off, BaseMegaEntity, MegaOutPort, safe_int, safe_float @@ -42,7 +40,7 @@ HUM_PATT = re.compile(r"hum:([01234567890\.]+)") PRESS_PATT = re.compile(r"press:([01234567890\.]+)") LUX_PATT = re.compile(r"lux:([01234567890\.]+)") PATTERNS = {TEMP: TEMP_PATT, HUM: HUM_PATT, PRESS: PRESS_PATT, LUX: LUX_PATT} -UNITS = {TEMP: TEMP_CELSIUS, HUM: PERCENTAGE, PRESS: "mmHg", LUX: LIGHT_LUX} +UNITS = {TEMP: UnitOfTemperature.CELSIUS, HUM: PERCENTAGE, PRESS: "mmHg", LUX: LIGHT_LUX} CLASSES = { TEMP: SensorDeviceClass.TEMPERATURE, HUM: SensorDeviceClass.HUMIDITY, diff --git a/custom_components/mega/i2c.py b/custom_components/mega/i2c.py index 74ed705..10e2869 100644 --- a/custom_components/mega/i2c.py +++ b/custom_components/mega/i2c.py @@ -7,9 +7,9 @@ from homeassistant.components.sensor import SensorDeviceClass from homeassistant.const import ( PERCENTAGE, LIGHT_LUX, - TEMP_CELSIUS, CONCENTRATION_PARTS_PER_MILLION, - PRESSURE_BAR, + UnitOfTemperature, + UnitOfPressure ) from collections import namedtuple @@ -101,48 +101,48 @@ class Request: i2c_classes = { "htu21d": [ DeviceType(SensorDeviceClass.HUMIDITY, PERCENTAGE, None), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), ], "sht31": [ DeviceType(SensorDeviceClass.HUMIDITY, PERCENTAGE, None, delay=0.1), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), ], "max44009": [DeviceType(SensorDeviceClass.ILLUMINANCE, LIGHT_LUX, None)], "bh1750": [DeviceType(SensorDeviceClass.ILLUMINANCE, LIGHT_LUX, None)], "tsl2591": [DeviceType(SensorDeviceClass.ILLUMINANCE, LIGHT_LUX, None)], "bmp180": [ - DeviceType(SensorDeviceClass.PRESSURE, PRESSURE_BAR, None), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.PRESSURE, UnitOfPressure.BAR, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), ], "bmx280": [ - DeviceType(SensorDeviceClass.PRESSURE, PRESSURE_BAR, None), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.PRESSURE, UnitOfPressure.BAR, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), DeviceType(SensorDeviceClass.HUMIDITY, PERCENTAGE, None), ], "dps368": [ - DeviceType(SensorDeviceClass.PRESSURE, PRESSURE_BAR, None), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.PRESSURE, UnitOfPressure.BAR, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), ], "mlx90614": [ Skip, - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, "temp"), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, "object"), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, "temp"), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, "object"), ], "ptsensor": [ Skip, Request(delay=3), # запрос на измерение - DeviceType(SensorDeviceClass.PRESSURE, PRESSURE_BAR, None), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.PRESSURE, UnitOfPressure.BAR, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), ], "mcp9600": [ - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), # термопара + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), # термопара DeviceType( - SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None + SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None ), # сенсор встроенный в микросхему ], "t67xx": [DeviceType(SensorDeviceClass.CO2, CONCENTRATION_PARTS_PER_MILLION, None)], "tmp117": [ - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None), ], "ads1115": [ DeviceType(None, None, "ch0"), @@ -172,7 +172,7 @@ i2c_classes = { i2c_par=0, idx=0, ), - DeviceType(SensorDeviceClass.TEMPERATURE, TEMP_CELSIUS, None, i2c_par=0, idx=1), + DeviceType(SensorDeviceClass.TEMPERATURE, UnitOfTemperature.CELSIUS, None, i2c_par=0, idx=1), DeviceType(SensorDeviceClass.HUMIDITY, PERCENTAGE, None, i2c_par=0, idx=2), ], } diff --git a/custom_components/mega/light.py b/custom_components/mega/light.py index ddd5969..585053f 100644 --- a/custom_components/mega/light.py +++ b/custom_components/mega/light.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio import logging -import typing from datetime import timedelta, datetime from functools import partial @@ -13,13 +12,9 @@ import time from homeassistant.components.light import ( PLATFORM_SCHEMA as LIGHT_SCHEMA, - SUPPORT_BRIGHTNESS, LightEntity, - SUPPORT_TRANSITION, - SUPPORT_COLOR, ColorMode, - LightEntityFeature, - # SUPPORT_WHITE_VALUE + LightEntityFeature ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -124,10 +119,21 @@ async def async_setup_entry( class MegaLight(MegaOutPort, LightEntity): @property def supported_features(self): - return (SUPPORT_BRIGHTNESS if self.dimmer else 0) | ( - SUPPORT_TRANSITION if self.dimmer else 0 - ) + return LightEntityFeature.TRANSITION if self.dimmer else LightEntityFeature(0) + @property + def supported_color_modes(self): + if self.dimmer: + return {ColorMode.BRIGHTNESS} + else: + return {ColorMode.ONOFF} + + @property + def color_mode(self): + if self.dimmer: + return ColorMode.BRIGHTNESS + else: + return ColorMode.ONOFF class MegaRGBW(LightEntity, BaseMegaEntity): def __init__(self, *args, **kwargs): diff --git a/custom_components/mega/manifest.json b/custom_components/mega/manifest.json index ed6b204..1e5ea05 100644 --- a/custom_components/mega/manifest.json +++ b/custom_components/mega/manifest.json @@ -12,8 +12,9 @@ "homekit": {}, "after_dependencies": ["mqtt"], "codeowners": [ - "@andvikt" + "@andvikt", + "@den-dmitriev" ], "issue_tracker": "https://github.com/andvikt/mega_hacs/issues", - "version": "v1.1.8b12" + "version": "v1.1.8b13" } \ No newline at end of file diff --git a/install.sh b/install.sh index b31f408..6f14693 100644 --- a/install.sh +++ b/install.sh @@ -1,5 +1,5 @@ #!/bin/bash -git clone https://github.com/andvikt/mega_hacs.git +git clone https://github.com/den-dmitriev/mega_hacs.git mkdir custom_components cp mega_hacs/custom_components/mega custom_components/mega rm -fR mega_hacs From 73ee6c3397daa020b3fbfc602e0a9ab9c9dab3f9 Mon Sep 17 00:00:00 2001 From: "d.dmitriev" Date: Sun, 16 Feb 2025 15:14:33 +0500 Subject: [PATCH 2/3] fix config migration --- custom_components/mega/__init__.py | 9 +++------ custom_components/mega/config_flow.py | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/custom_components/mega/__init__.py b/custom_components/mega/__init__.py index 88960de..b9d10d1 100644 --- a/custom_components/mega/__init__.py +++ b/custom_components/mega/__init__.py @@ -254,10 +254,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): _hubs[entry.entry_id] = hub _subs[entry.entry_id] = entry.add_update_listener(updater) await hub.start() - for platform in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + # for platform in PLATFORMS: + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hub.updater.async_refresh() return True @@ -314,8 +312,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry): await hub.stop() new.update(cfg) _LOGGER.debug(f"new config: %s", new) - config_entry.data = new - config_entry.version = ConfigFlow.VERSION + hass.config_entries.async_update_entry(config_entry, data=new, version=ConfigFlow.VERSION) _LOGGER.info("Migration to version %s successful", config_entry.version) diff --git a/custom_components/mega/config_flow.py b/custom_components/mega/config_flow.py index 8816ba8..efc597f 100644 --- a/custom_components/mega/config_flow.py +++ b/custom_components/mega/config_flow.py @@ -130,7 +130,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow): cfg = dict(self.config_entry.data) cfg.update(user_input) cfg["new_naming"] = new_naming - self.config_entry.data = cfg + self.hass.config_entries.async_update_entry(entry=self.config_entry, data=cfg) await get_hub(self.hass, cfg) if reload: From 5acd5ad6923cfb04aadcc01ca77bafa2e9bef331 Mon Sep 17 00:00:00 2001 From: "d.dmitriev" Date: Sun, 16 Feb 2025 15:15:48 +0500 Subject: [PATCH 3/3] bump version --- .bumpversion.cfg | 2 +- custom_components/mega/hub.py | 2 +- custom_components/mega/manifest.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 94e2510..5d18490 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.1.8b12 +current_version = 1.1.8b14 parse = (?P\d+)(\.(?P\d+))(\.(?P\d+))(?P[bf]*)(?P\d*) commit = True tag = True diff --git a/custom_components/mega/hub.py b/custom_components/mega/hub.py index 200d87c..ad3d499 100644 --- a/custom_components/mega/hub.py +++ b/custom_components/mega/hub.py @@ -689,7 +689,7 @@ class MegaD: cfg.pop(x, None) cfg.update(new) self.lg.debug(f"new config: %s", cfg) - self.config.data = cfg + self.hass.config_entries.async_update_entry(self.config, data=cfg) if reload_entry: await self.hass.config_entries.async_reload(self.config.entry_id) return cfg diff --git a/custom_components/mega/manifest.json b/custom_components/mega/manifest.json index 1e5ea05..6d461ab 100644 --- a/custom_components/mega/manifest.json +++ b/custom_components/mega/manifest.json @@ -16,5 +16,5 @@ "@den-dmitriev" ], "issue_tracker": "https://github.com/andvikt/mega_hacs/issues", - "version": "v1.1.8b13" + "version": "v1.1.8b14" } \ No newline at end of file