diff --git a/.coveragerc b/.coveragerc index 691cb6ecdc1b6f..04b23afb79bca9 100644 --- a/.coveragerc +++ b/.coveragerc @@ -581,6 +581,7 @@ omit = homeassistant/components/stiebel_eltron/* homeassistant/components/streamlabswater/* homeassistant/components/stride/notify.py + homeassistant/components/suez_water/* homeassistant/components/supervisord/sensor.py homeassistant/components/swiss_hydrological_data/sensor.py homeassistant/components/swiss_public_transport/sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index ca46cd3471fc59..398de307bd6bd3 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -239,6 +239,7 @@ homeassistant/components/sql/* @dgomes homeassistant/components/statistics/* @fabaff homeassistant/components/stiebel_eltron/* @fucm homeassistant/components/stream/* @hunterjm +homeassistant/components/suez_water/* @ooii homeassistant/components/sun/* @Swamp-Ig homeassistant/components/supla/* @mwegrzynek homeassistant/components/swiss_hydrological_data/* @fabaff diff --git a/homeassistant/components/suez_water/__init__.py b/homeassistant/components/suez_water/__init__.py new file mode 100644 index 00000000000000..a2d07a8d0a4de1 --- /dev/null +++ b/homeassistant/components/suez_water/__init__.py @@ -0,0 +1 @@ +"""France Suez Water integration.""" diff --git a/homeassistant/components/suez_water/manifest.json b/homeassistant/components/suez_water/manifest.json new file mode 100644 index 00000000000000..8ee3de2d77fa34 --- /dev/null +++ b/homeassistant/components/suez_water/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "suez_water", + "name": "Suez Water Consumption Sensor", + "documentation": "https://www.home-assistant.io/components/suez_water", + "dependencies": [], + "codeowners": ["@ooii"], + "requirements": ["pysuez==0.1.17"] +} diff --git a/homeassistant/components/suez_water/sensor.py b/homeassistant/components/suez_water/sensor.py new file mode 100644 index 00000000000000..cc6db4290e5b33 --- /dev/null +++ b/homeassistant/components/suez_water/sensor.py @@ -0,0 +1,128 @@ +"""Sensor for Suez Water Consumption data.""" +from datetime import timedelta +import logging + +import voluptuous as vol + +from pysuez.client import PySuezError +from pysuez import SuezClient + +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, VOLUME_LITERS +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity import Entity + +_LOGGER = logging.getLogger(__name__) +CONF_COUNTER_ID = 'counter_id' + +SCAN_INTERVAL = timedelta(hours=12) + +COMPONENT_ICON = 'mdi:water-pump' +COMPONENT_NAME = "Suez Water Client" + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_USERNAME): cv.string, + vol.Required(CONF_PASSWORD): cv.string, + vol.Required(CONF_COUNTER_ID): cv.string, +}) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the sensor platform.""" + username = config[CONF_USERNAME] + password = config[CONF_PASSWORD] + counter_id = config[CONF_COUNTER_ID] + try: + client = SuezClient( + username, password, counter_id) + + if not client.check_credentials(): + _LOGGER.warning("Wrong username and/or password") + return + + except PySuezError: + _LOGGER.warning("Unable to create Suez Client") + return + + add_entities([SuezSensor(client)], True) + + +class SuezSensor(Entity): + """Representation of a Sensor.""" + + def __init__(self, client): + """Initialize the data object.""" + self._attributes = {} + self._state = None + self._available = None + self.client = client + + @property + def name(self): + """Return the name of the sensor.""" + return COMPONENT_NAME + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def unit_of_measurement(self): + """Return the unit of measurement.""" + return VOLUME_LITERS + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return self._attributes + + @property + def icon(self): + """Return the icon of the sensor.""" + return COMPONENT_ICON + + def _fetch_data(self): + """Fetch latest data from Suez.""" + try: + self.client.update() + # _state holds the volume of consumed water during previous day + self._state = self.client.state + self._available = True + self._attributes[ + 'attribution'] = self.client.attributes[ + 'attribution'] + self._attributes['this_month_consumption'] = {} + for item in self.client.attributes['thisMonthConsumption']: + self._attributes['this_month_consumption'][ + item] = self.client.attributes[ + 'thisMonthConsumption'][item] + self._attributes['previous_month_consumption'] = {} + for item in self.client.attributes['previousMonthConsumption']: + self._attributes['previous_month_consumption'][ + item] = self.client.attributes[ + 'previousMonthConsumption'][item] + self._attributes[ + 'highest_monthly_consumption'] = self.client.attributes[ + 'highestMonthlyConsumption'] + self._attributes[ + 'last_year_overall'] = self.client.attributes[ + 'lastYearOverAll'] + self._attributes[ + 'this_year_overall'] = self.client.attributes[ + 'thisYearOverAll'] + self._attributes['history'] = {} + for item in self.client.attributes['history']: + self._attributes[ + 'history'][item] = self.client.attributes[ + 'history'][item] + + except PySuezError: + self._available = False + _LOGGER.warning("Unable to fetch data") + + def update(self): + """Return the latest collected data from Linky.""" + self._fetch_data() + _LOGGER.debug( + "Suez data state is: %s.", self._state) diff --git a/requirements_all.txt b/requirements_all.txt index 4b91ff3a77daab..e049e7ad1c9681 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1375,6 +1375,9 @@ pystiebeleltron==0.0.1.dev2 # homeassistant.components.stride pystride==0.1.7 +# homeassistant.components.suez_water +pysuez==0.1.17 + # homeassistant.components.supla pysupla==0.0.3