def cli(username, password, date, daily=True, halfhour=False): """CLI for this package.""" from ovoenergy.ovoenergy import OVOEnergy ovo = OVOEnergy(username, password) if daily is True: print(json.dumps(ovo.get_daily_usage(date))) if halfhour is True: print(json.dumps(ovo.get_half_hourly_usage(date)))
async def async_step_user(self, user_input=None): """Handle a flow initiated by the user.""" errors = {} if user_input is not None: client = OVOEnergy() try: authenticated = await client.authenticate( user_input[CONF_USERNAME], user_input[CONF_PASSWORD]) except aiohttp.ClientError: errors["base"] = "cannot_connect" else: if authenticated: await self.async_set_unique_id(user_input[CONF_USERNAME]) self._abort_if_unique_id_configured() return self.async_create_entry( title=client.username, data={ CONF_USERNAME: user_input[CONF_USERNAME], CONF_PASSWORD: user_input[CONF_PASSWORD], }, ) errors["base"] = "invalid_auth" return self.async_show_form(step_id="user", data_schema=USER_SCHEMA, errors=errors)
async def async_step_reauth(self, user_input): """Handle configuration by re-auth.""" errors = {} if user_input and user_input.get(CONF_USERNAME): self.username = user_input[CONF_USERNAME] self.context["title_placeholders"] = {CONF_USERNAME: self.username} if user_input is not None and user_input.get( CONF_PASSWORD) is not None: client = OVOEnergy() try: authenticated = await client.authenticate( self.username, user_input[CONF_PASSWORD]) except aiohttp.ClientError: errors["base"] = "connection_error" else: if authenticated: entry = await self.async_set_unique_id(self.username) self.hass.config_entries.async_update_entry( entry, data={ CONF_USERNAME: self.username, CONF_PASSWORD: user_input[CONF_PASSWORD], }, ) return self.async_abort(reason="reauth_successful") errors["base"] = "authorization_error" return self.async_show_form(step_id="reauth", data_schema=REAUTH_SCHEMA, errors=errors)
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: """Set up OVO Energy from a config entry.""" client = OVOEnergy() try: authenticated = await client.authenticate(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) except aiohttp.ClientError as exception: _LOGGER.warning(exception) raise ConfigEntryNotReady from exception if not authenticated: hass.async_create_task( hass.config_entries.flow.async_init(DOMAIN, context={"source": "reauth"}, data=entry.data)) return False async def async_update_data() -> OVODailyUsage: """Fetch data from OVO Energy.""" async with async_timeout.timeout(10): try: authenticated = await client.authenticate( entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) except aiohttp.ClientError as exception: raise UpdateFailed(exception) from exception if not authenticated: hass.async_create_task( hass.config_entries.flow.async_init( DOMAIN, context={"source": "reauth"}, data=entry.data)) raise UpdateFailed("Not authenticated with OVO Energy") return await client.get_daily_usage( datetime.utcnow().strftime("%Y-%m")) coordinator = DataUpdateCoordinator( hass, _LOGGER, # Name of the data. For logging purposes. name="sensor", update_method=async_update_data, # Polling interval. Will only be polled if there are subscribers. update_interval=timedelta(seconds=300), ) hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = { DATA_CLIENT: client, DATA_COORDINATOR: coordinator, } # Fetch initial data so we have data when entities subscribe await coordinator.async_refresh() # Setup components hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, "sensor")) return True
async def async_setup_entry(opp: OpenPeerPower, entry: ConfigEntry) -> bool: """Set up OVO Energy from a config entry.""" client = OVOEnergy() try: authenticated = await client.authenticate(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) except aiohttp.ClientError as exception: _LOGGER.warning(exception) raise ConfigEntryNotReady from exception if not authenticated: raise ConfigEntryAuthFailed async def async_update_data() -> OVODailyUsage: """Fetch data from OVO Energy.""" async with async_timeout.timeout(10): try: authenticated = await client.authenticate( entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) except aiohttp.ClientError as exception: raise UpdateFailed(exception) from exception if not authenticated: raise ConfigEntryAuthFailed( "Not authenticated with OVO Energy") return await client.get_daily_usage( datetime.utcnow().strftime("%Y-%m")) coordinator = DataUpdateCoordinator( opp, _LOGGER, # Name of the data. For logging purposes. name="sensor", update_method=async_update_data, # Polling interval. Will only be polled if there are subscribers. update_interval=timedelta(seconds=3600), ) opp.data.setdefault(DOMAIN, {}) opp.data[DOMAIN][entry.entry_id] = { DATA_CLIENT: client, DATA_COORDINATOR: coordinator, } # Fetch initial data so we have data when entities subscribe await coordinator.async_config_entry_first_refresh() # Setup components opp.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def handle(username, password, date, daily=True, halfhour=False) -> None: client = OVOEnergy() authenticated = await client.authenticate(username, password) print(f"Authenticated: {authenticated}") if authenticated: print("Authenticated.") if daily is True: usage = await client.get_daily_usage(date) if usage is not None: print("Usage:") print(usage) if usage.electricity is not None: print("Electricity:") count = 0 for x in usage.electricity: count += 1 print(f"{count}.consumption: {x.consumption}") print(f"{count}.interval.start: {x.interval.start}") print(f"{count}.interval.end: {x.interval.end}") print(f"{count}.meter_readings.start: {x.meter_readings.start}") print(f"{count}.meter_readings.end: {x.meter_readings.end}") print(f"{count}.has_hh_data: {x.has_half_hour_data}") print(f"{count}.cost.amount: {x.cost.amount}") print(f"{count}.cost.currency_unit: {x.cost.currency_unit}") if usage.gas is not None: print("Gas:") count = 0 for x in usage.gas: count += 1 print(f"{count}.consumption: {x.consumption}") print(f"{count}.volume: {x.volume}") print(f"{count}.interval.start: {x.interval.start}") print(f"{count}.interval.end: {x.interval.end}") print(f"{count}.meter_readings.start: {x.meter_readings.start}") print(f"{count}.meter_readings.end: {x.meter_readings.end}") print(f"{count}.has_hh_data: {x.has_half_hour_data}") print(f"{count}.cost.amount: {x.cost.amount}") print(f"{count}.cost.currency_unit: {x.cost.currency_unit}") if halfhour is True: usage = await client.get_half_hourly_usage(date) if usage is not None: print("Usage:") print(usage) if usage.electricity is not None: print("Electricity:") count = 0 for x in usage.electricity: count += 1 print(f"consumption: {x.consumption}") print(f"{count}.interval.start: {x.interval.start}") print(f"{count}.interval.end: {x.interval.end}") print(f"{count}.unit: {x.unit}") if usage.gas is not None: print("Gas:") count = 0 for x in usage.gas: count += 1 print(f"{count}.consumption: {x.consumption}") print(f"{count}.interval.start: {x.interval.start}") print(f"{count}.interval.end: {x.interval.end}") print(f"{count}.unit: {x.unit}")