diff --git a/homeassistant/components/rest/binary_sensor.py b/homeassistant/components/rest/binary_sensor.py index bc51433c3c5..fc40d76a21d 100644 --- a/homeassistant/components/rest/binary_sensor.py +++ b/homeassistant/components/rest/binary_sensor.py @@ -100,24 +100,17 @@ class RestBinarySensor(RestEntity, TemplateEntity, BinarySensorEntity): fallback_name=DEFAULT_BINARY_SENSOR_NAME, unique_id=unique_id, ) - self._state = False self._previous_data = None self._value_template = config.get(CONF_VALUE_TEMPLATE) if (value_template := self._value_template) is not None: value_template.hass = hass - self._is_on = None 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): """Update state from the rest data.""" if self.rest.data is None: - self._is_on = False + self._attr_is_on = False response = self.rest.data @@ -127,8 +120,11 @@ class RestBinarySensor(RestEntity, TemplateEntity, BinarySensorEntity): ) try: - self._is_on = bool(int(response)) + self._attr_is_on = bool(int(response)) except ValueError: - self._is_on = {"true": True, "on": True, "open": True, "yes": True}.get( - response.lower(), False - ) + self._attr_is_on = { + "true": True, + "on": True, + "open": True, + "yes": True, + }.get(response.lower(), False) diff --git a/homeassistant/components/rest/switch.py b/homeassistant/components/rest/switch.py index f2a5d93cd22..7e470674b1e 100644 --- a/homeassistant/components/rest/switch.py +++ b/homeassistant/components/rest/switch.py @@ -119,8 +119,6 @@ class RestSwitch(TemplateEntity, SwitchEntity): unique_id=unique_id, ) - self._state = None - auth = None if username := config.get(CONF_USERNAME): 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._params) - @property - def is_on(self): - """Return true if device is on.""" - return self._state - async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" 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) if req.status == HTTPStatus.OK: - self._state = True + self._attr_is_on = True else: _LOGGER.error( "Can't turn on %s. Is resource/endpoint offline?", self._resource @@ -177,7 +170,7 @@ class RestSwitch(TemplateEntity, SwitchEntity): try: req = await self.set_device_state(body_off_t) if req.status == HTTPStatus.OK: - self._state = False + self._attr_is_on = False else: _LOGGER.error( "Can't turn off %s. Is resource/endpoint offline?", self._resource @@ -233,17 +226,17 @@ class RestSwitch(TemplateEntity, SwitchEntity): ) text = text.lower() if text == "true": - self._state = True + self._attr_is_on = True elif text == "false": - self._state = False + self._attr_is_on = False else: - self._state = None + self._attr_is_on = None else: if text == self._body_on.template: - self._state = True + self._attr_is_on = True elif text == self._body_off.template: - self._state = False + self._attr_is_on = False else: - self._state = None + self._attr_is_on = None return req