first commit
This commit is contained in:
47
custom_components/strike/__init__.py
Normal file
47
custom_components/strike/__init__.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""The Strike Bitcoin integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Strike Bitcoin from a config entry."""
|
||||
_LOGGER.debug("Setting up Strike Bitcoin integration")
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
"session": async_get_clientsession(hass),
|
||||
"config": entry.data,
|
||||
}
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
_LOGGER.debug("Unloading Strike Bitcoin integration")
|
||||
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Reload config entry."""
|
||||
await async_unload_entry(hass, entry)
|
||||
await async_setup_entry(hass, entry)
|
||||
164
custom_components/strike/config_flow.py
Normal file
164
custom_components/strike/config_flow.py
Normal file
@ -0,0 +1,164 @@
|
||||
"""Config flow for Strike Bitcoin integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import (
|
||||
API_BASE_URL,
|
||||
API_TIMEOUT,
|
||||
CONF_API_KEY,
|
||||
CONF_CURRENCY_PAIR,
|
||||
DEFAULT_CURRENCY_PAIR,
|
||||
DEFAULT_NAME,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
TICKER_ENDPOINT,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def validate_api_key(hass: HomeAssistant, api_key: str, currency_pair: str) -> dict[str, Any]:
|
||||
"""Validate the API key by making a test request."""
|
||||
session = async_get_clientsession(hass)
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
}
|
||||
|
||||
try:
|
||||
async with session.get(
|
||||
f"{API_BASE_URL}{TICKER_ENDPOINT}",
|
||||
headers=headers,
|
||||
params={"sourceCurrency": currency_pair[:3], "targetCurrency": currency_pair[3:]},
|
||||
timeout=aiohttp.ClientTimeout(total=API_TIMEOUT),
|
||||
) as response:
|
||||
if response.status == 401:
|
||||
raise InvalidAuth
|
||||
if response.status == 403:
|
||||
raise InvalidAuth
|
||||
if response.status != 200:
|
||||
_LOGGER.error("API request failed with status %s", response.status)
|
||||
raise CannotConnect
|
||||
|
||||
data = await response.json()
|
||||
_LOGGER.debug("API validation successful: %s", data)
|
||||
return {"title": f"{DEFAULT_NAME} ({currency_pair})"}
|
||||
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Error connecting to Strike API: %s", err)
|
||||
raise CannotConnect from err
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected error validating API key: %s", err)
|
||||
raise CannotConnect from err
|
||||
|
||||
|
||||
class StrikeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Strike Bitcoin."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Handle the initial step."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
try:
|
||||
info = await validate_api_key(
|
||||
self.hass,
|
||||
user_input[CONF_API_KEY],
|
||||
user_input.get(CONF_CURRENCY_PAIR, DEFAULT_CURRENCY_PAIR),
|
||||
)
|
||||
|
||||
await self.async_set_unique_id(
|
||||
f"{user_input.get(CONF_CURRENCY_PAIR, DEFAULT_CURRENCY_PAIR)}"
|
||||
)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return self.async_create_entry(
|
||||
title=info["title"],
|
||||
data=user_input,
|
||||
)
|
||||
except InvalidAuth:
|
||||
errors["base"] = "invalid_auth"
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
|
||||
data_schema = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): str,
|
||||
vol.Optional(
|
||||
CONF_CURRENCY_PAIR, default=DEFAULT_CURRENCY_PAIR
|
||||
): str,
|
||||
vol.Optional(
|
||||
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
|
||||
): cv.positive_int,
|
||||
}
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=data_schema,
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(
|
||||
config_entry: config_entries.ConfigEntry,
|
||||
) -> StrikeOptionsFlowHandler:
|
||||
"""Get the options flow for this handler."""
|
||||
return StrikeOptionsFlowHandler(config_entry)
|
||||
|
||||
|
||||
class StrikeOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Handle Strike options."""
|
||||
|
||||
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
||||
"""Initialize options flow."""
|
||||
self.config_entry = config_entry
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Manage the options."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Optional(
|
||||
CONF_SCAN_INTERVAL,
|
||||
default=self.config_entry.data.get(
|
||||
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||
),
|
||||
): cv.positive_int,
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class CannotConnect(Exception):
|
||||
"""Error to indicate we cannot connect."""
|
||||
|
||||
|
||||
class InvalidAuth(Exception):
|
||||
"""Error to indicate there is invalid auth."""
|
||||
29
custom_components/strike/const.py
Normal file
29
custom_components/strike/const.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""Constants for the Strike Bitcoin integration."""
|
||||
from typing import Final
|
||||
|
||||
DOMAIN: Final = "strike"
|
||||
|
||||
# API Configuration
|
||||
API_BASE_URL: Final = "https://api.strike.me"
|
||||
API_TIMEOUT: Final = 10
|
||||
|
||||
# Strike API Endpoints
|
||||
TICKER_ENDPOINT: Final = "/v1/rates/ticker"
|
||||
|
||||
# Configuration
|
||||
CONF_API_KEY: Final = "api_key"
|
||||
CONF_CURRENCY_PAIR: Final = "currency_pair"
|
||||
|
||||
# Default values
|
||||
DEFAULT_NAME: Final = "Strike Bitcoin"
|
||||
DEFAULT_CURRENCY_PAIR: Final = "BTCUSD"
|
||||
DEFAULT_SCAN_INTERVAL: Final = 300 # 5 minutes
|
||||
|
||||
# Attributes
|
||||
ATTR_CURRENCY: Final = "currency"
|
||||
ATTR_LAST_UPDATE: Final = "last_update"
|
||||
ATTR_SOURCE: Final = "source"
|
||||
|
||||
# Device info
|
||||
MANUFACTURER: Final = "Strike"
|
||||
MODEL: Final = "Bitcoin Price Ticker"
|
||||
11
custom_components/strike/manifest.json
Normal file
11
custom_components/strike/manifest.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"domain": "strike",
|
||||
"name": "Strike Bitcoin",
|
||||
"codeowners": ["@martien"],
|
||||
"config_flow": true,
|
||||
"documentation": "https://github.com/martien/hass-to-be-good",
|
||||
"issue_tracker": "https://github.com/martien/hass-to-be-good/issues",
|
||||
"requirements": ["aiohttp>=3.8.0"],
|
||||
"version": "1.0.0",
|
||||
"iot_class": "cloud_polling"
|
||||
}
|
||||
181
custom_components/strike/sensor.py
Normal file
181
custom_components/strike/sensor.py
Normal file
@ -0,0 +1,181 @@
|
||||
"""Support for Strike Bitcoin price sensor."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
UpdateFailed,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
API_BASE_URL,
|
||||
API_TIMEOUT,
|
||||
ATTR_CURRENCY,
|
||||
ATTR_LAST_UPDATE,
|
||||
ATTR_SOURCE,
|
||||
CONF_API_KEY,
|
||||
CONF_CURRENCY_PAIR,
|
||||
DEFAULT_CURRENCY_PAIR,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
MODEL,
|
||||
TICKER_ENDPOINT,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Strike Bitcoin sensor based on a config entry."""
|
||||
coordinator = StrikeBitcoinCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
async_add_entities([StrikeBitcoinSensor(coordinator, entry)])
|
||||
|
||||
|
||||
class StrikeBitcoinCoordinator(DataUpdateCoordinator):
|
||||
"""Class to manage fetching Strike Bitcoin data."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
self.api_key = entry.data[CONF_API_KEY]
|
||||
self.currency_pair = entry.data.get(CONF_CURRENCY_PAIR, DEFAULT_CURRENCY_PAIR)
|
||||
self.session = hass.data[DOMAIN][entry.entry_id]["session"]
|
||||
|
||||
scan_interval = entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=DOMAIN,
|
||||
update_interval=timedelta(seconds=scan_interval),
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> dict[str, Any]:
|
||||
"""Fetch data from Strike API."""
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
}
|
||||
|
||||
# Parse currency pair (e.g., BTCUSD -> BTC and USD)
|
||||
source_currency = self.currency_pair[:3]
|
||||
target_currency = self.currency_pair[3:]
|
||||
|
||||
try:
|
||||
async with self.session.get(
|
||||
f"{API_BASE_URL}{TICKER_ENDPOINT}",
|
||||
headers=headers,
|
||||
params={
|
||||
"sourceCurrency": source_currency,
|
||||
"targetCurrency": target_currency,
|
||||
},
|
||||
timeout=aiohttp.ClientTimeout(total=API_TIMEOUT),
|
||||
) as response:
|
||||
if response.status == 401:
|
||||
raise UpdateFailed("Invalid API key")
|
||||
if response.status == 403:
|
||||
raise UpdateFailed("API key forbidden")
|
||||
if response.status != 200:
|
||||
raise UpdateFailed(f"API request failed with status {response.status}")
|
||||
|
||||
data = await response.json()
|
||||
|
||||
# Strike API returns the ticker data
|
||||
# Expected format: {"amount": "50000.00"}
|
||||
if "amount" not in data:
|
||||
raise UpdateFailed("Invalid response format from Strike API")
|
||||
|
||||
return {
|
||||
"price": float(data["amount"]),
|
||||
"currency": target_currency,
|
||||
"source_currency": source_currency,
|
||||
"last_update": datetime.now().isoformat(),
|
||||
}
|
||||
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Error connecting to Strike API: %s", err)
|
||||
raise UpdateFailed(f"Error connecting to Strike API: {err}") from err
|
||||
except Exception as err:
|
||||
_LOGGER.exception("Unexpected error fetching Strike data: %s", err)
|
||||
raise UpdateFailed(f"Unexpected error: {err}") from err
|
||||
|
||||
|
||||
class StrikeBitcoinSensor(CoordinatorEntity, SensorEntity):
|
||||
"""Representation of a Strike Bitcoin Price Sensor."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_name = None
|
||||
_attr_device_class = SensorDeviceClass.MONETARY
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
_attr_icon = "mdi:bitcoin"
|
||||
|
||||
def __init__(
|
||||
self, coordinator: StrikeBitcoinCoordinator, entry: ConfigEntry
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self._attr_unique_id = f"{entry.entry_id}_btc_price"
|
||||
self._entry_id = entry.entry_id
|
||||
self.currency_pair = entry.data.get(CONF_CURRENCY_PAIR, DEFAULT_CURRENCY_PAIR)
|
||||
|
||||
# Set device info
|
||||
self._attr_device_info = {
|
||||
"identifiers": {(DOMAIN, entry.entry_id)},
|
||||
"name": f"Strike {self.currency_pair}",
|
||||
"manufacturer": MANUFACTURER,
|
||||
"model": MODEL,
|
||||
"entry_type": "service",
|
||||
}
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return the state of the sensor."""
|
||||
if self.coordinator.data is None:
|
||||
return None
|
||||
return self.coordinator.data.get("price")
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self) -> str | None:
|
||||
"""Return the unit of measurement."""
|
||||
if self.coordinator.data is None:
|
||||
return None
|
||||
return self.coordinator.data.get("currency", "USD")
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return the state attributes."""
|
||||
if self.coordinator.data is None:
|
||||
return {}
|
||||
|
||||
return {
|
||||
ATTR_CURRENCY: self.coordinator.data.get("currency"),
|
||||
ATTR_SOURCE: f"{self.coordinator.data.get('source_currency')}/{self.coordinator.data.get('currency')}",
|
||||
ATTR_LAST_UPDATE: self.coordinator.data.get("last_update"),
|
||||
}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
return self.coordinator.last_update_success and self.coordinator.data is not None
|
||||
Reference in New Issue
Block a user