314ma

Fronius Sensor

May 24th, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. import logging
  2. import requests
  3.  
  4. import voluptuous as vol
  5.  
  6. from homeassistant.components.sensor import (PLATFORM_SCHEMA, ENTITY_ID_FORMAT)
  7. from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_ADDRESS, CONF_DEVICE_ID, ATTR_ATTRIBUTION
  8. import homeassistant.helpers.config_validation as cv
  9. from homeassistant.helpers.entity import Entity
  10. from homeassistant.helpers.entity import async_generate_entity_id
  11.  
  12. _LOGGER = logging.getLogger(__name__)
  13.  
  14. DEFAULT_NAME = 'Fronius'
  15.  
  16. SENSOR_TYPES = {
  17.     'fac': {"jsonPath": "Body.Data.FAC.Value", "name": "FAC", "unit": "Hz", "icon": "mdi:weather-lightning"},
  18.     'iac': {"jsonPath": "Body.Data.IAC.Value", "name": "IAC", "unit": "A", "icon": "mdi:weather-lightning"},
  19.     'status_code': {"jsonPath": "Body.Data.DeviceStatus.StatusCode", "name": "Status Code", "unit": " ", "icon": "mdi:weather-lightning"},
  20.     'day_energy': {"jsonPath": "Body.Data.DAY_ENERGY.Value", "name": "Day Energy", "unit": "Wh", "icon": "mdi:weather-lightning"},
  21. }
  22.  
  23. PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
  24.     vol.Required(CONF_NAME, default=DEFAULT_NAME): cv.string,
  25.     vol.Required(CONF_ADDRESS): cv.string,
  26.     vol.Required(CONF_DEVICE_ID): cv.string,
  27.     vol.Required(CONF_MONITORED_CONDITIONS, default=[]):
  28.         vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)])
  29. })
  30.  
  31.  
  32. def setup_platform(hass, config, add_entities, discovery_info=None):
  33.     address = config.get(CONF_ADDRESS)
  34.     device_id = config.get(CONF_DEVICE_ID)
  35.     name = config.get(CONF_NAME)
  36.     dev = []
  37.     for monitored_condition in config[CONF_MONITORED_CONDITIONS]:
  38.         uid = '{}_{}_{}'.format(DEFAULT_NAME, device_id, monitored_condition)
  39.         entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, uid, hass=hass)
  40.         dev.append(FroniusSensor(entity_id, name, address, device_id, monitored_condition))
  41.     add_entities(dev, True)
  42.  
  43.  
  44. class FroniusSensor(Entity):
  45.     def __init__(self, entity_id, name, address, device_id, sensor_type):
  46.         self.entity_id = entity_id
  47.         self._platform_name = name
  48.         self._address = address
  49.         self._device_id = device_id
  50.         self.sensor_type = sensor_type
  51.         self._data = None
  52.         self._state = None
  53.         self._jsonPath = SENSOR_TYPES[sensor_type]["jsonPath"]
  54.         self._sensor_name = SENSOR_TYPES[sensor_type]["name"]
  55.         self._unit_of_measurement = SENSOR_TYPES[sensor_type]["unit"]
  56.         self._icon = SENSOR_TYPES[sensor_type]["icon"]
  57.  
  58.     @property
  59.     def name(self):
  60.         return '{} - {}'.format(self._platform_name, self._sensor_name)
  61.  
  62.     @property
  63.     def icon(self):
  64.         return self._icon
  65.  
  66.     @property
  67.     def state(self):
  68.         if self._data is not None:
  69.             self._state = FroniusSensor.extractor(self._data, self._jsonPath)
  70.         return self._state
  71.  
  72.     @property
  73.     def unit_of_measurement(self):
  74.         return self._unit_of_measurement
  75.  
  76.     def update(self):
  77.         address = '{}/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId={}&DataCollection=CommonInverterData'.format(
  78.             self._address, self._device_id)
  79.         request = requests.get(address)
  80.         if request.status_code == 200 and request.content.__len__() > 0:
  81.             self._data = request.json()
  82.  
  83.     @staticmethod
  84.     def extractor(json, path):
  85.         def extractor_arr(json_obj, path_array):
  86.             if len(path_array) > 1:
  87.                 return extractor_arr(json_obj[path_array[0]], path_array[1:])
  88.             return json_obj[path_array[0]]
  89.  
  90.         return extractor_arr(json, path.split("."))
Add Comment
Please, Sign In to add comment