mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
move current_position to RollershutterDevice class
This commit is contained in:
parent
e001ea913a
commit
351430c1b3
@ -13,7 +13,7 @@ from homeassistant.helpers.entity import Entity
|
|||||||
from homeassistant.components import group
|
from homeassistant.components import group
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
SERVICE_MOVE_UP, SERVICE_MOVE_DOWN, SERVICE_MOVE_STOP,
|
SERVICE_MOVE_UP, SERVICE_MOVE_DOWN, SERVICE_MOVE_STOP,
|
||||||
STATE_OPEN, ATTR_ENTITY_ID)
|
STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN, ATTR_ENTITY_ID)
|
||||||
|
|
||||||
|
|
||||||
DOMAIN = 'rollershutter'
|
DOMAIN = 'rollershutter'
|
||||||
@ -98,6 +98,21 @@ class RollershutterDevice(Entity):
|
|||||||
""" Represents a rollershutter within Home Assistant. """
|
""" Represents a rollershutter within Home Assistant. """
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_position(self):
|
||||||
|
""" Return current position of rollershutter.
|
||||||
|
None is unknown, 0 is closed, 100 is fully open. """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
current = self.current_position
|
||||||
|
|
||||||
|
if current is None:
|
||||||
|
return STATE_UNKNOWN
|
||||||
|
|
||||||
|
return STATE_CLOSED if current == 0 else STATE_OPEN
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
""" Returns optional state attributes. """
|
""" Returns optional state attributes. """
|
||||||
|
@ -8,7 +8,6 @@ https://home-assistant.io/components/rollershutter.mqtt/
|
|||||||
import logging
|
import logging
|
||||||
import homeassistant.components.mqtt as mqtt
|
import homeassistant.components.mqtt as mqtt
|
||||||
from homeassistant.components.rollershutter import RollershutterDevice
|
from homeassistant.components.rollershutter import RollershutterDevice
|
||||||
from homeassistant.const import (STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN)
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEPENDENCIES = ['mqtt']
|
DEPENDENCIES = ['mqtt']
|
||||||
@ -47,7 +46,7 @@ class MqttRollershutter(RollershutterDevice):
|
|||||||
""" Represents a rollershutter that can be togggled using MQTT """
|
""" Represents a rollershutter that can be togggled using MQTT """
|
||||||
def __init__(self, hass, name, state_topic, command_topic, qos,
|
def __init__(self, hass, name, state_topic, command_topic, qos,
|
||||||
payload_up, payload_down, payload_stop, state_format):
|
payload_up, payload_down, payload_stop, state_format):
|
||||||
self._state = -1
|
self._state = None
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state_topic = state_topic
|
self._state_topic = state_topic
|
||||||
@ -58,17 +57,20 @@ class MqttRollershutter(RollershutterDevice):
|
|||||||
self._payload_stop = payload_stop
|
self._payload_stop = payload_stop
|
||||||
self._parse = mqtt.FmtParser(state_format)
|
self._parse = mqtt.FmtParser(state_format)
|
||||||
|
|
||||||
if self._state_topic:
|
if self._state_topic is None:
|
||||||
def message_received(topic, payload, qos):
|
return
|
||||||
""" A new MQTT message has been received. """
|
|
||||||
value = self._parse(payload)
|
|
||||||
if value.isnumeric():
|
|
||||||
if 0 <= int(value) <= 100:
|
|
||||||
self._state = int(value)
|
|
||||||
self.update_ha_state()
|
|
||||||
|
|
||||||
mqtt.subscribe(hass, self._state_topic, message_received,
|
def message_received(topic, payload, qos):
|
||||||
self._qos)
|
""" A new MQTT message has been received. """
|
||||||
|
value = self._parse(payload)
|
||||||
|
if value.isnumeric() and 0 <= int(value) <= 100:
|
||||||
|
self._state = int(value)
|
||||||
|
self.update_ha_state()
|
||||||
|
else:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Payload is expected to be an integer between 0 and 100")
|
||||||
|
|
||||||
|
mqtt.subscribe(hass, self._state_topic, message_received, self._qos)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
@ -81,19 +83,15 @@ class MqttRollershutter(RollershutterDevice):
|
|||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def current_position(self):
|
||||||
""" Returns the state of the device. """
|
""" Return current position of rollershutter.
|
||||||
if self._state == -1:
|
None is unknown, 0 is closed, 100 is fully open. """
|
||||||
return STATE_UNKNOWN
|
return self._state
|
||||||
elif self._state == 0:
|
|
||||||
return STATE_CLOSED
|
|
||||||
else:
|
|
||||||
return STATE_OPEN
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_open(self):
|
def is_open(self):
|
||||||
""" True if device is open. """
|
""" True if device is open. """
|
||||||
return self.state == STATE_OPEN
|
return self._state > 0
|
||||||
|
|
||||||
def move_up(self, **kwargs):
|
def move_up(self, **kwargs):
|
||||||
""" Moves the device UP. """
|
""" Moves the device UP. """
|
||||||
@ -113,7 +111,7 @@ class MqttRollershutter(RollershutterDevice):
|
|||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
""" Return the state attributes. """
|
""" Return the state attributes. """
|
||||||
state_attr = {
|
state_attr = {}
|
||||||
ATTR_CURRENT_POSITION: self._state,
|
if self._state is not None:
|
||||||
}
|
state_attr[ATTR_CURRENT_POSITION] = self._state
|
||||||
return state_attr
|
return state_attr
|
||||||
|
@ -93,9 +93,9 @@ class TestRollershutterMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
current_position = self.hass.states.get(
|
state_attributes_dict = self.hass.states.get(
|
||||||
'rollershutter.test').attributes['current_position']
|
'rollershutter.test').attributes
|
||||||
self.assertEqual(-1, current_position)
|
self.assertFalse('current_position' in state_attributes_dict)
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '0')
|
fire_mqtt_message(self.hass, 'state-topic', '0')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
@ -114,3 +114,9 @@ class TestRollershutterMQTT(unittest.TestCase):
|
|||||||
current_position = self.hass.states.get(
|
current_position = self.hass.states.get(
|
||||||
'rollershutter.test').attributes['current_position']
|
'rollershutter.test').attributes['current_position']
|
||||||
self.assertEqual(50, current_position)
|
self.assertEqual(50, current_position)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'state-topic', 'non-numeric')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
current_position = self.hass.states.get(
|
||||||
|
'rollershutter.test').attributes['current_position']
|
||||||
|
self.assertEqual(50, current_position)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user