<commit_before>"""Support for Tibber sensors."""
import asyncio
from datetime import timedelta
import logging
from random import randrange

import aiohttp

from homeassistant.components.sensor import (
    DEVICE_CLASS_CURRENT,
    DEVICE_CLASS_ENERGY,
    DEVICE_CLASS_POWER,
    DEVICE_CLASS_POWER_FACTOR,
    DEVICE_CLASS_SIGNAL_STRENGTH,
    DEVICE_CLASS_VOLTAGE,
    STATE_CLASS_MEASUREMENT,
    SensorEntity,
)
from homeassistant.const import (
    ELECTRICAL_CURRENT_AMPERE,
    ENERGY_KILO_WATT_HOUR,
    PERCENTAGE,
    POWER_WATT,
    SIGNAL_STRENGTH_DECIBELS,
    VOLT,
)
from homeassistant.core import callback
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
from homeassistant.helpers.dispatcher import (
    async_dispatcher_connect,
    async_dispatcher_send,
)
from homeassistant.helpers.entity_registry import async_get as async_get_entity_reg
from homeassistant.util import Throttle, dt as dt_util

from .const import DOMAIN as TIBBER_DOMAIN, MANUFACTURER

_LOGGER = logging.getLogger(__name__)

ICON = "mdi:currency-usd"
SCAN_INTERVAL = timedelta(minutes=1)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
PARALLEL_UPDATES = 0
SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}"

RT_SENSOR_MAP = {
    "averagePower": ["average power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "power": ["power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "powerProduction": ["power production", DEVICE_CLASS_POWER, POWER_WATT, None],
    "minPower": ["min power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "maxPower": ["max power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "accumulatedConsumption": [
        "accumulated consumption",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedConsumptionLastHour": [
        "accumulated consumption current hour",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedProduction": [
        "accumulated production",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedProductionLastHour": [
        "accumulated production current hour",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "lastMeterConsumption": [
        "last meter consumption",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "lastMeterProduction": [
        "last meter production",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "voltagePhase1": [
        "voltage phase1",
        DEVICE_CLASS_VOLTAGE,
        VOLT,
        STATE_CLASS_MEASUREMENT,
    ],
    "voltagePhase2": [
        "voltage phase2",
        DEVICE_CLASS_VOLTAGE,
        VOLT,
        STATE_CLASS_MEASUREMENT,
    ],
    "voltagePhase3": [
        "voltage phase3",
        DEVICE_CLASS_VOLTAGE,
        VOLT,
        STATE_CLASS_MEASUREMENT,
    ],
    "currentL1": [
        "current L1",
        DEVICE_CLASS_CURRENT,
        ELECTRICAL_CURRENT_AMPERE,
        STATE_CLASS_MEASUREMENT,
    ],
    "currentL2": [
        "current L2",
        DEVICE_CLASS_CURRENT,
        ELECTRICAL_CURRENT_AMPERE,
        STATE_CLASS_MEASUREMENT,
    ],
    "currentL3": [
        "current L3",
        DEVICE_CLASS_CURRENT,
        ELECTRICAL_CURRENT_AMPERE,
        STATE_CLASS_MEASUREMENT,
    ],
    "signalStrength": [
        "signal strength",
        DEVICE_CLASS_SIGNAL_STRENGTH,
        SIGNAL_STRENGTH_DECIBELS,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedCost": ["accumulated cost", None, None, STATE_CLASS_MEASUREMENT],
    "powerFactor": [
        "power factor",
        DEVICE_CLASS_POWER_FACTOR,
        PERCENTAGE,
        STATE_CLASS_MEASUREMENT,
    ],
}


async def async_setup_entry(hass, entry, async_add_entities):
    """Set up the Tibber sensor."""

    tibber_connection = hass.data.get(TIBBER_DOMAIN)

    entity_registry = async_get_entity_reg(hass)
    device_registry = async_get_dev_reg(hass)

    entities = []
    for home in tibber_connection.get_homes(only_active=False):
        try:<commit_msg>Tibber, add device class monetary to accumulated cost (#52259)<commit_after>"""Support for Tibber sensors."""
import asyncio
from datetime import timedelta
import logging
from random import randrange

import aiohttp

from homeassistant.components.sensor import (
    DEVICE_CLASS_CURRENT,
    DEVICE_CLASS_ENERGY,
    DEVICE_CLASS_MONETARY,
    DEVICE_CLASS_POWER,
    DEVICE_CLASS_POWER_FACTOR,
    DEVICE_CLASS_SIGNAL_STRENGTH,
    DEVICE_CLASS_VOLTAGE,
    STATE_CLASS_MEASUREMENT,
    SensorEntity,
)
from homeassistant.const import (
    ELECTRICAL_CURRENT_AMPERE,
    ENERGY_KILO_WATT_HOUR,
    PERCENTAGE,
    POWER_WATT,
    SIGNAL_STRENGTH_DECIBELS,
    VOLT,
)
from homeassistant.core import callback
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
from homeassistant.helpers.dispatcher import (
    async_dispatcher_connect,
    async_dispatcher_send,
)
from homeassistant.helpers.entity_registry import async_get as async_get_entity_reg
from homeassistant.util import Throttle, dt as dt_util

from .const import DOMAIN as TIBBER_DOMAIN, MANUFACTURER

_LOGGER = logging.getLogger(__name__)

ICON = "mdi:currency-usd"
SCAN_INTERVAL = timedelta(minutes=1)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
PARALLEL_UPDATES = 0
SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}"

RT_SENSOR_MAP = {
    "averagePower": ["average power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "power": ["power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "powerProduction": ["power production", DEVICE_CLASS_POWER, POWER_WATT, None],
    "minPower": ["min power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "maxPower": ["max power", DEVICE_CLASS_POWER, POWER_WATT, None],
    "accumulatedConsumption": [
        "accumulated consumption",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedConsumptionLastHour": [
        "accumulated consumption current hour",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedProduction": [
        "accumulated production",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedProductionLastHour": [
        "accumulated production current hour",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "lastMeterConsumption": [
        "last meter consumption",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "lastMeterProduction": [
        "last meter production",
        DEVICE_CLASS_ENERGY,
        ENERGY_KILO_WATT_HOUR,
        STATE_CLASS_MEASUREMENT,
    ],
    "voltagePhase1": [
        "voltage phase1",
        DEVICE_CLASS_VOLTAGE,
        VOLT,
        STATE_CLASS_MEASUREMENT,
    ],
    "voltagePhase2": [
        "voltage phase2",
        DEVICE_CLASS_VOLTAGE,
        VOLT,
        STATE_CLASS_MEASUREMENT,
    ],
    "voltagePhase3": [
        "voltage phase3",
        DEVICE_CLASS_VOLTAGE,
        VOLT,
        STATE_CLASS_MEASUREMENT,
    ],
    "currentL1": [
        "current L1",
        DEVICE_CLASS_CURRENT,
        ELECTRICAL_CURRENT_AMPERE,
        STATE_CLASS_MEASUREMENT,
    ],
    "currentL2": [
        "current L2",
        DEVICE_CLASS_CURRENT,
        ELECTRICAL_CURRENT_AMPERE,
        STATE_CLASS_MEASUREMENT,
    ],
    "currentL3": [
        "current L3",
        DEVICE_CLASS_CURRENT,
        ELECTRICAL_CURRENT_AMPERE,
        STATE_CLASS_MEASUREMENT,
    ],
    "signalStrength": [
        "signal strength",
        DEVICE_CLASS_SIGNAL_STRENGTH,
        SIGNAL_STRENGTH_DECIBELS,
        STATE_CLASS_MEASUREMENT,
    ],
    "accumulatedCost": [
        "accumulated cost",
        DEVICE_CLASS_MONETARY,
        None,
        STATE_CLASS_MEASUREMENT,
    ],
    "powerFactor": [
        "power factor",
        DEVICE_CLASS_POWER_FACTOR,
        PERCENTAGE,
        STATE_CLASS_MEASUREMENT,
    ],
}


async def async_setup_entry(hass, entry, async_add_entities):
    """Set up the Tibber sensor."""

    tibber_connection = hass.data.get(TIBBER_DOMAIN)

    entity_registry = async_get_entity_reg(hass)
    device_registry = async_get_dev_reg(hass)

    entities = []
    for home in tibber_connection.get_homes(only_active=False):
        try:
{'fraction_non_alphanumeric': 0.07887453874538745, 'fraction_numerical': 0.004035977859778598, 'mean_word_length': 3.2473065621939274, 'pattern_counts': {'":': 40, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4727355', 'n_tokens_mistral': 3113, 'n_tokens_neox': 2913, 'n_words': 482}