Created
December 21, 2018 21:50
-
-
Save kennedyshead/aa228a5fac96e0e47e5c8ce64228a0b0 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 homeassistant.helpers.entity import Entity | |
class kris: | |
URL = "http://api.krisinformation.se/v1/feed?format=json" | |
@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 kris.fetch(session, kris.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 kris.get_data() | |
print(data) | |
entities = [ | |
KrisSensor(data), | |
] | |
async_add_entities(entities) | |
class KrisSensor(Entity): | |
"""Representation of a kris scale sensor.""" | |
def __init__(self, data): | |
"""Init the sensor.""" | |
self._unit = "Warnings" | |
self._attributes = {} | |
self._state = "off" | |
if data: | |
self._state = "on" | |
self.filter_attributes(data) | |
self._name = "krisinformation" | |
@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 | |
@property | |
def state_attributes(self): | |
"""Property for the state attributes.""" | |
return self._attributes | |
def filter_attributes(self, data): | |
"""Filter out data for this Sensor.""" | |
self._attributes = { | |
"title": data["Entries"][0]["Title"], | |
"Author": data["Entries"][0]["Author"]["Name"], | |
"Published": data["Entries"][0]["Published"], | |
"Updated": data["Entries"][0]["Updated"], | |
"Location": data["Entries"][0]["CapArea"][0]["CapAreaDesc"], | |
"Summary": data["Entries"][0]["Summary"], | |
"latitude": data[ | |
"Entries" | |
][0]["CapArea"][0]["Coordinate"].split(',')[1].strip(" 0"), | |
"longitude": data[ | |
"Entries" | |
][0]["CapArea"][0]["Coordinate"].split(',')[0] | |
} | |
async def async_update(self): | |
"""Fetch new state data for the sensor.""" | |
data = await kris.get_data() | |
self._state = "off" | |
if data: | |
self._state = "on" | |
self.filter_attributes(data) |
why not include these components on https://github.com/custom-components
hit us up if you want em there 👍
Hm... Thing is that I develop these very fast and without warranty ;) If you want to add them to that repo I'm fine with that. But for me what I actually have time with atm. is just a quick blog-post and a gist paste.
But I might change my mind
And Ill probably add them as real components in the future, time given that is
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why not include these components on https://github.com/custom-components
hit us up if you want em there 👍