Skip to content

Commit

Permalink
Move tagline and header to sensor attributes (#7)
Browse files Browse the repository at this point in the history
* Limit sensors to init with taglines

* Tidy up

* Lint
  • Loading branch information
inverse authored Jan 3, 2025
1 parent e71ef0b commit 7708121
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
3 changes: 3 additions & 0 deletions custom_components/premium_bond_checker/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
CONF_HOLDER_NUMBER = "holder_number"


ATTR_HEADER = "header"
ATTR_TAGLINE = "tagline"

BOND_PERIODS = [
"this_month",
"last_six_months",
Expand Down
66 changes: 21 additions & 45 deletions custom_components/premium_bond_checker/sensor.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
"""Support for Premium Bond Checker sensors."""

import logging
from typing import Any

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from premium_bond_checker.client import Result

from .const import BOND_PERIODS, BOND_PERIODS_TO_NAME, CONF_HOLDER_NUMBER, DOMAIN
from .const import (
ATTR_HEADER,
ATTR_TAGLINE,
BOND_PERIODS,
BOND_PERIODS_TO_NAME,
CONF_HOLDER_NUMBER,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -33,16 +39,6 @@ async def async_setup_entry(
coordinator, config_entry.data[CONF_HOLDER_NUMBER], period
)
)
entities.append(
PremiumBondCheckerDetailSensor(
coordinator, config_entry.data[CONF_HOLDER_NUMBER], period, "header"
)
)
entities.append(
PremiumBondCheckerDetailSensor(
coordinator, config_entry.data[CONF_HOLDER_NUMBER], period, "tagline"
)
)

async_add_entities(entities)

Expand All @@ -61,11 +57,14 @@ def __init__(self, coordinator, holder_number: str, bond_period: str):
@property
def is_on(self) -> bool:
"""Return if won"""
data: Result = self.coordinator.data.results.get(self._bond_period, {})
_LOGGER.debug(f"Got {self.data.won} for {self.data.bond_period}")

_LOGGER.debug(f"Got {data.won} for {data.bond_period}")
return self.data.won

return data.won
@property
def data(self) -> Result:
"""Returns the result from the coordinator."""
return self.coordinator.data.results.get(self._bond_period, {})

@property
def name(self) -> str:
Expand All @@ -76,33 +75,10 @@ def name(self) -> str:
def unique_id(self) -> str:
return self._id


class PremiumBondCheckerDetailSensor(CoordinatorEntity, SensorEntity):
def __init__(
self, coordinator, holder_number: str, bond_period: str, detail_type: str
):
"""Initialize the sensor."""
super().__init__(coordinator)
self._data = coordinator
self._bond_period = bond_period
self._detail_type = detail_type
self._name = f"Premium Bond Checker {holder_number} {BOND_PERIODS_TO_NAME[bond_period]} {detail_type}"
self._id = f"premium_bond_checker-{holder_number}-{bond_period}-{detail_type}"

@property
def native_value(self) -> StateType:
"""Return the state"""
data: Result = self.coordinator.data.results.get(self._bond_period, {})

_LOGGER.debug(f"Got {getattr(data, self._detail_type)} for {data.bond_period}")

return getattr(data, self._detail_type)

@property
def name(self) -> str:
"""Return the name of the sensor."""
return self._name

@property
def unique_id(self) -> str:
return self._id
def extra_state_attributes(self) -> dict[str, Any]:
"""Return state attributes."""
return {
ATTR_HEADER: self.data.header,
ATTR_TAGLINE: self.data.tagline,
}

0 comments on commit 7708121

Please sign in to comment.