Skip to content

Commit

Permalink
Fix old Hass support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 18, 2024
1 parent 84f37e1 commit 61b950e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
11 changes: 2 additions & 9 deletions custom_components/xiaomi_gateway3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging

import voluptuous as vol
from homeassistant.components.system_log import CONF_LOGGER
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import MAJOR_VERSION, MINOR_VERSION, EVENT_HOMEASSISTANT_STOP
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import device_registry
Expand Down Expand Up @@ -37,6 +36,7 @@
CONF_DEVICES = "devices"
CONF_ATTRIBUTES_TEMPLATE = "attributes_template"
CONF_OPENMIIO = "openmiio"
CONF_LOGGER = "logger"

CONFIG_SCHEMA = vol.Schema(
{
Expand All @@ -53,13 +53,6 @@


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
# 2022.5.0 - AlarmControlPanelEntityFeature, ClimateEntityFeature, HVACMode
# 2022.11.0 - UnitOfEnergy, UnitOfLength, UnitOfPower, UnitOfTemperature
# 2023.1.0 - UnitOfElectricCurrent, UnitOfElectricPotential, UnitOfTime
if (MAJOR_VERSION, MINOR_VERSION) < (2023, 1):
_LOGGER.error("Minimum supported Hass version 2023.1")
return False

if config := config.get(DOMAIN):
if devices_config := config.get(CONF_DEVICES):
XDevice.configs = hass_utils.fix_yaml_devices_config(devices_config)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/xiaomi_gateway3/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import time
from datetime import datetime, UTC
from datetime import datetime, timezone
from functools import cached_property

from homeassistant.components.binary_sensor import BinarySensorEntity
Expand Down Expand Up @@ -85,7 +85,7 @@ def set_state(self, data: dict):
if self.clear_task:
self.clear_task.cancel()

utcnow = datetime.fromtimestamp(ts, UTC)
utcnow = datetime.fromtimestamp(ts, timezone.utc)

self._attr_is_on = True
self._attr_extra_state_attributes[ATTR_LAST_TRIGGERED] = utcnow
Expand Down
7 changes: 3 additions & 4 deletions custom_components/xiaomi_gateway3/hass/entity.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import logging
from datetime import datetime, UTC
from datetime import datetime, timezone
from functools import cached_property
from typing import TYPE_CHECKING, Callable

from homeassistant.helpers.device_registry import (
CONNECTION_BLUETOOTH,
CONNECTION_NETWORK_MAC,
CONNECTION_ZIGBEE,
DeviceInfo,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import ExtraStoredData, RestoredExtraData
from homeassistant.helpers.template import Template
Expand Down Expand Up @@ -188,7 +187,7 @@ def on_device_update(self, data: dict):
state_change = True

if ts := data.get(self.attr):
self._attr_native_value = datetime.fromtimestamp(ts, UTC)
self._attr_native_value = datetime.fromtimestamp(ts, timezone.utc)

if "msg_received" in self._attr_extra_state_attributes:
self._attr_extra_state_attributes["msg_received"] += 1
Expand Down
3 changes: 1 addition & 2 deletions custom_components/xiaomi_gateway3/hass/entity_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
LIGHT_LUX,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
EntityCategory,
UnitOfElectricPotential,
UnitOfElectricCurrent,
UnitOfEnergy,
Expand All @@ -21,7 +20,7 @@
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import Entity, EntityCategory

from ..core.converters.base import BaseConv

Expand Down
36 changes: 36 additions & 0 deletions tests/test_backward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from homeassistant.const import REQUIRED_PYTHON_VER

from custom_components.xiaomi_gateway3 import *
from custom_components.xiaomi_gateway3.alarm_control_panel import *
from custom_components.xiaomi_gateway3.binary_sensor import *
from custom_components.xiaomi_gateway3.climate import *
from custom_components.xiaomi_gateway3.config_flow import *
from custom_components.xiaomi_gateway3.cover import *
from custom_components.xiaomi_gateway3.device_trigger import *
from custom_components.xiaomi_gateway3.diagnostics import *
from custom_components.xiaomi_gateway3.light import *
from custom_components.xiaomi_gateway3.number import *
from custom_components.xiaomi_gateway3.select import *
from custom_components.xiaomi_gateway3.sensor import *
from custom_components.xiaomi_gateway3.switch import *
from custom_components.xiaomi_gateway3.text import *


def test_backward():
# https://github.com/home-assistant/core/blob/2023.2.0/homeassistant/const.py
assert REQUIRED_PYTHON_VER >= (3, 10, 0)

assert async_setup_entry, async_unload_entry
assert XAlarmControlPanel
assert XBinarySensor
assert XAqaraS2
assert FlowHandler
assert XCover
assert async_get_triggers
assert async_get_config_entry_diagnostics
assert XLight
assert XNumber
assert XSelect
assert XSensor
assert XSwitch
assert XText

0 comments on commit 61b950e

Please sign in to comment.