Skip to content

Instantly share code, notes, and snippets.

@kennedyshead
Last active February 17, 2023 16:05
Show Gist options
  • Save kennedyshead/180aaffc17a76b195451f364e38e4f07 to your computer and use it in GitHub Desktop.
Save kennedyshead/180aaffc17a76b195451f364e38e4f07 to your computer and use it in GitHub Desktop.
import logging
from homeassistant.helpers.entity import Entity
class nooa:
URL = "https://services.swpc.noaa.gov/products/noaa-scales.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 nooa.fetch(session, nooa.URL)
return html
_LOGGER = logging.getLogger(__name__)
STATIC_DATA = {
"S": ("Solar Radiation Storms",),
"R": ("Radio Blackouts",),
"G": ("Geomagnetic Storms",),
}
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Set up the sensors."""
data = await nooa.get_data()
entities = [
NooaSensor(data, "R"),
NooaSensor(data, "S"),
NooaSensor(data, "G"),
]
async_add_entities(entities)
class NooaSensor(Entity):
"""Representation of sensor."""
def __init__(self, data, sensor_type):
"""Init the sensor."""
self._unit = sensor_type
self._attributes = {}
self._state = data["0"][sensor_type]["Scale"]
self.filter_attributes(data)
self._name = STATIC_DATA[sensor_type][0]
@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 = {
"Yesterday": data["-1"][self._unit]["Scale"],
"Tomorrow": data["1"][self._unit]["Scale"],
"Day after tomorrow": data["2"][self._unit]["Scale"],
"In three days": data["3"][self._unit]["Scale"]
}
async def async_update(self):
"""Fetch new state data for the sensor."""
data = await nooa.get_data()
self._state = data["0"][self._unit]["Scale"]
self.filter_attributes(data)
@kennedyshead
Copy link
Author

kennedyshead commented Apr 7, 2022 via email

@Cobolt60
Copy link

Cobolt60 commented May 15, 2022

I'm have some trouble getting this working and was wondering if there are any further instructions?
(I used https://techcode7821332.wordpress.com/2018/12/19/custom-nooa-space-weather-component-for-homeassistant/)

I'm using the latest version of HASS IO on a Pi.
I created a folder under /custom_components called sensors (/custom_components/sensors) and copied the py file to that folder (/config/custom_components/sensors/nooa_space_weather.py)
I'm assuming I add the
sensors:
– platform: nooa_space_weather
to my configuration.yaml?

I did this, which results in:
Setup failed for sensors: Integration not found.
22:42:49 – (ERROR) setup.py

Any thoughts on how to proceed?

@kennedyshead
Copy link
Author

Hi! it should look like this: /custom_components/nooa_space_weather/sensors.py There are also some more stuff needed nowadays to make the component work, I could upload a zip to put in custom_components if you want?

@kennedyshead
Copy link
Author

Unzip this in custom_components: https://cloud.mknutas.se/s/geN5iztFrMn8D92 and then add config to yaml and reload hass.

@Cobolt60
Copy link

Thank you! That was very helpful. It seems to be working.
I just need to wait for some solar activity as everything is clear at the moment.
Appreciate your help and making this available!

@drhuszkar
Copy link

drhuszkar commented Feb 17, 2023

Hi!
May I ask to upload that zip again? The old url dosn't working and it make me happy to use this tool.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment