Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency symbol as unit #135

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ logger:

yahoofinance:
show_trending_icon: true
show_currency_symbol_as_unit: true

#Interval too small - will throw an error
#scan_interval: 30
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ friendly_name: Ivy Science & Technology Fund C
include_two_hundred_day_values: false
```

- The currency symbol e.g. $ can be show as the unit instead of USD by setting `show_currency_symbol_as_unit: true`.
- **Note:** Using this setting will generate a warning like `The unit of this entity changed to '$' which can't be converted ...` You will have to manually resolve it by picking the first option to update the unit of the historicalvalues without convertion. This can be done from `Developer tools > STATISTICS`.


### Symbol

- An alternate target currency can be specified for a symbol using the extended declaration format. Here, the symbol EMIM.L is reported in USD but will be presented in EUR. The conversion would be based on the value of the symbol USDEUR=X.
Expand Down
18 changes: 12 additions & 6 deletions custom_components/yahoofinance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CONF_INCLUDE_PRE_VALUES,
CONF_INCLUDE_TWO_HUNDRED_DAY_VALUES,
CONF_NO_UNIT,
CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT,
CONF_SHOW_TRENDING_ICON,
CONF_SYMBOLS,
CONF_TARGET_CURRENCY,
Expand All @@ -35,6 +36,7 @@
DEFAULT_CONF_INCLUDE_PRE_VALUES,
DEFAULT_CONF_INCLUDE_TWO_HUNDRED_DAY_VALUES,
DEFAULT_CONF_NO_UNIT,
DEFAULT_CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT,
DEFAULT_CONF_SHOW_TRENDING_ICON,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
Expand Down Expand Up @@ -88,6 +90,10 @@ def minimum_scan_interval(value: timedelta) -> timedelta:
vol.Optional(
CONF_SHOW_TRENDING_ICON, default=DEFAULT_CONF_SHOW_TRENDING_ICON
): cv.boolean,
vol.Optional(
CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT,
default=DEFAULT_CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT,
): cv.boolean,
vol.Optional(
CONF_DECIMAL_PLACES, default=DEFAULT_CONF_DECIMAL_PLACES
): vol.Coerce(int),
Expand Down Expand Up @@ -134,12 +140,12 @@ def __init__(self, symbol: str, **kwargs: any) -> None:
"""
self.symbol = symbol

if "target_currency" in kwargs:
self.target_currency = kwargs["target_currency"]
if "scan_interval" in kwargs:
self.scan_interval = kwargs["scan_interval"]
if "no_unit" in kwargs:
self.no_unit = kwargs["no_unit"]
if CONF_TARGET_CURRENCY in kwargs:
self.target_currency = kwargs[CONF_TARGET_CURRENCY]
if CONF_SCAN_INTERVAL in kwargs:
self.scan_interval = kwargs[CONF_SCAN_INTERVAL]
if CONF_NO_UNIT in kwargs:
self.no_unit = kwargs[CONF_NO_UNIT]

def __repr__(self) -> str:
"""Return the representation."""
Expand Down
14 changes: 13 additions & 1 deletion custom_components/yahoofinance/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
CONF_INCLUDE_TWO_HUNDRED_DAY_VALUES: Final = "include_two_hundred_day_values"
CONF_INCLUDE_FIFTY_TWO_WEEK_VALUES: Final = "include_fifty_two_week_values"
CONF_SHOW_TRENDING_ICON: Final = "show_trending_icon"
CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT = "show_currency_symbol_as_unit"
CONF_TARGET_CURRENCY: Final = "target_currency"
CONF_NO_UNIT: Final = "no_unit"

Expand All @@ -57,6 +58,7 @@
DEFAULT_CONF_INCLUDE_TWO_HUNDRED_DAY_VALUES: Final = True
DEFAULT_CONF_INCLUDE_FIFTY_TWO_WEEK_VALUES: Final = True
DEFAULT_CONF_SHOW_TRENDING_ICON: Final = False
DEFAULT_CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT: Final = False
DEFAULT_CONF_NO_UNIT: Final = False

DEFAULT_NUMERIC_DATA_GROUP: Final = "default"
Expand Down Expand Up @@ -176,22 +178,32 @@
"bdt": "৳",
"brl": "R$",
"btc": "₿",
"cad": "CA$",
"chf": "₣",
"cny": "¥",
"eth": "Ξ",
"eur": "€",
"gbp": "£",
"hkd": "HK$",
"ils": "₪",
"inr": "₹",
"jpy": "¥",
"krw": "₩",
"kzt": "лв",
"mxn": "MX$",
"ngn": "₦",
"nzd": "NZ$",
"php": "₱",
"rial": "﷼",
"rub": "₽",
"sign": "",
"try": "₺",
"twd": "$",
"twd": "NT$",
"usd": "$",
"vnd": "₫",
"xaf": "FCFA",
"xcd": "EC$",
"xof": "F\u202fCFA",
"xpf": "CFPF",
"xxx": "¤",
}
49 changes: 33 additions & 16 deletions custom_components/yahoofinance/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ATTR_TRENDING,
ATTRIBUTION,
CONF_DECIMAL_PLACES,
CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT,
CONF_SHOW_TRENDING_ICON,
CONF_SYMBOLS,
CURRENCY_CODES,
Expand Down Expand Up @@ -124,6 +125,9 @@ def __init__(
symbol = symbol_definition.symbol
self._symbol = symbol
self._show_trending_icon = domain_config[CONF_SHOW_TRENDING_ICON]
self._show_currency_symbol_as_unit = domain_config[
CONF_SHOW_CURRENCY_SYMBOL_AS_UNIT
]
self._decimal_places = domain_config[CONF_DECIMAL_PLACES]
self._previous_close = None
self._target_currency = symbol_definition.target_currency
Expand Down Expand Up @@ -181,7 +185,9 @@ def convert_timestamp_to_datetime(date_timestamp, return_format) -> str | None:
if date_timestamp is None or date_timestamp == 0:
return date_timestamp

converted_date = datetime.fromtimestamp(date_timestamp,tz=dt_util.DEFAULT_TIME_ZONE)
converted_date = datetime.fromtimestamp(
date_timestamp, tz=dt_util.DEFAULT_TIME_ZONE
)
if return_format == "date":
converted_date = converted_date.date()

Expand Down Expand Up @@ -211,9 +217,12 @@ def native_unit_of_measurement(self) -> str | None:
if self._no_unit:
return None

if self._target_currency:
return self._target_currency
return self._currency
currency = self._target_currency if self._target_currency else self._currency

if self._show_currency_symbol_as_unit:
return CURRENCY_CODES.get(currency.lower(), currency)

return currency

@property
def icon(self) -> str:
Expand Down Expand Up @@ -377,21 +386,29 @@ def update_properties(self) -> None:
DATA_MARKET_STATE
]

self._attr_extra_state_attributes[
ATTR_DIVIDEND_DATE
] = self.convert_timestamp_to_datetime(symbol_data.get(DATA_DIVIDEND_DATE),'date')
self._attr_extra_state_attributes[ATTR_DIVIDEND_DATE] = (
self.convert_timestamp_to_datetime(
symbol_data.get(DATA_DIVIDEND_DATE), "date"
)
)

self._attr_extra_state_attributes[
ATTR_REGULAR_MARKET_TIME
] = self.convert_timestamp_to_datetime(symbol_data.get(DATA_REGULAR_MARKET_TIME),'dateTime')
self._attr_extra_state_attributes[ATTR_REGULAR_MARKET_TIME] = (
self.convert_timestamp_to_datetime(
symbol_data.get(DATA_REGULAR_MARKET_TIME), "dateTime"
)
)

self._attr_extra_state_attributes[
ATTR_POST_MARKET_TIME
] = self.convert_timestamp_to_datetime(symbol_data.get(DATA_POST_MARKET_TIME),'dateTime')
self._attr_extra_state_attributes[ATTR_POST_MARKET_TIME] = (
self.convert_timestamp_to_datetime(
symbol_data.get(DATA_POST_MARKET_TIME), "dateTime"
)
)

self._attr_extra_state_attributes[
ATTR_PRE_MARKET_TIME
] = self.convert_timestamp_to_datetime(symbol_data.get(DATA_PRE_MARKET_TIME),'dateTime')
self._attr_extra_state_attributes[ATTR_PRE_MARKET_TIME] = (
self.convert_timestamp_to_datetime(
symbol_data.get(DATA_PRE_MARKET_TIME), "dateTime"
)
)

# Use target_currency if we have conversion data. Otherwise keep using the
# currency from data.
Expand Down
Loading
Loading