Last active
October 16, 2022 10:36
-
-
Save kennedyshead/50f46caff68016299449ceb0d867b329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from datetime import timedelta | |
from homeassistant.helpers.entity import Entity | |
from homeassistant.util import Throttle | |
MAPPING = { | |
'usd': 4, | |
'eur': 12, | |
'gbp': 16 | |
} | |
SCAN_INTERVAL = timedelta(minutes=30) | |
class Valutase: | |
URL = "https://api.valuta.se/api/sek/rates/" | |
@staticmethod | |
async def fetch(session, url): | |
import json | |
async with session.get(url) as response: | |
return json.loads(await response.text()) | |
@staticmethod | |
async def get_data(): | |
import aiohttp | |
async with aiohttp.ClientSession() as session: | |
html = await Valutase.fetch(session, Valutase.URL) | |
return html | |
_LOGGER = logging.getLogger(__name__) | |
async def async_setup_platform( | |
hass, config, async_add_entities, discovery_info=None): | |
"""Set up the asuswrt sensors.""" | |
data = await Valutase.get_data() | |
entities = [ | |
ValutaSensor(data, 'usd'), | |
ValutaSensor(data, 'eur'), | |
ValutaSensor(data, 'gbp') | |
] | |
async_add_entities(entities) | |
class ValutaSensor(Entity): | |
"""Representation of sensor.""" | |
def __init__(self, data, currency): | |
"""Init the sensor.""" | |
self._unit = "SEK" | |
self._attributes = {} | |
self._state = 0 | |
if data: | |
self._state = round(data[MAPPING[currency]]['value'] / 100, 2) | |
self._name = currency.upper() | |
self.i = 0 | |
self.currency = currency | |
@property | |
def unit_of_measurement(self): | |
"""Return the unit of measurement.""" | |
return self._unit | |
@property | |
def state(self): | |
"""Property for the state attributes.""" | |
return self._state | |
@property | |
def name(self): | |
"""Name property for sensor.""" | |
return self._name | |
@Throttle(SCAN_INTERVAL) | |
async def async_update(self): | |
"""Fetch new state data for the sensor.""" | |
data = await Valutase.get_data() | |
if data: | |
self._state = round( | |
data[MAPPING[self.currency]]['value'] / 100, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
Added folder: \config\custom_components\valutase
And there I put valutase.py as sensor.py.
Updated config.yaml with
#Valuta-Omvandlare
But at restart HA i get
The system cannot restart because the configuration is not valid: Platform error sensor.valutase - Integration 'valutase' not found.
Also tried to add modified init.py , manifest.json files. That didnt make any differens