Skip to content

Instantly share code, notes, and snippets.

@kennedyshead
Created December 21, 2018 21:50
Show Gist options
  • Save kennedyshead/aa228a5fac96e0e47e5c8ce64228a0b0 to your computer and use it in GitHub Desktop.
Save kennedyshead/aa228a5fac96e0e47e5c8ce64228a0b0 to your computer and use it in GitHub Desktop.
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)
@swetoast
Copy link

swetoast commented Jan 8, 2019

why not include these components on https://github.com/custom-components

hit us up if you want em there 👍

@kennedyshead
Copy link
Author

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

@kennedyshead
Copy link
Author

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