Skip to content

Commit

Permalink
File formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hokiebrian committed May 7, 2023
1 parent 643a25a commit 8f34b8d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
3 changes: 3 additions & 0 deletions custom_components/eia_hourly_demand/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
CONF_API_KEY = "api_key"
CONF_ID = "ba_id"


def setup(hass, config):
"""Set up the EIA Energy component."""
return True


async def async_setup_entry(hass, entry):
"""Set up EIA Energy Data from a config entry."""
hass.data.setdefault(DOMAIN, {})
Expand All @@ -19,6 +21,7 @@ async def async_setup_entry(hass, entry):

return True


async def async_unload_entry(hass, entry):
"""Unload the EIA Energy sensor platform."""
await hass.config_entries.async_forward_entry_unload(entry, "sensor")
Expand Down
18 changes: 11 additions & 7 deletions custom_components/eia_hourly_demand/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from datetime import timedelta, date
from .const import DOMAIN

class EIAConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

class EIAConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input=None):
errors = {}

Expand All @@ -15,7 +15,9 @@ async def async_step_user(self, user_input=None):
errors["base"] = "invalid API Key or Balancing Authority"
else:
# If validation is successful, create and return the config entry
return self.async_create_entry(title=user_input["ba_id"], data=user_input)
return self.async_create_entry(
title=user_input["ba_id"], data=user_input
)

return self.async_show_form(
step_id="user",
Expand All @@ -36,10 +38,12 @@ async def _validate_input(self, user_input):
# Check if the API key is valid by making a test API call

start_date = (date.today() - timedelta(days=7)).strftime("%Y-%m-%d")
url = (f"https://api.eia.gov/v2/electricity/rto/region-data/data/"
f"?api_key={api_key}&data[]=value&facets[respondent][]={ba_id}"
f"&facets[type][]=D&frequency=hourly&start={start_date}"
f"&sort[0][column]=period&sort[0][direction]=desc")
url = (
f"https://api.eia.gov/v2/electricity/rto/region-data/data/"
f"?api_key={api_key}&data[]=value&facets[respondent][]={ba_id}"
f"&facets[type][]=D&frequency=hourly&start={start_date}"
f"&sort[0][column]=period&sort[0][direction]=desc"
)

async with aiohttp.ClientSession() as session:
try:
Expand All @@ -53,4 +57,4 @@ async def _validate_input(self, user_input):
except:
pass

return True
return True
2 changes: 1 addition & 1 deletion custom_components/eia_hourly_demand/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/hokiebrian/eia_hourly_demand/issues",
"requirements": [],
"version": "1.0.7"
"version": "1.0.8"
}
15 changes: 9 additions & 6 deletions custom_components/eia_hourly_demand/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

SCAN_INTERVAL = timedelta(seconds=1800)


async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entities):
api_key = config_entry.data["api_key"]
ba_id = config_entry.data["ba_id"]
eia_data = hass.data[DOMAIN][config_entry.entry_id]

async_add_entities([EIASensor(api_key, ba_id, eia_data)], True)

class EIASensor(SensorEntity):

class EIASensor(SensorEntity):
_attr_icon = "mdi:factory"
_attr_native_unit_of_measurement = "MWh"
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
Expand All @@ -35,17 +36,19 @@ def name(self):
@property
def state(self):
return self._state

@property
def unique_id(self):
return f"HourlyMWh{self._ba_id}"

async def async_update(self):
start_date = (date.today() - timedelta(days=1)).strftime("%Y-%m-%d")
url = (f"https://api.eia.gov/v2/electricity/rto/region-data/data/"
f"?api_key={self._api_key}&data[]=value&facets[respondent][]={self._ba_id}"
f"&facets[type][]=D&frequency=hourly&start={start_date}"
f"&sort[0][column]=period&sort[0][direction]=desc")
url = (
f"https://api.eia.gov/v2/electricity/rto/region-data/data/"
f"?api_key={self._api_key}&data[]=value&facets[respondent][]={self._ba_id}"
f"&facets[type][]=D&frequency=hourly&start={start_date}"
f"&sort[0][column]=period&sort[0][direction]=desc"
)

async with aiohttp.ClientSession() as session:
try:
Expand Down

0 comments on commit 8f34b8d

Please sign in to comment.