mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Add kraken code review changes (#50683)
This commit is contained in:
parent
7f6b8bbd1e
commit
256a2de7ce
@ -34,7 +34,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
||||
kraken_data = KrakenData(hass, config_entry)
|
||||
await kraken_data.async_setup()
|
||||
hass.data[DOMAIN] = kraken_data
|
||||
config_entry.add_update_listener(async_options_updated)
|
||||
config_entry.async_on_unload(
|
||||
config_entry.add_update_listener(async_options_updated)
|
||||
)
|
||||
hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
@ -8,7 +8,6 @@ import voluptuous as vol
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .const import CONF_TRACKED_ASSET_PAIRS, DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
@ -75,7 +74,3 @@ class KrakenOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
}
|
||||
|
||||
return self.async_show_form(step_id="init", data_schema=vol.Schema(options))
|
||||
|
||||
|
||||
class AlreadyConfigured(HomeAssistantError):
|
||||
"""Error to indicate the asset pair is already configured."""
|
||||
|
@ -3,9 +3,10 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import async_entries_for_config_entry
|
||||
from homeassistant.helpers import device_registry
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
@ -24,24 +25,24 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Add kraken entities from a config_entry."""
|
||||
|
||||
@callback
|
||||
async def async_update_sensors(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
) -> None:
|
||||
device_registry = await hass.helpers.device_registry.async_get_registry()
|
||||
def async_update_sensors(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||
dev_reg = device_registry.async_get(hass)
|
||||
|
||||
existing_devices = {
|
||||
device.name: device.id
|
||||
for device in async_entries_for_config_entry(
|
||||
device_registry, config_entry.entry_id
|
||||
for device in device_registry.async_entries_for_config_entry(
|
||||
dev_reg, config_entry.entry_id
|
||||
)
|
||||
}
|
||||
|
||||
sensors = []
|
||||
for tracked_asset_pair in config_entry.options[CONF_TRACKED_ASSET_PAIRS]:
|
||||
# Only create new devices
|
||||
if create_device_name(tracked_asset_pair) in existing_devices:
|
||||
existing_devices.pop(create_device_name(tracked_asset_pair))
|
||||
if (
|
||||
device_name := create_device_name(tracked_asset_pair)
|
||||
) in existing_devices:
|
||||
existing_devices.pop(device_name)
|
||||
else:
|
||||
sensors = []
|
||||
for sensor_type in SENSOR_TYPES:
|
||||
sensors.append(
|
||||
KrakenSensor(
|
||||
@ -50,13 +51,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
sensor_type,
|
||||
)
|
||||
)
|
||||
async_add_entities(sensors, True)
|
||||
async_add_entities(sensors, True)
|
||||
|
||||
# Remove devices for asset pairs which are no longer tracked
|
||||
for device_id in existing_devices.values():
|
||||
device_registry.async_remove_device(device_id)
|
||||
dev_reg.async_remove_device(device_id)
|
||||
|
||||
await async_update_sensors(hass, config_entry)
|
||||
async_update_sensors(hass, config_entry)
|
||||
|
||||
hass.data[DOMAIN].unsub_listeners.append(
|
||||
async_dispatcher_connect(
|
||||
@ -67,7 +68,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
)
|
||||
|
||||
|
||||
class KrakenSensor(CoordinatorEntity):
|
||||
class KrakenSensor(CoordinatorEntity, SensorEntity):
|
||||
"""Define a Kraken sensor."""
|
||||
|
||||
def __init__(
|
||||
@ -125,7 +126,7 @@ class KrakenSensor(CoordinatorEntity):
|
||||
|
||||
def _handle_coordinator_update(self):
|
||||
self._update_internal_state()
|
||||
return super()._handle_coordinator_update()
|
||||
super()._handle_coordinator_update()
|
||||
|
||||
def _update_internal_state(self):
|
||||
try:
|
||||
@ -207,6 +208,7 @@ class KrakenSensor(CoordinatorEntity):
|
||||
"""Return the unit the value is expressed in."""
|
||||
if "number_of" not in self._sensor_type:
|
||||
return self._unit_of_measurement
|
||||
return None
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
@ -218,7 +220,7 @@ class KrakenSensor(CoordinatorEntity):
|
||||
"""Return a device description for device registry."""
|
||||
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self._source_asset, self._target_asset)},
|
||||
"identifiers": {(DOMAIN, f"{self._source_asset}_{self._target_asset}")},
|
||||
"name": self._device_name,
|
||||
"manufacturer": "Kraken.com",
|
||||
"entry_type": "service",
|
||||
|
@ -52,6 +52,7 @@ async def test_already_configured(hass):
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_options(hass):
|
||||
|
@ -3,7 +3,6 @@ from unittest.mock import patch
|
||||
|
||||
from pykrakenapi.pykrakenapi import CallRateLimitError, KrakenAPIError
|
||||
|
||||
from homeassistant.components import kraken
|
||||
from homeassistant.components.kraken.const import DOMAIN
|
||||
|
||||
from .const import TICKER_INFORMATION_RESPONSE, TRADEABLE_ASSET_PAIR_RESPONSE
|
||||
@ -25,7 +24,7 @@ async def test_unload_entry(hass):
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert await kraken.async_unload_entry(hass, entry)
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
assert DOMAIN not in hass.data
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user