core/homeassistant/components/suez_water/sensor.py

126 lines
4.0 KiB
Python
Raw Normal View History

Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
"""Sensor for Suez Water Consumption data."""
from datetime import timedelta
import logging
from pysuez import SuezClient
from pysuez.client import PySuezError
import voluptuous as vol
Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
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__)
2019-07-31 19:25:30 +00:00
CONF_COUNTER_ID = "counter_id"
Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
SCAN_INTERVAL = timedelta(hours=12)
2019-07-31 19:25:30 +00:00
COMPONENT_ICON = "mdi:water-pump"
Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
COMPONENT_NAME = "Suez Water Client"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_COUNTER_ID): cv.string,
}
)
Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
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:
2019-07-31 19:25:30 +00:00
client = SuezClient(username, password, counter_id)
Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
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
2019-07-31 19:25:30 +00:00
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
]
Suez water (#23844) * Add suez water sensor * flake8 test * pylint test * edition to fix flake8 and pylint issues * edition to be okay with the musts * Added a blank line to __init.py__ for flake8 * added blank line for flake8 * changer scan interval from 10 to 720 minutes * use of pysuez * bug fix and isort * use of pysuez * fixed flake8 and pylint errors * update requirements_all.txt * added a method to test login/password befire adding device * flake8 edition * update requirements_all.txt * add of .coveragerc file with untested files * update of .coveragerc * Update homeassistant/components/suez_water/__init__.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Update homeassistant/components/suez_water/sensor.py Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * bug fix in check credentials * flake8 and pylint fixes * fix codeowner * update requirements_all.txt * Sorted suez_water line * edition to answer comments from @MartinHjelmare on #23844 * Attribute keys formatting to lowercase snakecase, name and icon constants returned directly, and remove of attribute. Update of . * pylint edition * correction wrong keys in client attributes * remove of unnedeed return and move add_entities
2019-07-23 23:14:41 +00:00
except PySuezError:
self._available = False
_LOGGER.warning("Unable to fetch data")
def update(self):
"""Return the latest collected data from Linky."""
self._fetch_data()
2019-07-31 19:25:30 +00:00
_LOGGER.debug("Suez data state is: %s.", self._state)