emhass icon indicating copy to clipboard operation
emhass copied to clipboard

Use time_zone (or new param) to calculate local currency symbol

Open GeoDerp opened this issue 1 year ago • 7 comments

https://github.com/davidusb-geek/emhass/blob/3dfcf1b4032fc0677b489049a98e93581218b44a/src/emhass/utils.py#L140-L141

I could see this having multiple solutions.

  • new pram that just has the currency sign
    • currency="$"
  • pass the time_zone param using an python library
  • new param that overrides locale country code, otherwise let local guess
.
.
.
import locale

if CountryCode != "":
   locale.setlocale(locale.LC_ALL, CountryCode)
else:
   locale.setlocale(locale.LC_ALL, '')

currency_symbol = locale.localeconv()["int_curr_symbol"] 
.
.
.
'custom_unit_load_cost_id': {"entity_id": "sensor.unit_load_cost", "unit_of_measurement": "currency_symbol", "friendly_name": "Unit Load Cost"}, 
'custom_unit_prod_price_id': {"entity_id": "sensor.unit_prod_price", "unit_of_measurement": "currency_symbol", "friendly_name": "Unit Prod Price"},
.
.
.

GeoDerp avatar Jan 25 '24 04:01 GeoDerp

What is the problem with the existent? There is a method to pass your own custom names, currency, etc

davidusb-geek avatar Jan 25 '24 06:01 davidusb-geek

ah my bad.

GeoDerp avatar Jan 25 '24 06:01 GeoDerp

Yes no problem. Those lines that you posted are just there as default values but the user can change to whatever they want.

davidusb-geek avatar Jan 25 '24 09:01 davidusb-geek

@davidusb-geek is the "currency" homeassistant variable being passed in. Could that be used as default?

GeoDerp avatar Jan 27 '24 08:01 GeoDerp

No, currency is not being fetched from HA. It could be interesting to fetch it directly yes.

davidusb-geek avatar Jan 27 '24 09:01 davidusb-geek

A temporary stop gap could be another param (for addon users):

web_server.py

...
        params['retrieve_hass_conf']['alt'] = options['Altitude']
        params['retrieve_hass_conf']['unit_of_measurement']  =  options['Currency']
        params['optim_conf']['set_use_battery'] = options['set_use_battery']
...        

utils.py

...
if 'custom_unit_load_cost_id' in runtimeparams.keys():
            params['passed_data']['custom_unit_load_cost_id'] = runtimeparams['custom_unit_load_cost_id']
        elif 'unit_of_measurement' in params['retrieve_hass_conf']:
            params['passed_data']['custom_unit_load_cost_id']['unit_of_measurement'] = str(params['retrieve_hass_conf']['unit_of_measurement']+"/kWh") 
        if 'custom_unit_prod_price_id' in runtimeparams.keys():
            params['passed_data']['custom_unit_prod_price_id'] = runtimeparams['custom_unit_prod_price_id']
        elif 'unit_of_measurement' in params['retrieve_hass_conf']:
            params['passed_data']['custom_unit_prod_price_id']['unit_of_measurement'] = str(params['retrieve_hass_conf']['unit_of_measurement']+"/kWh")
...

GeoDerp avatar Jan 27 '24 12:01 GeoDerp

To implement this, there should not be any stop gap for add-on users or any other installation method. Actually if we do:

url = self.hass_url+"api/config"
response = get(url, headers=headers)
response.json()['currency']

In my case I obtain 'EUR'. So we actually have access to all the configuration parameters directly retrieved from HA.

davidusb-geek avatar Mar 11 '24 21:03 davidusb-geek