Code Monkey home page Code Monkey logo

Comments (6)

BottlecapDave avatar BottlecapDave commented on August 30, 2024

Ah I see the issue. I think you're on an tarrif with no current end date, whereas I'm assuming there's always one. I'll rectify this hopefully tonight.

from homeassistant-octopusenergy.

cooperaj avatar cooperaj commented on August 30, 2024

Think i've cracked the empty response to curl -u "sk_live_muhkey:" "https://api.octopus.energy/v1/accounts/A-MUHACC" as curl -u "sk_live_muhkey:" "https://api.octopus.energy/v1/accounts/A-MUHACC/" brings back everything I expect (note the closing slash). I expect curl does not follow the redirect but the python library does.

I think this would still hit the issue you've described as the 'valid_to' field is null in my response.

from homeassistant-octopusenergy.

cooperaj avatar cooperaj commented on August 30, 2024
from homeassistant.util.dt import (utcnow, as_utc, parse_datetime)
import datetime as dt

def get_active_agreement(agreements):
  now = utcnow()

  for agreement in agreements:
    valid_from = as_utc(parse_datetime(agreement["valid_from"]))

    if agreement["valid_to"] is not None:
      valid_to = as_utc(parse_datetime(agreement["valid_to"]))
    else:
      valid_to = now + dt.timedelta(seconds=1)

    if valid_from <= now and valid_to >= now:
      return agreement
  
  return None

Python is not my thing but this appears to work.

from homeassistant-octopusenergy.

BottlecapDave avatar BottlecapDave commented on August 30, 2024

Yup that's the area. I've updated to handle it slightly differently to how you did it, just because I don't want to make any assumptions. Closing issue as I've applied a fix, but feel free to reopen if my fix is somehow still broken

from homeassistant-octopusenergy.

cooperaj avatar cooperaj commented on August 30, 2024

Ah, that's the path I took originally but I think won't work. The valid_to field will always exist, but the value may either be None or a string. I think (unless my knowledge of Python is way off) the altered code will have the same issue as reported.

from homeassistant-octopusenergy.

BottlecapDave avatar BottlecapDave commented on August 30, 2024

I think it should be fine, but to be on the safe side I've added the additional check you had as well.

from homeassistant-octopusenergy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.