mullerdavid / hass_GreeExt

Gree Extension for built in integration
10 stars 1 forks source link

Exception when loading #3

Closed bithajcsar closed 1 year ago

bithajcsar commented 1 year ago

After updating HA core the configuration check produces a message: Component error: gree_ext - Exception importing custom_components.gree_ext The component cannot be loaded any more

Home Assistant 2023.6.0 Supervisor 2023.06.1 Operating System 10.2 Kezelőfelület 20230607.0 - latest

bithajcsar commented 1 year ago

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/loader.py", line 813, in get_component ComponentProtocol, importlib.import_module(self.pkg_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1206, in _gcd_import File "", line 1178, in _find_and_load File "", line 1149, in _find_and_load_unlocked File "", line 690, in _load_unlocked File "", line 940, in exec_module File "", line 241, in _call_with_frames_removed File "/config/custom_components/gree_ext/init.py", line 30, in @asyncio.coroutine ^^^^^^^^^^^^^^^^^ AttributeError: module 'asyncio' has no attribute 'coroutine'

mullerdavid commented 1 year ago

Currently not at home. I’ll try to replicate it and fix it next week. Probably just asyncio/python api change with the coroutines.

bithajcsar commented 1 year ago

I managed to correct it. Below is the correct init.py

""" The "Gree climate extension" custom component.

This component implements the different swing types not configurable with the built in climate.

Configuration:

To use the component you will need to add the following to your configuration.yaml file.

gree_ext: """

import asyncio import logging

from homeassistant.core import callback from homeassistant.helpers import area_registry as ar, device_registry as dr, entity_registry as er, entity_platform as ep from homeassistant.components.climate.const import DOMAIN as CLIMATE_DOMAIN from homeassistant.components.gree.const import DOMAIN as GREE_DOMAIN from homeassistant.components.gree.climate import GreeClimateEntity from greeclimate.device import HorizontalSwing, VerticalSwing

_LOGGER = logging.getLogger(name)

The domain of your component. Should be equal to the name of your component.

DOMAIN = "gree_ext"

async def async_setup(hass, config): """Setup our component."""

@callback
async def async_set_swing_mode_ext(call):
    """Set swing mode extended."""
    swing_mode_horizontal = call.data.get("swing_mode_horizontal", None)
    swing_mode_vertical = call.data.get("swing_mode_vertical", None)
    area_id = call.data.get("area_id", [])
    device_id = call.data.get("device_id", [])
    entity_id = call.data.get("entity_id", [])
    dev_reg = dr.async_get(hass)
    ent_reg = er.async_get(hass)
    for aid in area_id:
        for dev in dr.async_entries_for_area(dev_reg,aid):
            if dev.id not in device_id:
                device_id.append(dev.id)
    for did in device_id:
        for ent in er.async_entries_for_device(ent_reg,did):
            if ent.entity_id not in entity_id:
                entity_id.append(ent.entity_id)
    for platform in ep.async_get_platforms(hass, GREE_DOMAIN):
        if platform.domain == CLIMATE_DOMAIN:
            for eid, entity in platform.entities.items():
                if entity.entity_id in entity_id and isinstance(entity, GreeClimateEntity):
                    _LOGGER.warning( "Setting switng mode for entity (%s) to %s/%s", entity.entity_id, str(swing_mode_horizontal), str(swing_mode_vertical))
                    greeclimate = entity.coordinator.device
                    if swing_mode_horizontal:
                        try:
                            greeclimate.horizontal_swing = HorizontalSwing[swing_mode_horizontal]
                        except KeyError:
                            _LOGGER.debug("Invalid swing_mode_horizontal mode: %s", swing_mode_horizontal)
                    if swing_mode_vertical:
                        try:
                            greeclimate.vertical_swing = VerticalSwing[swing_mode_vertical]
                        except KeyError:
                            _LOGGER.debug("Invalid swing_mode_vertical mode: %s", swing_mode_vertical)
                    await greeclimate.push_state_update()
                    entity.async_write_ha_state()

hass.services.async_register(DOMAIN, 'set_swing_mode_ext', async_set_swing_mode_ext)

# Return boolean to indicate that initialization was successfully.
return True
kgergo1713 commented 1 year ago

I managed to correct it. Below is the correct init.py

""" The "Gree climate extension" custom component.

This component implements the different swing types not configurable with the built in climate.

Configuration:

To use the component you will need to add the following to your configuration.yaml file.

gree_ext: """

import asyncio import logging

from homeassistant.core import callback from homeassistant.helpers import area_registry as ar, device_registry as dr, entity_registry as er, entity_platform as ep from homeassistant.components.climate.const import DOMAIN as CLIMATE_DOMAIN from homeassistant.components.gree.const import DOMAIN as GREE_DOMAIN from homeassistant.components.gree.climate import GreeClimateEntity from greeclimate.device import HorizontalSwing, VerticalSwing

_LOGGER = logging.getLogger(name)

The domain of your component. Should be equal to the name of your component.

DOMAIN = "gree_ext"

async def async_setup(hass, config): """Setup our component."""

@callback
async def async_set_swing_mode_ext(call):
    """Set swing mode extended."""
    swing_mode_horizontal = call.data.get("swing_mode_horizontal", None)
    swing_mode_vertical = call.data.get("swing_mode_vertical", None)
    area_id = call.data.get("area_id", [])
    device_id = call.data.get("device_id", [])
    entity_id = call.data.get("entity_id", [])
    dev_reg = dr.async_get(hass)
    ent_reg = er.async_get(hass)
    for aid in area_id:
        for dev in dr.async_entries_for_area(dev_reg,aid):
            if dev.id not in device_id:
                device_id.append(dev.id)
    for did in device_id:
        for ent in er.async_entries_for_device(ent_reg,did):
            if ent.entity_id not in entity_id:
                entity_id.append(ent.entity_id)
    for platform in ep.async_get_platforms(hass, GREE_DOMAIN):
        if platform.domain == CLIMATE_DOMAIN:
            for eid, entity in platform.entities.items():
                if entity.entity_id in entity_id and isinstance(entity, GreeClimateEntity):
                    _LOGGER.warning( "Setting switng mode for entity (%s) to %s/%s", entity.entity_id, str(swing_mode_horizontal), str(swing_mode_vertical))
                    greeclimate = entity.coordinator.device
                    if swing_mode_horizontal:
                        try:
                            greeclimate.horizontal_swing = HorizontalSwing[swing_mode_horizontal]
                        except KeyError:
                            _LOGGER.debug("Invalid swing_mode_horizontal mode: %s", swing_mode_horizontal)
                    if swing_mode_vertical:
                        try:
                            greeclimate.vertical_swing = VerticalSwing[swing_mode_vertical]
                        except KeyError:
                            _LOGGER.debug("Invalid swing_mode_vertical mode: %s", swing_mode_vertical)
                    await greeclimate.push_state_update()
                    entity.async_write_ha_state()

hass.services.async_register(DOMAIN, 'set_swing_mode_ext', async_set_swing_mode_ext)

# Return boolean to indicate that initialization was successfully.
return True

I tried it, but unfortunately it doesn't work either...

mullerdavid commented 1 year ago

As I see they removed the legacy asyncio support and using the plain async modifier on the definition now. Replaced the asyncio corutine with new syntax. I think you modified the same thing, but the formatting is messed up here in the comment. It works for me on this: Home Assistant 2023.6.1 Supervisor 2023.06.1 Operating System 10.2 Frontend 20230608.0 - latest

kgergo1713 commented 1 year ago

As I see they removed the legacy asyncio support and using the plain async modifier on the definition now. Replaced the asyncio corutine with new syntax. I think you modified the same thing, but the formatting is messed up. It works for me on this: Home Assistant 2023.6.1 Supervisor 2023.06.1 Operating System 10.2 Frontend 20230608.0 - latest

My version: Home Assistant 2023.6.1 Supervisor 2023.06.1 Operating System 10.2 Kezelőfelület 20230608.0 - latest

Could you give me the right format?

mullerdavid commented 1 year ago

I changed only the python file. https://github.com/mullerdavid/hass_GreeExt/blob/master/custom_components/gree_ext/__init__.py Diff: https://github.com/mullerdavid/hass_GreeExt/commit/77915fa30ea1f5c7f7bc8eeb85424cf63a16a837