Use _attr_is_on in rest (#81305)
parent
a0ed91e30c
commit
fee3898f64
|
@ -100,24 +100,17 @@ class RestBinarySensor(RestEntity, TemplateEntity, BinarySensorEntity):
|
||||||
fallback_name=DEFAULT_BINARY_SENSOR_NAME,
|
fallback_name=DEFAULT_BINARY_SENSOR_NAME,
|
||||||
unique_id=unique_id,
|
unique_id=unique_id,
|
||||||
)
|
)
|
||||||
self._state = False
|
|
||||||
self._previous_data = None
|
self._previous_data = None
|
||||||
self._value_template = config.get(CONF_VALUE_TEMPLATE)
|
self._value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||||
if (value_template := self._value_template) is not None:
|
if (value_template := self._value_template) is not None:
|
||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
self._is_on = None
|
|
||||||
|
|
||||||
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Return true if the binary sensor is on."""
|
|
||||||
return self._is_on
|
|
||||||
|
|
||||||
def _update_from_rest_data(self):
|
def _update_from_rest_data(self):
|
||||||
"""Update state from the rest data."""
|
"""Update state from the rest data."""
|
||||||
if self.rest.data is None:
|
if self.rest.data is None:
|
||||||
self._is_on = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
response = self.rest.data
|
response = self.rest.data
|
||||||
|
|
||||||
|
@ -127,8 +120,11 @@ class RestBinarySensor(RestEntity, TemplateEntity, BinarySensorEntity):
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._is_on = bool(int(response))
|
self._attr_is_on = bool(int(response))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self._is_on = {"true": True, "on": True, "open": True, "yes": True}.get(
|
self._attr_is_on = {
|
||||||
response.lower(), False
|
"true": True,
|
||||||
)
|
"on": True,
|
||||||
|
"open": True,
|
||||||
|
"yes": True,
|
||||||
|
}.get(response.lower(), False)
|
||||||
|
|
|
@ -119,8 +119,6 @@ class RestSwitch(TemplateEntity, SwitchEntity):
|
||||||
unique_id=unique_id,
|
unique_id=unique_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
auth = None
|
auth = None
|
||||||
if username := config.get(CONF_USERNAME):
|
if username := config.get(CONF_USERNAME):
|
||||||
auth = aiohttp.BasicAuth(username, password=config[CONF_PASSWORD])
|
auth = aiohttp.BasicAuth(username, password=config[CONF_PASSWORD])
|
||||||
|
@ -149,11 +147,6 @@ class RestSwitch(TemplateEntity, SwitchEntity):
|
||||||
template.attach(hass, self._headers)
|
template.attach(hass, self._headers)
|
||||||
template.attach(hass, self._params)
|
template.attach(hass, self._params)
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Return true if device is on."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
body_on_t = self._body_on.async_render(parse_result=False)
|
body_on_t = self._body_on.async_render(parse_result=False)
|
||||||
|
@ -162,7 +155,7 @@ class RestSwitch(TemplateEntity, SwitchEntity):
|
||||||
req = await self.set_device_state(body_on_t)
|
req = await self.set_device_state(body_on_t)
|
||||||
|
|
||||||
if req.status == HTTPStatus.OK:
|
if req.status == HTTPStatus.OK:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Can't turn on %s. Is resource/endpoint offline?", self._resource
|
"Can't turn on %s. Is resource/endpoint offline?", self._resource
|
||||||
|
@ -177,7 +170,7 @@ class RestSwitch(TemplateEntity, SwitchEntity):
|
||||||
try:
|
try:
|
||||||
req = await self.set_device_state(body_off_t)
|
req = await self.set_device_state(body_off_t)
|
||||||
if req.status == HTTPStatus.OK:
|
if req.status == HTTPStatus.OK:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Can't turn off %s. Is resource/endpoint offline?", self._resource
|
"Can't turn off %s. Is resource/endpoint offline?", self._resource
|
||||||
|
@ -233,17 +226,17 @@ class RestSwitch(TemplateEntity, SwitchEntity):
|
||||||
)
|
)
|
||||||
text = text.lower()
|
text = text.lower()
|
||||||
if text == "true":
|
if text == "true":
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif text == "false":
|
elif text == "false":
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
else:
|
else:
|
||||||
self._state = None
|
self._attr_is_on = None
|
||||||
else:
|
else:
|
||||||
if text == self._body_on.template:
|
if text == self._body_on.template:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif text == self._body_off.template:
|
elif text == self._body_off.template:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
else:
|
else:
|
||||||
self._state = None
|
self._attr_is_on = None
|
||||||
|
|
||||||
return req
|
return req
|
||||||
|
|
Loading…
Reference in New Issue