Skip to content
Open
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 custom_components/yahoofinance/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
BASE: Final = "https://query1.finance.yahoo.com/v7/finance/quote?symbols="

INITIAL_URL: Final = "https://finance.yahoo.com/quote/NQ%3DF/"
YAHOO_QUOTE_URL: Final = "https://finance.yahoo.com/quote/"
CONSENT_HOST: Final = "consent.yahoo.com"
GET_CRUMB_URL: Final = "https://query2.finance.yahoo.com/v1/test/getcrumb"

Expand Down
16 changes: 15 additions & 1 deletion custom_components/yahoofinance/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
)
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo, async_generate_entity_id
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -54,6 +55,7 @@
NUMERIC_DATA_GROUPS,
PERCENTAGE_DATA_KEYS_NEEDING_MULTIPLICATION,
TIME_PRICE_DATA_DICT,
YAHOO_QUOTE_URL,
)
from .coordinator import YahooSymbolUpdateCoordinator
from .dataclasses import SymbolDefinition
Expand Down Expand Up @@ -199,6 +201,18 @@ def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id

@property
def device_info(self) -> DeviceInfo:
"""Return device registry information."""
return DeviceInfo(
configuration_url=f"{YAHOO_QUOTE_URL}{self._symbol}",
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, self._symbol)},
manufacturer="Yahoo",
model=self._short_name or self._symbol,
name=f"Yahoo Finance {self.name}",
)

@property
def name(self) -> str:
"""Return the name of the sensor."""
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Tests for Yahoo Finance component."""

pytest_plugins = ("pytest_homeassistant_custom_component",)

TEST_SYMBOL = "BABA"
TEST_CRUMB = "!123"
24 changes: 24 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
HASS_DATA_CONFIG,
HASS_DATA_COORDINATORS,
NUMERIC_DATA_GROUPS,
YAHOO_QUOTE_URL,
)
from custom_components.yahoofinance.sensor import (
YahooFinanceSensor,
Expand All @@ -56,6 +57,7 @@
from homeassistant.const import CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceEntryType

from . import TEST_SYMBOL

Expand Down Expand Up @@ -337,6 +339,28 @@ async def test_sensor_update_calls_coordinator(hass: HomeAssistant) -> None:
assert mock_coordinator.async_request_refresh.call_count == 1


def test_device_info_exposes_symbol_details() -> None:
"""Ensure each entity registers a per-symbol device."""

mock_hass = MagicMock()
mock_hass.states.async_available.return_value = True

symbol = "XYZ"
mock_coordinator = build_mock_coordinator(mock_hass, True, symbol, 12)
sensor = YahooFinanceSensor(
mock_hass, mock_coordinator, SymbolDefinition(symbol), DEFAULT_OPTIONAL_CONFIG
)

sensor.update_properties()
info = sensor.device_info

assert info["entry_type"] is DeviceEntryType.SERVICE
assert info["identifiers"] == {(DOMAIN, symbol)}
assert info["configuration_url"] == f"{YAHOO_QUOTE_URL}{symbol}"
assert info["manufacturer"] == "Yahoo"
assert info["name"] == "Yahoo Finance Symbol XYZ Long"


@pytest.mark.parametrize(
("market_price", "previous_close", "show_trending", "expected_trend"),
[
Expand Down