Advertisement
314ma

PSE

Jan 24th, 2023
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import csv
  2. import datetime
  3. import requests
  4.  
  5. from homeassistant.components.binary_sensor import BinarySensorEntity
  6.  
  7.  
  8. def setup_platform(hass, config, add_entities, discovery_info=None):
  9.     add_entities([], True)
  10.  
  11.  
  12. class PSEBinarySensor(BinarySensorEntity):
  13.     def __init__(self):
  14.         self.data = None
  15.  
  16.     @property
  17.     def name(self):
  18.         return "PSE peak hour"
  19.  
  20.     @property
  21.     def is_on(self):
  22.         if self.data is None:
  23.             return None
  24.         return not self.data[3].startswith("NORMALNE")
  25.  
  26.     @property
  27.     def extra_state_attributes(self):
  28.         output = dict()
  29.         if self.data is not None:
  30.             output["demand"] = float(self.data[3].replace(",","."))
  31.         return output
  32.  
  33.     @property
  34.     def icon(self):
  35.         if self.is_on:
  36.             return 'mdi:transmission-tower-off'
  37.         return 'mdi:transmission-tower'
  38.  
  39.     def update(self):
  40.         now = datetime.datetime.now()
  41.         now_hour = now.strftime("%-H")
  42.         response = requests.get(f"https://www.pse.pl/getcsv/-/export/csv/PL_GS/data/{now.strftime('%Y%m%d')}")
  43.         csv_output = csv.reader(response.text.splitlines(), delimiter=";")
  44.         self.data = next(filter(lambda r: r[1] == now_hour, csv_output))
  45.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement