Editing Existing Components Python Code

Hi Everyone,

This is my first post here because almost all questions I’ve had have been asked and answered already in this awesome community. I have some programming and linux experience but I am relatively new to home assistant (And know relatively nothing about jinja although I’m working on it).

So I have VeSync Etekcity Smart Outlets on my network. And I noticed the state values for the current energy usage was different than the VeSync App. I did a little investigation and I realized the python module the Hass Vesync component loads uses the average energy consumption over the active time of the device, which is completely off because if you switch the outlet on and off it divides the usage over the past 24 hours by 1, 2, 3, … minutes. Here is a snippet of the code:

def get_power(self, cid):
        """Return current power in watts of a device"""

        response = self.call_api('/v1/device/' + cid + '/detail', 'get', headers=self.get_headers())

        if response is not None and response:
            if 'energy' in response and response['energy']:
                if 'activeTime' in response and response['activeTime'] is not None:
                    watts = self.calculate_watts(response['energy'], response['activeTime'])

                    if watts is not None and watts > 0:
                        return watts
        
return 0

I experimented with the api and found the developer most likely did this because the energy and voltage is sent as two hexadecimal numbers by the API. For example: (voltage: 7b13e:7b1e0, energy: 810f9:7e22b). I figured out how to convert the hexadecimal to watts and volts with python. I am very tempted to edit the vesync python package in the site-packages directly but everything I read said it will break the component. Would changing a few lines to convert the hex number in python break the component? I already tried it in a separate environment and it works like a charm. Would it be better to use a template in YAML to convert hex to decimal and do the appropriate math? Or should I just create a custom component named vesync2 using the original site-package module and just performing the conversion in the new component file? Or is there a better option I am not seeing?

Thanks in advanced!

If you think this is a bug then lodge a pull request.

Forgive me if this is a stupid question. Should I do the pull request on the home assistant source where the module is located or the original git source of the python module?

That depends where the error is. Home assistant components generally consist of two parts, a generic python program that could be developed for general purposes, and the home assistant components that pulls in the general program and interfaces to ha core code.