Last active
February 17, 2023 16:05
-
-
Save kennedyshead/180aaffc17a76b195451f364e38e4f07 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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) |
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?
Author
Unzip this in custom_components: https://cloud.mknutas.se/s/geN5iztFrMn8D92 and then add config to yaml and reload hass.
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!
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
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?