tolwi / hassio-ecoflow-cloud

EcoFlow Cloud Integration for Home Assistant
281 stars 47 forks source link

Developer Documentation / Developer experience #248

Open paule96 opened 3 weeks ago

paule96 commented 3 weeks ago

Hi @tolwi,

I currently trying to add powerkits to the integration. Python is for me a language that I don't use so often. So for me, it would be beneficial to have much better documentation on how to add a new device and how to debug the code and try it out, so that I can see the results upfront in my Python development environment.

currently I tried to write myself a small script to test if my code works:

import asyncio
import logging
import os
from types import MappingProxyType

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from custom_components.ecoflow_cloud.devices.powerkit import PowerKit
from custom_components.ecoflow_cloud.entities import BaseSensorEntity
from custom_components.ecoflow_cloud import EcoflowAuthentication, EcoflowMQTTClient

logging.basicConfig(level=logging.INFO)
_LOGGER = logging.getLogger(__name__)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

async def main():

    data = MappingProxyType(
        {
            "type": "powerkit",
            "device_id": os.environ['ecoflowSn']
        }
    )

    config = ConfigEntry(
        version='1.0.0',
        minor_version='1',
        domain='homeassistant.local',
        title='debugEcoflow',
        data=data,
        source='',
        options= MappingProxyType(
        {
            "refresh_period_sec": 1
        })
    )
    auth = EcoflowAuthentication(os.environ['ecoflowUserName'], os.environ['ecoflowPassword'])
    auth.authorize()
    home = HomeAssistant('./')
    client = EcoflowMQTTClient(home, config, auth)
    powerkit = PowerKit()
    sensors = powerkit.sensors(client)
    client.data.params_observable().subscribe(lambda v: updateSensors(v))
    def updateSensors(data: dict[str, any]):
        for v in sensors:
            v.hass = home
            v._updated(data)
            _LOGGER.info(v.name)
            _LOGGER.info(v._attr_native_value)

    while True:
            await asyncio.sleep(1)
asyncio.run(main())

The problem I figured out right now, is that everything relies on the home assistant SDK. For me, this seems to complicate things if you want to check if your class gets the expected values from the ecoflow API.

Maybe it would be nice to have some guide to how you can debug this project. In special the data retrival part is a mystery to me.

Nid01 commented 3 weeks ago

@paule96 Can you confirm, that you have read the documentation of Home Assistant and have set up the described development environment?

paule96 commented 3 weeks ago

@Nid01 actually no, because it's not documented in this repository todo so. But I see they recommend to setup a Dev container.

I will try my best today to integrate this dev container into this repository.

Yesterday was quite hard to get used to the code overall because there is zero documentation in the whole code base too. That makes it incredibly hard to understand what is happening where.

Nid01 commented 3 weeks ago

@paule96 I don't know how much the development of a "HACS" integration differs from a integration directly for home assistant, but I recommend setting up the dev container. When that is up and running you can install HACS and the integration and start adding the power kits from that point.

paule96 commented 3 weeks ago

Yes I will try to make it easy for the next developer. So I will see if I can create a preconfigured Dev container in this repository that just works.

And all what a developer needs todo is to hit F5 and things a running. Without the need to configure homeassistant first. That should be kinda the goal. I mean of course the hardest part will be to give the developer the chance to provide his ecoflow credentials.