mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Remove global variable from sleepiq (#33715)
* Remove global variable from sleepiq * Remove global variable from sleepiq v2 * Create constant file * Move back time constant * Update homeassistant/components/sleepiq/__init__.py Co-Authored-By: Quentame <polletquentin74@me.com> Co-authored-by: Quentame <polletquentin74@me.com>
This commit is contained in:
parent
67c3a4c970
commit
2e6108365e
@ -11,22 +11,12 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
DOMAIN = "sleepiq"
|
from .const import DOMAIN
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
||||||
|
|
||||||
IS_IN_BED = "is_in_bed"
|
|
||||||
SLEEP_NUMBER = "sleep_number"
|
|
||||||
SENSOR_TYPES = {SLEEP_NUMBER: "SleepNumber", IS_IN_BED: "Is In Bed"}
|
|
||||||
|
|
||||||
LEFT = "left"
|
|
||||||
RIGHT = "right"
|
|
||||||
SIDES = [LEFT, RIGHT]
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DATA = None
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(DOMAIN): vol.Schema(
|
vol.Required(DOMAIN): vol.Schema(
|
||||||
@ -46,14 +36,14 @@ def setup(hass, config):
|
|||||||
Will automatically load sensor components to support
|
Will automatically load sensor components to support
|
||||||
devices discovered on the account.
|
devices discovered on the account.
|
||||||
"""
|
"""
|
||||||
global DATA # pylint: disable=global-statement
|
data = None
|
||||||
|
|
||||||
username = config[DOMAIN][CONF_USERNAME]
|
username = config[DOMAIN][CONF_USERNAME]
|
||||||
password = config[DOMAIN][CONF_PASSWORD]
|
password = config[DOMAIN][CONF_PASSWORD]
|
||||||
client = Sleepyq(username, password)
|
client = Sleepyq(username, password)
|
||||||
try:
|
try:
|
||||||
DATA = SleepIQData(client)
|
data = SleepIQData(client)
|
||||||
DATA.update()
|
data.update()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
message = """
|
message = """
|
||||||
SleepIQ failed to login, double check your username and password"
|
SleepIQ failed to login, double check your username and password"
|
||||||
@ -61,6 +51,7 @@ def setup(hass, config):
|
|||||||
_LOGGER.error(message)
|
_LOGGER.error(message)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
hass.data[DOMAIN] = data
|
||||||
discovery.load_platform(hass, "sensor", DOMAIN, {}, config)
|
discovery.load_platform(hass, "sensor", DOMAIN, {}, config)
|
||||||
discovery.load_platform(hass, "binary_sensor", DOMAIN, {}, config)
|
discovery.load_platform(hass, "binary_sensor", DOMAIN, {}, config)
|
||||||
|
|
||||||
|
@ -1,33 +1,35 @@
|
|||||||
"""Support for SleepIQ sensors."""
|
"""Support for SleepIQ sensors."""
|
||||||
from homeassistant.components import sleepiq
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||||
|
|
||||||
|
from . import SleepIQSensor
|
||||||
|
from .const import DOMAIN, IS_IN_BED, SENSOR_TYPES, SIDES
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the SleepIQ sensors."""
|
"""Set up the SleepIQ sensors."""
|
||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
data = sleepiq.DATA
|
data = hass.data[DOMAIN]
|
||||||
data.update()
|
data.update()
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for bed_id, bed in data.beds.items():
|
for bed_id, bed in data.beds.items():
|
||||||
for side in sleepiq.SIDES:
|
for side in SIDES:
|
||||||
if getattr(bed, side) is not None:
|
if getattr(bed, side) is not None:
|
||||||
dev.append(IsInBedBinarySensor(data, bed_id, side))
|
dev.append(IsInBedBinarySensor(data, bed_id, side))
|
||||||
add_entities(dev)
|
add_entities(dev)
|
||||||
|
|
||||||
|
|
||||||
class IsInBedBinarySensor(sleepiq.SleepIQSensor, BinarySensorDevice):
|
class IsInBedBinarySensor(SleepIQSensor, BinarySensorDevice):
|
||||||
"""Implementation of a SleepIQ presence sensor."""
|
"""Implementation of a SleepIQ presence sensor."""
|
||||||
|
|
||||||
def __init__(self, sleepiq_data, bed_id, side):
|
def __init__(self, sleepiq_data, bed_id, side):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
sleepiq.SleepIQSensor.__init__(self, sleepiq_data, bed_id, side)
|
SleepIQSensor.__init__(self, sleepiq_data, bed_id, side)
|
||||||
self.type = sleepiq.IS_IN_BED
|
self.type = IS_IN_BED
|
||||||
self._state = None
|
self._state = None
|
||||||
self._name = sleepiq.SENSOR_TYPES[self.type]
|
self._name = SENSOR_TYPES[self.type]
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -42,5 +44,5 @@ class IsInBedBinarySensor(sleepiq.SleepIQSensor, BinarySensorDevice):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from SleepIQ and updates the states."""
|
"""Get the latest data from SleepIQ and updates the states."""
|
||||||
sleepiq.SleepIQSensor.update(self)
|
SleepIQSensor.update(self)
|
||||||
self._state = self.side.is_in_bed
|
self._state = self.side.is_in_bed
|
||||||
|
11
homeassistant/components/sleepiq/const.py
Normal file
11
homeassistant/components/sleepiq/const.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
"""Define constants for the SleepIQ component."""
|
||||||
|
|
||||||
|
DOMAIN = "sleepiq"
|
||||||
|
|
||||||
|
IS_IN_BED = "is_in_bed"
|
||||||
|
SLEEP_NUMBER = "sleep_number"
|
||||||
|
SENSOR_TYPES = {SLEEP_NUMBER: "SleepNumber", IS_IN_BED: "Is In Bed"}
|
||||||
|
|
||||||
|
LEFT = "left"
|
||||||
|
RIGHT = "right"
|
||||||
|
SIDES = [LEFT, RIGHT]
|
@ -1,5 +1,6 @@
|
|||||||
"""Support for SleepIQ sensors."""
|
"""Support for SleepIQ sensors."""
|
||||||
from homeassistant.components import sleepiq
|
from . import SleepIQSensor
|
||||||
|
from .const import DOMAIN, SENSOR_TYPES, SIDES, SLEEP_NUMBER
|
||||||
|
|
||||||
ICON = "mdi:hotel"
|
ICON = "mdi:hotel"
|
||||||
|
|
||||||
@ -9,27 +10,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
data = sleepiq.DATA
|
data = hass.data[DOMAIN]
|
||||||
data.update()
|
data.update()
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for bed_id, bed in data.beds.items():
|
for bed_id, bed in data.beds.items():
|
||||||
for side in sleepiq.SIDES:
|
for side in SIDES:
|
||||||
if getattr(bed, side) is not None:
|
if getattr(bed, side) is not None:
|
||||||
dev.append(SleepNumberSensor(data, bed_id, side))
|
dev.append(SleepNumberSensor(data, bed_id, side))
|
||||||
add_entities(dev)
|
add_entities(dev)
|
||||||
|
|
||||||
|
|
||||||
class SleepNumberSensor(sleepiq.SleepIQSensor):
|
class SleepNumberSensor(SleepIQSensor):
|
||||||
"""Implementation of a SleepIQ sensor."""
|
"""Implementation of a SleepIQ sensor."""
|
||||||
|
|
||||||
def __init__(self, sleepiq_data, bed_id, side):
|
def __init__(self, sleepiq_data, bed_id, side):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
sleepiq.SleepIQSensor.__init__(self, sleepiq_data, bed_id, side)
|
SleepIQSensor.__init__(self, sleepiq_data, bed_id, side)
|
||||||
|
|
||||||
self._state = None
|
self._state = None
|
||||||
self.type = sleepiq.SLEEP_NUMBER
|
self.type = SLEEP_NUMBER
|
||||||
self._name = sleepiq.SENSOR_TYPES[self.type]
|
self._name = SENSOR_TYPES[self.type]
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@ -45,5 +46,5 @@ class SleepNumberSensor(sleepiq.SleepIQSensor):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from SleepIQ and updates the states."""
|
"""Get the latest data from SleepIQ and updates the states."""
|
||||||
sleepiq.SleepIQSensor.update(self)
|
SleepIQSensor.update(self)
|
||||||
self._state = self.side.sleep_number
|
self._state = self.side.sleep_number
|
||||||
|
Loading…
x
Reference in New Issue
Block a user