1 """Support for ComEd Hourly Pricing data."""
3 from __future__
import annotations
6 from datetime
import timedelta
11 import voluptuous
as vol
14 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
16 SensorEntityDescription,
25 _LOGGER = logging.getLogger(__name__)
26 _RESOURCE =
"https://hourlypricing.comed.com/api"
30 CONF_CURRENT_HOUR_AVERAGE =
"current_hour_average"
31 CONF_FIVE_MINUTE =
"five_minute"
32 CONF_MONITORED_FEEDS =
"monitored_feeds"
33 CONF_SENSOR_TYPE =
"type"
35 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
38 name=
"ComEd 5 Minute Price",
39 native_unit_of_measurement=f
"{CURRENCY_CENT}/{UnitOfEnergy.KILO_WATT_HOUR}",
42 key=CONF_CURRENT_HOUR_AVERAGE,
43 name=
"ComEd Current Hour Average Price",
44 native_unit_of_measurement=f
"{CURRENCY_CENT}/{UnitOfEnergy.KILO_WATT_HOUR}",
48 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
50 TYPES_SCHEMA = vol.In(SENSOR_KEYS)
52 SENSORS_SCHEMA = vol.Schema(
54 vol.Required(CONF_SENSOR_TYPE): TYPES_SCHEMA,
55 vol.Optional(CONF_NAME): cv.string,
56 vol.Optional(CONF_OFFSET, default=0.0): vol.Coerce(float),
60 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
61 {vol.Required(CONF_MONITORED_FEEDS): [SENSORS_SCHEMA]}
68 async_add_entities: AddEntitiesCallback,
69 discovery_info: DiscoveryInfoType |
None =
None,
71 """Set up the ComEd Hourly Pricing sensor."""
77 variable[CONF_OFFSET],
78 variable.get(CONF_NAME),
81 for variable
in config[CONF_MONITORED_FEEDS]
82 for description
in SENSOR_TYPES
83 if description.key == variable[CONF_SENSOR_TYPE]
90 """Implementation of a ComEd Hourly Pricing sensor."""
92 _attr_attribution =
"Data provided by ComEd Hourly Pricing service"
95 self, websession, offset, name, description: SensorEntityDescription
97 """Initialize the sensor."""
105 """Get the ComEd Hourly Pricing data from the web service."""
108 if sensor_type
in (CONF_FIVE_MINUTE, CONF_CURRENT_HOUR_AVERAGE):
109 url_string = _RESOURCE
110 if sensor_type == CONF_FIVE_MINUTE:
111 url_string +=
"?type=5minutefeed"
113 url_string +=
"?type=currenthouraverage"
115 async
with asyncio.timeout(60):
118 text = await response.text()
119 data = json.loads(text)
127 except (TimeoutError, aiohttp.ClientError)
as err:
128 _LOGGER.error(
"Could not get data from ComEd API: %s", err)
129 except (ValueError, KeyError):
130 _LOGGER.warning(
"Could not update status for %s", self.
namename)
None __init__(self, websession, offset, name, SensorEntityDescription description)
str|UndefinedType|None name(self)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
web.Response get(self, web.Request request, str config_key)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)