core/homeassistant/components/flume/sensor.py

95 lines
2.7 KiB
Python
Raw Normal View History

Add flume support (#27235) * Add Flume Sensor Add support for Flume API and sensor * Add support for choosing timezone Timezone is now a required option in configuration.yaml * Add Flume to coveragerc and CODEOWNERS Add flume to be ommited from testing. Add @ChrisMandich as Flume Code owner * Revert "Add Flume to coveragerc and CODEOWNERS" This reverts commit 0b27437a3b13f024332c6498b3bb0d554482e49b. * Update manifest.json Added Documentation URL for Flume to HASS.IO * Update manifest.json Added missing Newline at EOF. * Update sensor.py Update sensor to not required Device ID for configuration. Now loops through all available Type 2 devices and adds them as an entity. * Update Manifest, CODEOWNERS, and manifest.json Resolved errors related to code owners and requirements. Using hassfest and gen_requirements_all * Update sensor.py Implemented recommendations from @Quentame. Including time zone from Home Assistant, Updated variable names, and Consolidated duplicate functions. * Implemented suggested changes from @Quentame This includes: components name, using f-strings instead of concat, snake_case for variables, constants for the addition of future device types, clearer errors, and removed variables no longer in use. * Update sensor.py Restored unit_of_measurement. Updated return to "gal". * Address pylint errors * Update sensor.py Include protected attributes in setup_platform. * Address Pylint errors homeassistant/components/flume/sensor.py:63:11: W0703: Catching too general exception Exception (broad-except) homeassistant/components/flume/sensor.py:133:8: R1720: Unnecessary "else" after "raise" (no-else-raise) homeassistant/components/flume/sensor.py:162:8: R1705: Unnecessary "else" after "return" (no-else-return) homeassistant/components/flume/sensor.py:236:8: R1720: Unnecessary "else" after "raise" (no-else-raise) * Update sensor.py I'm okay with the broad exception clause. homeassistant/components/flume/sensor.py:65:11: W0703: Catching too general exception Exception (broad-except) * Update sensor.py Add more specific exceptions for Try/Except. * Update Flume Sensory.py add requirements, exclude from tests, us pyflume pypi package. * Update sensor.py to support latest pyflume package * Update manifest provide better flow of manifest and easier readibility. * Update manifest.json Reccomended by @balloob as it is already a core requirement * Update sensor.py Add proposed changes from @balloob * Update requirements_all.txt pytz is a core dependency, removing flume's requirement for it. * Update sensor.py Added @MartinHjelmare recommended changes. * Update sensor.py Resolving PyLint error * Update sensor.py Remove `KeyError`. Add length check for flume entity list before adding. * Update sensor.py * Update pyflume version * Update imports with isort * Add line break between standard library and thirdparty imports. * Remove throttle from sensor.py
2019-11-23 08:55:46 +00:00
"""Sensor for displaying the number of result from Flume."""
from datetime import timedelta
import logging
from pyflume import FlumeData, FlumeDeviceList
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Flume Sensor"
CONF_CLIENT_ID = "client_id"
CONF_CLIENT_SECRET = "client_secret"
FLUME_TYPE_SENSOR = 2
SCAN_INTERVAL = timedelta(minutes=1)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_CLIENT_ID): cv.string,
vol.Required(CONF_CLIENT_SECRET): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Flume sensor."""
username = config[CONF_USERNAME]
password = config[CONF_PASSWORD]
client_id = config[CONF_CLIENT_ID]
client_secret = config[CONF_CLIENT_SECRET]
flume_token_file = hass.config.path("FLUME_TOKEN_FILE")
Add flume support (#27235) * Add Flume Sensor Add support for Flume API and sensor * Add support for choosing timezone Timezone is now a required option in configuration.yaml * Add Flume to coveragerc and CODEOWNERS Add flume to be ommited from testing. Add @ChrisMandich as Flume Code owner * Revert "Add Flume to coveragerc and CODEOWNERS" This reverts commit 0b27437a3b13f024332c6498b3bb0d554482e49b. * Update manifest.json Added Documentation URL for Flume to HASS.IO * Update manifest.json Added missing Newline at EOF. * Update sensor.py Update sensor to not required Device ID for configuration. Now loops through all available Type 2 devices and adds them as an entity. * Update Manifest, CODEOWNERS, and manifest.json Resolved errors related to code owners and requirements. Using hassfest and gen_requirements_all * Update sensor.py Implemented recommendations from @Quentame. Including time zone from Home Assistant, Updated variable names, and Consolidated duplicate functions. * Implemented suggested changes from @Quentame This includes: components name, using f-strings instead of concat, snake_case for variables, constants for the addition of future device types, clearer errors, and removed variables no longer in use. * Update sensor.py Restored unit_of_measurement. Updated return to "gal". * Address pylint errors * Update sensor.py Include protected attributes in setup_platform. * Address Pylint errors homeassistant/components/flume/sensor.py:63:11: W0703: Catching too general exception Exception (broad-except) homeassistant/components/flume/sensor.py:133:8: R1720: Unnecessary "else" after "raise" (no-else-raise) homeassistant/components/flume/sensor.py:162:8: R1705: Unnecessary "else" after "return" (no-else-return) homeassistant/components/flume/sensor.py:236:8: R1720: Unnecessary "else" after "raise" (no-else-raise) * Update sensor.py I'm okay with the broad exception clause. homeassistant/components/flume/sensor.py:65:11: W0703: Catching too general exception Exception (broad-except) * Update sensor.py Add more specific exceptions for Try/Except. * Update Flume Sensory.py add requirements, exclude from tests, us pyflume pypi package. * Update sensor.py to support latest pyflume package * Update manifest provide better flow of manifest and easier readibility. * Update manifest.json Reccomended by @balloob as it is already a core requirement * Update sensor.py Add proposed changes from @balloob * Update requirements_all.txt pytz is a core dependency, removing flume's requirement for it. * Update sensor.py Added @MartinHjelmare recommended changes. * Update sensor.py Resolving PyLint error * Update sensor.py Remove `KeyError`. Add length check for flume entity list before adding. * Update sensor.py * Update pyflume version * Update imports with isort * Add line break between standard library and thirdparty imports. * Remove throttle from sensor.py
2019-11-23 08:55:46 +00:00
time_zone = str(hass.config.time_zone)
name = config[CONF_NAME]
flume_entity_list = []
flume_devices = FlumeDeviceList(
username, password, client_id, client_secret, flume_token_file
)
Add flume support (#27235) * Add Flume Sensor Add support for Flume API and sensor * Add support for choosing timezone Timezone is now a required option in configuration.yaml * Add Flume to coveragerc and CODEOWNERS Add flume to be ommited from testing. Add @ChrisMandich as Flume Code owner * Revert "Add Flume to coveragerc and CODEOWNERS" This reverts commit 0b27437a3b13f024332c6498b3bb0d554482e49b. * Update manifest.json Added Documentation URL for Flume to HASS.IO * Update manifest.json Added missing Newline at EOF. * Update sensor.py Update sensor to not required Device ID for configuration. Now loops through all available Type 2 devices and adds them as an entity. * Update Manifest, CODEOWNERS, and manifest.json Resolved errors related to code owners and requirements. Using hassfest and gen_requirements_all * Update sensor.py Implemented recommendations from @Quentame. Including time zone from Home Assistant, Updated variable names, and Consolidated duplicate functions. * Implemented suggested changes from @Quentame This includes: components name, using f-strings instead of concat, snake_case for variables, constants for the addition of future device types, clearer errors, and removed variables no longer in use. * Update sensor.py Restored unit_of_measurement. Updated return to "gal". * Address pylint errors * Update sensor.py Include protected attributes in setup_platform. * Address Pylint errors homeassistant/components/flume/sensor.py:63:11: W0703: Catching too general exception Exception (broad-except) homeassistant/components/flume/sensor.py:133:8: R1720: Unnecessary "else" after "raise" (no-else-raise) homeassistant/components/flume/sensor.py:162:8: R1705: Unnecessary "else" after "return" (no-else-return) homeassistant/components/flume/sensor.py:236:8: R1720: Unnecessary "else" after "raise" (no-else-raise) * Update sensor.py I'm okay with the broad exception clause. homeassistant/components/flume/sensor.py:65:11: W0703: Catching too general exception Exception (broad-except) * Update sensor.py Add more specific exceptions for Try/Except. * Update Flume Sensory.py add requirements, exclude from tests, us pyflume pypi package. * Update sensor.py to support latest pyflume package * Update manifest provide better flow of manifest and easier readibility. * Update manifest.json Reccomended by @balloob as it is already a core requirement * Update sensor.py Add proposed changes from @balloob * Update requirements_all.txt pytz is a core dependency, removing flume's requirement for it. * Update sensor.py Added @MartinHjelmare recommended changes. * Update sensor.py Resolving PyLint error * Update sensor.py Remove `KeyError`. Add length check for flume entity list before adding. * Update sensor.py * Update pyflume version * Update imports with isort * Add line break between standard library and thirdparty imports. * Remove throttle from sensor.py
2019-11-23 08:55:46 +00:00
for device in flume_devices.device_list:
if device["type"] == FLUME_TYPE_SENSOR:
flume = FlumeData(
username,
password,
client_id,
client_secret,
device["id"],
time_zone,
SCAN_INTERVAL,
flume_token_file,
Add flume support (#27235) * Add Flume Sensor Add support for Flume API and sensor * Add support for choosing timezone Timezone is now a required option in configuration.yaml * Add Flume to coveragerc and CODEOWNERS Add flume to be ommited from testing. Add @ChrisMandich as Flume Code owner * Revert "Add Flume to coveragerc and CODEOWNERS" This reverts commit 0b27437a3b13f024332c6498b3bb0d554482e49b. * Update manifest.json Added Documentation URL for Flume to HASS.IO * Update manifest.json Added missing Newline at EOF. * Update sensor.py Update sensor to not required Device ID for configuration. Now loops through all available Type 2 devices and adds them as an entity. * Update Manifest, CODEOWNERS, and manifest.json Resolved errors related to code owners and requirements. Using hassfest and gen_requirements_all * Update sensor.py Implemented recommendations from @Quentame. Including time zone from Home Assistant, Updated variable names, and Consolidated duplicate functions. * Implemented suggested changes from @Quentame This includes: components name, using f-strings instead of concat, snake_case for variables, constants for the addition of future device types, clearer errors, and removed variables no longer in use. * Update sensor.py Restored unit_of_measurement. Updated return to "gal". * Address pylint errors * Update sensor.py Include protected attributes in setup_platform. * Address Pylint errors homeassistant/components/flume/sensor.py:63:11: W0703: Catching too general exception Exception (broad-except) homeassistant/components/flume/sensor.py:133:8: R1720: Unnecessary "else" after "raise" (no-else-raise) homeassistant/components/flume/sensor.py:162:8: R1705: Unnecessary "else" after "return" (no-else-return) homeassistant/components/flume/sensor.py:236:8: R1720: Unnecessary "else" after "raise" (no-else-raise) * Update sensor.py I'm okay with the broad exception clause. homeassistant/components/flume/sensor.py:65:11: W0703: Catching too general exception Exception (broad-except) * Update sensor.py Add more specific exceptions for Try/Except. * Update Flume Sensory.py add requirements, exclude from tests, us pyflume pypi package. * Update sensor.py to support latest pyflume package * Update manifest provide better flow of manifest and easier readibility. * Update manifest.json Reccomended by @balloob as it is already a core requirement * Update sensor.py Add proposed changes from @balloob * Update requirements_all.txt pytz is a core dependency, removing flume's requirement for it. * Update sensor.py Added @MartinHjelmare recommended changes. * Update sensor.py Resolving PyLint error * Update sensor.py Remove `KeyError`. Add length check for flume entity list before adding. * Update sensor.py * Update pyflume version * Update imports with isort * Add line break between standard library and thirdparty imports. * Remove throttle from sensor.py
2019-11-23 08:55:46 +00:00
)
flume_entity_list.append(FlumeSensor(flume, f"{name} {device['id']}"))
if flume_entity_list:
add_entities(flume_entity_list, True)
class FlumeSensor(Entity):
"""Representation of the Flume sensor."""
def __init__(self, flume, name):
"""Initialize the Flume sensor."""
self.flume = flume
self._name = name
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return "gal"
def update(self):
"""Get the latest data and updates the states."""
self.flume.update()
self._state = self.flume.value