Add Flick Electric NZ integration (#30696)
* Add integration for Flick Electric NZ * Start adding Config Flow and external API * Second Wave of Config Flow and API implementation * Fix test (errors is None instead of blank array?) * Don't update sensor if price is still valid * Add input validation * Fix linting for DOMAIN Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Remove platform schema (config is by entries only) * Don't catch AbortFlow exception Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update test * Re-arrange try-catch in config flow * Fix linting in sensor.py * Staticly define list of components * Fix test exceptions * Fix _validate_input not being awaited * Fix tests * Fix pylint logger * Rename test and remove print debug * Add test for duplicate entry * Don't format string in log function * Add tests __init__ file * Remove duplicate result assignment * Add test for generic exception handling * Move translations folder * Simplify testing * Fix strings/translation * Move to "flick_electric" as domain Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
0cf1ca7736
commit
e2b622fb78
14 files changed
+464
No files matched your search
@@ -0,0 +1,83 @@
|
||||
"""Support for Flick Electric Pricing data."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import async_timeout
|
||||
from pyflick import FlickAPI, FlickPrice
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_FRIENDLY_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .const import ATTR_COMPONENTS, ATTR_END_AT, ATTR_START_AT, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_AUTH_URL = "https://api.flick.energy/identity/oauth/token"
|
||||
_RESOURCE = "https://api.flick.energy/customer/mobile_provider/price"
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=5)
|
||||
|
||||
ATTRIBUTION = "Data provided by Flick Electric"
|
||||
FRIENDLY_NAME = "Flick Power Price"
|
||||
UNIT_NAME = "cents"
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
|
||||
):
|
||||
"""Flick Sensor Setup."""
|
||||
api: FlickAPI = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
async_add_entities([FlickPricingSensor(api)], True)
|
||||
|
||||
|
||||
class FlickPricingSensor(Entity):
|
||||
"""Entity object for Flick Electric sensor."""
|
||||
|
||||
def __init__(self, api: FlickAPI):
|
||||
"""Entity object for Flick Electric sensor."""
|
||||
self._api: FlickAPI = api
|
||||
self._price: FlickPrice = None
|
||||
self._attributes = {
|
||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||
ATTR_FRIENDLY_NAME: FRIENDLY_NAME,
|
||||
}
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return FRIENDLY_NAME
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._price.price
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return UNIT_NAME
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._attributes
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the Flick Pricing data from the web service."""
|
||||
if self._price and self._price.end_at >= utcnow():
|
||||
return # Power price data is still valid
|
||||
|
||||
with async_timeout.timeout(60):
|
||||
self._price = await self._api.getPricing()
|
||||
|
||||
self._attributes[ATTR_START_AT] = self._price.start_at
|
||||
self._attributes[ATTR_END_AT] = self._price.end_at
|
||||
for component in self._price.components:
|
||||
if component.charge_setter not in ATTR_COMPONENTS:
|
||||
_LOGGER.warning("Found unknown component: %s", component.charge_setter)
|
||||
continue
|
||||
|
||||
self._attributes[component.charge_setter] = float(component.value)
|
||||
Reference in new issue
Block a user